package log import ( "os" "time" "github.com/natefinch/lumberjack" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) const CallerSkipNum = 1 var ( s *zap.SugaredLogger ) func zapEncoderConfig() zapcore.EncoderConfig { return zapcore.EncoderConfig{ TimeKey: "timestamp", LevelKey: "level", NameKey: "logger", CallerKey: "caller", MessageKey: "message", StacktraceKey: "stacktrace", LineEnding: "\n", EncodeLevel: zapcore.LowercaseLevelEncoder, EncodeTime: func(t time.Time, e zapcore.PrimitiveArrayEncoder) { e.AppendString(t.Format("2006-01-02 15:04:05")) }, EncodeDuration: zapcore.SecondsDurationEncoder, EncodeCaller: zapcore.FullCallerEncoder, } } func init() { config := zap.Config{ Level: zap.NewAtomicLevelAt(zapcore.DebugLevel), Encoding: "json", EncoderConfig: zapEncoderConfig(), InitialFields: map[string]interface{}{"service": "pink"}, OutputPaths: []string{"stdout"}, ErrorOutputPaths: []string{"stdout"}, } logger, err := config.Build(zap.AddCallerSkip(CallerSkipNum)) if err != nil { panic(err) } s = logger.Sugar() } func NewLoggerSugar(serviceName, logFile string, level int32) error { hook := &lumberjack.Logger{ Filename: logFile, // 日志文件路径 MaxSize: 128, // 每个日志文件保存的大小 单位:M MaxAge: 7, // 文件最多保存多少天 MaxBackups: 30, // 日志文件最多保存多少个备份 Compress: false, // 是否压缩 } fileWriter := zapcore.AddSync(hook) writes := []zapcore.WriteSyncer{fileWriter} if zapcore.Level(level) == zapcore.DebugLevel { writes = append(writes, zapcore.AddSync(os.Stdout)) } lowPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool { return lev >= zap.DebugLevel }) zcore := zapcore.NewCore( zapcore.NewJSONEncoder(zapEncoderConfig()), zapcore.NewMultiWriteSyncer(writes...), lowPriority, ) fields := zap.Fields(zap.String("service_name", serviceName)) logger := zap.New(zcore, zap.AddCallerSkip(CallerSkipNum), fields) s = logger.Sugar() return nil } // Debug uses fmt.Sprint to construct and log a message. func Debug(args ...interface{}) { s.Debug(args) } // Info uses fmt.Sprint to construct and log a message. func Info(args ...interface{}) { s.Info(args) } // Warn uses fmt.Sprint to construct and log a message. func Warn(args ...interface{}) { s.Warn(args) } // Error uses fmt.Sprint to construct and log a message. func Error(args ...interface{}) { s.Error(args) } // DPanic uses fmt.Sprint to construct and log a message. In development, the // logger then panics. (See DPanicLevel for details.) func DPanic(args ...interface{}) { s.DPanic(args) } // Panic uses fmt.Sprint to construct and log a message, then panics. func Panic(args ...interface{}) { s.Panic(args) } // Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit. func Fatal(args ...interface{}) { s.Fatal(args) } // Debugf uses fmt.Sprintf to log a templated message. func Debugf(template string, args ...interface{}) { s.Debugf(template, args...) } // Infof uses fmt.Sprintf to log a templated message. func Infof(template string, args ...interface{}) { s.Infof(template, args...) } // Warnf uses fmt.Sprintf to log a templated message. func Warnf(template string, args ...interface{}) { s.Warnf(template, args...) } // Errorf uses fmt.Sprintf to log a templated message. func Errorf(template string, args ...interface{}) { s.Errorf(template, args...) } // DPanicf uses fmt.Sprintf to log a templated message. In development, the // logger then panics. (See DPanicLevel for details.) func DPanicf(template string, args ...interface{}) { s.DPanicf(template, args...) } // Panicf uses fmt.Sprintf to log a templated message, then panics. func Panicf(template string, args ...interface{}) { s.Panicf(template, args...) } // Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit. func Fatalf(template string, args ...interface{}) { s.Fatalf(template, args...) }