互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.8 KiB

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.SugaredLogger
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()
}
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(args ...interface{}) {
logger.Info(args...)
}
func Debug(args ...interface{}) {
logger.Debug(args...)
}
func Panic(args ...interface{}) {
logger.Panic(args...)
}
func Warn(args ...interface{}) {
logger.Warn(args...)
}
func Error(args ...interface{}) {
logger.Error(args...)
}