liuxiaolong
2020-11-10 311cf16f18049d43fbda4ce357193f210d07496a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package logger
 
import (
    "github.com/natefinch/lumberjack"
    "go.uber.org/zap"
    "go.uber.org/zap/zapcore"
    "os"
    "strings"
    "time"
)
 
type BLog struct {
    logger *zap.SugaredLogger
}
 
func (l *BLog) Write(buf []byte) (n int, err error) {
    l.logger.Debug(string(buf))
    return len(buf),nil
}
 
func GetLogFile() *BLog {
    return  blog
}
 
var blog *BLog
 
const (
    DebugLevel = iota -1
    InfoLevel
    WarnLevel
    ErrorLevel
    DPanicLevel
    PanicLevel
    FatalLevel
)
func InitLogger(logPath string, logLevel int,maxSize int, maxBackups int, maxAge int) {
    logdir := "./logger/"
    if index := strings.LastIndex(logPath, "/"); index != -1 {
        logdir = logPath[0:index] + "/"
    }
    fi,err := os.Stat(logdir)
    if !((err == nil || os.IsExist(err)) && fi.IsDir()) {
        os.MkdirAll(logdir, os.ModePerm)
    }
    hook := lumberjack.Logger {
        Filename: logPath,   //日志文件的位置
        MaxSize: maxSize,        //在进行切割之前,日志文件的最大大小(以MB为单位)
        MaxBackups: maxBackups,      //保留旧文件的最大个数
        MaxAge: maxAge,              //保留旧文件的最大天数
        Compress: true,        //是否压缩
        LocalTime: true,
    }
    w := zapcore.AddSync(&hook)
 
    var level zapcore.Level
    switch logLevel {
    case DebugLevel:
        level = zap.DebugLevel
    case InfoLevel:
        level = zap.InfoLevel
    case WarnLevel:
        level = zap.WarnLevel
    case ErrorLevel:
        level = zap.ErrorLevel
    case DPanicLevel:
        level = zap.DPanicLevel
    case PanicLevel:
        level = zap.PanicLevel
    case FatalLevel:
        level = zap.FatalLevel
    default:
        level = zap.InfoLevel
    }
    encoderConfig := zap.NewProductionEncoderConfig()
    encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
    encoderConfig.CallerKey = "file"
    encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
    encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
        enc.AppendString(t.Format("2006-01-02 15:04:05"))
    }
 
    core := zapcore.NewCore(
        zapcore.NewConsoleEncoder(encoderConfig),
        w,
        level,
    )
    log := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
    blog = &BLog{
        logger: log.Sugar(),
    }
    blog.logger.Info("init logger success")
}
 
func Debug(v ...interface{}) {
    blog.logger.Debug(v...)
}
 
func Debugf(template string, v ...interface{}) {
    blog.logger.Debugf(template, v...)
}
 
func Info(v ...interface{}) {
    blog.logger.Info(v)
}
 
func Infof(template string, v ...interface{}) {
    blog.logger.Infof(template, v...)
}
 
func Warn(v ...interface{}) {
    blog.logger.Warn(v...)
}
 
func Warnf(template string, v ...interface{}) {
    blog.logger.Warnf(template, v...)
}
 
func Error(v ...interface{}) {
    blog.logger.Error(v...)
}
 
func Errorf(template string, v ...interface{}) {
    blog.logger.Errorf(template, v...)
}