liuxiaolong
2020-12-21 494dab566ec8a70f20b852db298c51aa6fe709b2
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
}
 
var (
    blog *BLog
    atomicLevel = zap.NewAtomicLevelAt(zapcore.DebugLevel)
)
 
func (l *BLog) Write(buf []byte) (n int, err error) {
    l.logger.Debug(strings.Replace(string(buf),"\n","", -1))
    return len(buf),nil
}
 
func GetLogFile() *BLog {
    return  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/"
    logName := ""
    if index := strings.LastIndex(logPath, "/"); index != -1 {
        logdir = logPath[0:index] + "/"
        if index < len(logPath)-1 {
            logName = logPath[index+1:]
        }
    } else {
        logName = logPath
    }
    if logName == "" {
        logName = "log.log"
    }
    fi,err := os.Stat(logdir)
    if !((err == nil || os.IsExist(err)) && fi.IsDir()) {
        os.MkdirAll(logdir, os.ModePerm)
    }
    if logLevel < DebugLevel || logLevel > FatalLevel {
        logLevel = DebugLevel
    }
    if maxSize <=0 {
        maxSize = 128
    }
    if maxBackups <=0 {
        maxBackups = 30
    }
    if maxAge <= 0 {
        maxAge = 15
    }
    hook := lumberjack.Logger {
        Filename: logdir+logName,   //日志文件的位置
        MaxSize: maxSize,        //在进行切割之前,日志文件的最大大小(以MB为单位)
        MaxBackups: maxBackups,      //保留旧文件的最大个数
        MaxAge: maxAge,              //保留旧文件的最大天数
        Compress: true,        //是否压缩
        LocalTime: true,
    }
    w := zapcore.AddSync(&hook)
 
    atomicLevel = zap.NewAtomicLevelAt(getZapLevel(logLevel))
 
    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,
        atomicLevel,
    )
 
    log := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
 
    blog = &BLog{
        logger: log.Sugar(),
    }
    blog.logger.Info("init logger success")
}
 
func getZapLevel(logLevel int) zapcore.Level {
    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
    }
    return level
}
 
func SetLevel(lv int) error {
    atomicLevel.SetLevel(getZapLevel(lv))
    return nil
}
 
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...)
}
 
func Fatal(v ...interface{}) {
    blog.logger.Fatal(v...)
}
 
func Fatalf(template string, v ...interface{}) {
    blog.logger.Fatalf(template, v...)
}