zhangqian
2023-08-26 5193dcb9336e853502baf8a539d3f45efebe2f86
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
package config
 
import (
    "go.uber.org/zap/zapcore"
    "strings"
)
 
type Zap struct {
    Level         string `mapstructure:"level" json:"level" yaml:"level"`                            // 级别
    Prefix        string `mapstructure:"prefix" json:"prefix" yaml:"prefix"`                         // 日志前缀
    Format        string `mapstructure:"format" json:"format" yaml:"format"`                         // 输出
    Director      string `mapstructure:"director" json:"director"  yaml:"director"`                  // 日志文件夹
    EncodeLevel   string `mapstructure:"encode-level" json:"encode-level" yaml:"encode-level"`       // 编码级
    StacktraceKey string `mapstructure:"stacktrace-key" json:"stacktrace-key" yaml:"stacktrace-key"` // 栈名
 
    MaxAge       int  `mapstructure:"max-age" json:"max-age" yaml:"max-age"`                      // 日志留存时间
    ShowLine     bool `mapstructure:"show-line" json:"show-line" yaml:"show-line"`                // 显示行
    LogInConsole bool `mapstructure:"log-in-console" json:"log-in-console" yaml:"log-in-console"` // 输出控制台
}
 
// ZapEncodeLevel 根据 EncodeLevel 返回 zapcore.LevelEncoder
// Author [SliverHorn](https://github.com/SliverHorn)
func (z *Zap) ZapEncodeLevel() zapcore.LevelEncoder {
    switch {
    case z.EncodeLevel == "LowercaseLevelEncoder": // 小写编码器(默认)
        return zapcore.LowercaseLevelEncoder
    case z.EncodeLevel == "LowercaseColorLevelEncoder": // 小写编码器带颜色
        return zapcore.LowercaseColorLevelEncoder
    case z.EncodeLevel == "CapitalLevelEncoder": // 大写编码器
        return zapcore.CapitalLevelEncoder
    case z.EncodeLevel == "CapitalColorLevelEncoder": // 大写编码器带颜色
        return zapcore.CapitalColorLevelEncoder
    default:
        return zapcore.LowercaseLevelEncoder
    }
}
 
// TransportLevel 根据字符串转化为 zapcore.Level
// Author [SliverHorn](https://github.com/SliverHorn)
func (z *Zap) TransportLevel() zapcore.Level {
    z.Level = strings.ToLower(z.Level)
    switch z.Level {
    case "debug":
        return zapcore.DebugLevel
    case "info":
        return zapcore.InfoLevel
    case "warn":
        return zapcore.WarnLevel
    case "error":
        return zapcore.WarnLevel
    case "dpanic":
        return zapcore.DPanicLevel
    case "panic":
        return zapcore.PanicLevel
    case "fatal":
        return zapcore.FatalLevel
    default:
        return zapcore.DebugLevel
    }
}