|
| 1 | +package lg |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "go.uber.org/zap" |
| 9 | + "go.uber.org/zap/zapcore" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + // APPLog is global logger |
| 14 | + APPLog *zap.Logger |
| 15 | + |
| 16 | + // TimeFormat is custom Time format |
| 17 | + // example: "2006-01-02T15:04:05.999999999Z07:00" |
| 18 | + // 推荐不要设置, 使用默认时间戳 |
| 19 | + TimeFormat string |
| 20 | + |
| 21 | + // onceInit guarantee initialize logger only once |
| 22 | + onceInit sync.Once |
| 23 | +) |
| 24 | + |
| 25 | +type commonInfo struct { |
| 26 | + Project string `json:"project"` |
| 27 | + Hostname string `json:"hostname"` |
| 28 | +} |
| 29 | + |
| 30 | +func (i *commonInfo) MarshalLogObject(enc zapcore.ObjectEncoder) error { |
| 31 | + enc.AddString("project", i.Project) |
| 32 | + enc.AddString("hostname", i.Hostname) |
| 33 | + return nil |
| 34 | +} |
| 35 | + |
| 36 | +// customTimeEncoder encode Time to our custom format |
| 37 | +// This example how we can customize zap default functionality |
| 38 | +func customTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { |
| 39 | + enc.AppendString(t.Format(TimeFormat)) |
| 40 | +} |
| 41 | + |
| 42 | +// Init initializes log by input parameters |
| 43 | +// lvl - global log level: Debug(-1), Info(0), Warn(1), Error(2), DPanic(3), Panic(4), Fatal(5) |
| 44 | +// timeFormat - custom time format for logger of empty string to use default |
| 45 | +func Init(lvl int, project string) (err error) { |
| 46 | + onceInit.Do(func() { |
| 47 | + // First, define our level-handling logic. |
| 48 | + globalLevel := zapcore.Level(lvl) |
| 49 | + // High-priority output should also go to standard error, and low-priority |
| 50 | + // output should also go to standard out. |
| 51 | + // It is usefull for Kubernetes deployment. |
| 52 | + // Kubernetes interprets os.Stdout log items as INFO and os.Stderr log items |
| 53 | + // as ERROR by default. |
| 54 | + highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { |
| 55 | + return lvl >= zapcore.ErrorLevel |
| 56 | + }) |
| 57 | + lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { |
| 58 | + return lvl >= globalLevel && lvl < zapcore.ErrorLevel |
| 59 | + }) |
| 60 | + |
| 61 | + cfg := zap.NewProductionEncoderConfig() |
| 62 | + if len(TimeFormat) > 0 { |
| 63 | + // 默认的TimeKey为(ts) float64类型. 自定义会将TimeKey修改为string,防止es中出现问题,所以换个新的key叫tsp |
| 64 | + cfg.TimeKey = "tsp" |
| 65 | + cfg.EncodeTime = customTimeEncoder |
| 66 | + } |
| 67 | + // Optimize the Kafka output for machine consumption and the console output |
| 68 | + // for human operators. |
| 69 | + consoleEncoder := zapcore.NewJSONEncoder(cfg) |
| 70 | + // Join the outputs, encoders, and level-handling functions into |
| 71 | + // zapcore.Cores, then tee the four cores together. |
| 72 | + core := zapcore.NewTee( |
| 73 | + zapcore.NewCore(consoleEncoder, zapcore.Lock(os.Stdout), lowPriority), |
| 74 | + zapcore.NewCore(consoleEncoder, zapcore.Lock(os.Stderr), highPriority), |
| 75 | + ) |
| 76 | + |
| 77 | + // ErrorLevel 堆栈跟踪 |
| 78 | + stackTrace := zap.AddStacktrace(zap.ErrorLevel) |
| 79 | + // 设置初始化字段 |
| 80 | + fields := zap.Fields(zap.Object("info", &commonInfo{project, getHostName()})) |
| 81 | + |
| 82 | + // From a zapcore.Core, it's easy to construct a Logger. |
| 83 | + APPLog = zap.New(core, fields, stackTrace) |
| 84 | + zap.RedirectStdLog(APPLog) |
| 85 | + }) |
| 86 | + |
| 87 | + return err |
| 88 | +} |
| 89 | + |
| 90 | +// getHostName 获取当前主机的Hostname |
| 91 | +func getHostName() string { |
| 92 | + if host, err := os.Hostname(); err == nil { |
| 93 | + return host |
| 94 | + } |
| 95 | + return "unknown" |
| 96 | +} |
0 commit comments