197 lines
4.7 KiB
Go
197 lines
4.7 KiB
Go
package log
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"go.opentelemetry.io/otel/trace"
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
)
|
|
|
|
var workdir, _ = filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
type Logger struct {
|
|
*slog.Logger
|
|
}
|
|
|
|
func SetDefault(logger *Logger) {
|
|
slog.SetDefault(logger.Logger)
|
|
}
|
|
|
|
func NewLogger(conf LogConf) *Logger {
|
|
var log *slog.Logger
|
|
|
|
logLevel := slog.LevelInfo
|
|
switch strings.ToLower(conf.Level) {
|
|
case "error":
|
|
logLevel = slog.LevelError
|
|
case "warn":
|
|
logLevel = slog.LevelWarn
|
|
case "info":
|
|
logLevel = slog.LevelInfo
|
|
case "debug":
|
|
logLevel = slog.LevelDebug
|
|
}
|
|
|
|
if !path.IsAbs(conf.Filename) {
|
|
conf.Filename = filepath.Join(workdir, conf.Filename)
|
|
}
|
|
|
|
// 创建lumberjack日志滚动器
|
|
lumberjackLogger := &lumberjack.Logger{
|
|
Filename: conf.Filename,
|
|
MaxSize: conf.MaxSize,
|
|
MaxBackups: conf.MaxBackups,
|
|
MaxAge: conf.MaxAge,
|
|
Compress: conf.Compress,
|
|
} // 创建写入器
|
|
|
|
var writer io.Writer = lumberjackLogger
|
|
if conf.Console {
|
|
writer = io.MultiWriter(writer, os.Stdout)
|
|
}
|
|
log = slog.New(NewContextHandler(writer, conf.Format, &slog.HandlerOptions{
|
|
Level: logLevel,
|
|
AddSource: conf.AddSource,
|
|
ReplaceAttr: func(_ []string, attr slog.Attr) slog.Attr {
|
|
switch attr.Key {
|
|
case "caller":
|
|
return slog.Attr{}
|
|
case slog.SourceKey:
|
|
if source, ok := attr.Value.Any().(*slog.Source); ok {
|
|
if source.File == "" {
|
|
return slog.Attr{}
|
|
}
|
|
return slog.String(slog.SourceKey, filepath.Base(source.File)+fmt.Sprintf(":%d", source.Line))
|
|
}
|
|
case slog.TimeKey:
|
|
return slog.String(slog.TimeKey, attr.Value.Time().Format(conf.TimeFormat))
|
|
}
|
|
return attr
|
|
},
|
|
}))
|
|
return &Logger{
|
|
Logger: log,
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Alert(v any) {
|
|
l.Logger.Info("%v", v)
|
|
}
|
|
|
|
func (l *Logger) Close() error {
|
|
return nil
|
|
}
|
|
|
|
func (l *Logger) buildAttrs(fields []logx.LogField) []slog.Attr {
|
|
attrs := make([]slog.Attr, 0, len(fields))
|
|
for _, field := range fields {
|
|
attrs = append(attrs, slog.Attr{
|
|
Key: field.Key,
|
|
Value: slog.AnyValue(field.Value),
|
|
})
|
|
}
|
|
return attrs
|
|
}
|
|
|
|
func (l *Logger) DebugCtx(ctx context.Context, v ...any) {
|
|
if !l.Enabled(ctx, slog.LevelDebug) {
|
|
return
|
|
}
|
|
var pcs [1]uintptr
|
|
// skip [runtime.Callers, this function, this function's caller]
|
|
runtime.Callers(2, pcs[:])
|
|
pc := pcs[0]
|
|
|
|
r := slog.NewRecord(time.Now(), slog.LevelDebug, fmt.Sprint(v...), pc)
|
|
var attrs []slog.Attr
|
|
spanCtx := trace.SpanContextFromContext(ctx)
|
|
if spanCtx.HasTraceID() {
|
|
attrs = append(attrs, slog.String("trace", spanCtx.TraceID().String()))
|
|
}
|
|
if spanCtx.HasSpanID() {
|
|
attrs = append(attrs, slog.String("span", spanCtx.SpanID().String()))
|
|
}
|
|
r.AddAttrs(attrs...)
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
_ = l.Handler().Handle(ctx, r)
|
|
}
|
|
|
|
func (l *Logger) log(ctx context.Context, level slog.Level, msg string, args ...any) {
|
|
if !l.Enabled(ctx, level) {
|
|
return
|
|
}
|
|
var pc uintptr
|
|
var pcs [1]uintptr
|
|
// skip [runtime.Callers, this function, this function's caller]
|
|
runtime.Callers(5, pcs[:])
|
|
pc = pcs[0]
|
|
|
|
r := slog.NewRecord(time.Now(), level, msg, pc)
|
|
r.Add(args...)
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
_ = l.Handler().Handle(ctx, r)
|
|
}
|
|
func (l *Logger) logAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr) {
|
|
if !l.Enabled(ctx, level) {
|
|
return
|
|
}
|
|
var pcs [1]uintptr
|
|
// skip [runtime.Callers, this function, this function's caller]
|
|
runtime.Callers(5, pcs[:])
|
|
pc := pcs[0]
|
|
|
|
r := slog.NewRecord(time.Now(), level, msg, pc)
|
|
r.AddAttrs(attrs...)
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
_ = l.Handler().Handle(ctx, r)
|
|
}
|
|
|
|
func (l *Logger) Debug(v any, fields ...logx.LogField) {
|
|
attrs := l.buildAttrs(fields)
|
|
l.logAttrs(context.Background(), slog.LevelDebug, fmt.Sprintf("%v", v), attrs...)
|
|
}
|
|
|
|
func (l *Logger) Error(v any, fields ...logx.LogField) {
|
|
attrs := l.buildAttrs(fields)
|
|
l.logAttrs(context.Background(), slog.LevelError, fmt.Sprintf("%v", v), attrs...)
|
|
}
|
|
|
|
func (l *Logger) Info(v any, fields ...logx.LogField) {
|
|
attrs := l.buildAttrs(fields)
|
|
l.logAttrs(context.Background(), slog.LevelInfo, fmt.Sprintf("%v", v), attrs...)
|
|
}
|
|
|
|
func (l *Logger) Severe(v any) {
|
|
l.log(context.Background(), slog.LevelError, fmt.Sprintf("%v", v))
|
|
}
|
|
|
|
func (l *Logger) Slow(v any, fields ...logx.LogField) {
|
|
attrs := l.buildAttrs(fields)
|
|
l.logAttrs(context.Background(), slog.LevelWarn, fmt.Sprintf("%v", v), attrs...)
|
|
}
|
|
|
|
func (l *Logger) Stack(v any) {
|
|
l.log(context.Background(), slog.LevelError, fmt.Sprintf("%v", v))
|
|
}
|
|
|
|
func (l *Logger) Stat(v any, fields ...logx.LogField) {
|
|
attrs := l.buildAttrs(fields)
|
|
l.logAttrs(context.Background(), slog.LevelInfo, fmt.Sprintf("%v", v), attrs...)
|
|
}
|