2024-02-12 04:52:41 +08:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-13 02:22:52 +08:00
|
|
|
"os"
|
2024-02-12 04:52:41 +08:00
|
|
|
"sync"
|
2024-02-13 02:22:52 +08:00
|
|
|
"time"
|
2024-02-12 04:52:41 +08:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2024-02-13 02:22:52 +08:00
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
2024-02-12 04:52:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type contextKey = string
|
|
|
|
|
|
|
|
const loggerKey = contextKey("logger")
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultLogger *zap.SugaredLogger
|
|
|
|
defaultLoggerOnce sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
var conf = &Config{
|
|
|
|
Level: zapcore.InfoLevel,
|
|
|
|
Development: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Level zapcore.Level
|
|
|
|
Development bool
|
2024-02-13 02:22:52 +08:00
|
|
|
FilePath string
|
2024-02-12 04:52:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetConfig(c *Config) {
|
|
|
|
conf = &Config{
|
|
|
|
Level: c.Level,
|
|
|
|
Development: c.Development,
|
2024-02-13 02:22:52 +08:00
|
|
|
FilePath: c.FilePath,
|
2024-02-12 04:52:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetLevel(l zapcore.Level) {
|
|
|
|
conf.Level = l
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLogger(conf *Config) *zap.SugaredLogger {
|
2024-02-13 02:22:52 +08:00
|
|
|
|
2024-02-12 04:52:41 +08:00
|
|
|
ec := zap.NewProductionEncoderConfig()
|
|
|
|
ec.EncodeTime = zapcore.ISO8601TimeEncoder
|
2024-02-13 02:22:52 +08:00
|
|
|
ec.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
|
|
|
ec.CallerKey = ""
|
|
|
|
ec.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
|
|
|
enc.AppendString(t.Format("02/01/2006 03:04 PM"))
|
2024-02-12 04:52:41 +08:00
|
|
|
}
|
2024-02-13 02:22:52 +08:00
|
|
|
|
|
|
|
var cores []zapcore.Core
|
|
|
|
|
|
|
|
cores = append(cores, zapcore.NewCore(zapcore.NewConsoleEncoder(ec),
|
|
|
|
zapcore.AddSync(os.Stdout), zap.NewAtomicLevelAt(conf.Level)))
|
|
|
|
|
|
|
|
if conf.FilePath != "" {
|
|
|
|
lumberjackLogger := &lumberjack.Logger{
|
|
|
|
Filename: conf.FilePath,
|
|
|
|
MaxSize: 10,
|
|
|
|
MaxBackups: 3,
|
|
|
|
MaxAge: 15,
|
|
|
|
Compress: true,
|
|
|
|
}
|
|
|
|
cores = append(cores, zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
|
|
|
|
zapcore.AddSync(lumberjackLogger), zap.NewAtomicLevelAt(conf.Level)))
|
|
|
|
}
|
|
|
|
|
|
|
|
options := []zap.Option{}
|
|
|
|
if conf.Development {
|
|
|
|
options = append(options, zap.Development())
|
|
|
|
|
2024-02-12 04:52:41 +08:00
|
|
|
}
|
2024-02-13 02:22:52 +08:00
|
|
|
return zap.New(zapcore.NewTee(cores...), options...).Sugar()
|
2024-02-12 04:52:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func DefaultLogger() *zap.SugaredLogger {
|
|
|
|
defaultLoggerOnce.Do(func() {
|
|
|
|
defaultLogger = NewLogger(conf)
|
|
|
|
})
|
|
|
|
return defaultLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithLogger(ctx context.Context, logger *zap.SugaredLogger) context.Context {
|
|
|
|
if gCtx, ok := ctx.(*gin.Context); ok {
|
|
|
|
ctx = gCtx.Request.Context()
|
|
|
|
}
|
|
|
|
return context.WithValue(ctx, loggerKey, logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromContext(ctx context.Context) *zap.SugaredLogger {
|
|
|
|
if ctx == nil {
|
|
|
|
return DefaultLogger()
|
|
|
|
}
|
|
|
|
if gCtx, ok := ctx.(*gin.Context); ok && gCtx != nil {
|
|
|
|
ctx = gCtx.Request.Context()
|
|
|
|
}
|
|
|
|
if logger, ok := ctx.Value(loggerKey).(*zap.SugaredLogger); ok {
|
|
|
|
return logger
|
|
|
|
}
|
|
|
|
return DefaultLogger()
|
|
|
|
}
|