|
|
package logger
import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" "gopkg.in/natefinch/lumberjack.v2" "os" "time" )
var Sugar *zap.SugaredLogger var logger *zap.Logger
func init() { InitZap() } func InitZap() { w := zapcore.AddSync(&lumberjack.Logger{ Filename: "log/hdzj.log", MaxSize: 50, // megabytes
MaxBackups: 3, MaxAge: 7, // days
}) core := zapcore.NewCore( zapcore.NewConsoleEncoder(NewEncoderConfig()), zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), w), zap.DebugLevel) logger = zap.New(core, zap.AddCaller()) Sugar = logger.Sugar() } func NewEncoderConfig() zapcore.EncoderConfig { return zapcore.EncoderConfig{ // Keys can be anything except the empty string.
TimeKey: "T", LevelKey: "L", NameKey: "N", CallerKey: "C", MessageKey: "M", StacktraceKey: "S", LineEnding: zapcore.DefaultLineEnding, EncodeLevel: zapcore.CapitalLevelEncoder, EncodeTime: TimeEncoder, EncodeDuration: zapcore.StringDurationEncoder, EncodeCaller: zapcore.ShortCallerEncoder, } }
func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { enc.AppendString(t.Format("2006-01-02 15:04:05.000")) }
func level(level string) zapcore.Level { switch level { case "info": return zap.InfoLevel case "fatal": return zap.FatalLevel case "panic": return zap.PanicLevel case "error": return zap.ErrorLevel case "dpanic": return zap.DPanicLevel case "warn": return zap.WarnLevel default: return zap.DebugLevel } }
func Info(msg string, fields ...zap.Field) { logger.Info(msg, fields...) }
func Debug(msg string, fields ...zap.Field) { logger.Debug(msg, fields...) }
func Panic(msg string, fields ...zap.Field) { logger.Debug(msg, fields...) }
func Warn(msg string, fields ...zap.Field) { logger.Warn(msg, fields...) }
func DPanic(msg string, fields ...zap.Field) { logger.DPanic(msg, fields...) }
func Error(msg string, fields ...zap.Field) { logger.Error(msg, fields...) }
func With(fields ...zap.Field) { logger.With(fields...) }
func WithOptions(opts ...zap.Option) { logger.WithOptions(opts...) }
|