zap是Uber开源的高性能log包。
1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/natefinch/lumberjack" 8 "go.uber.org/zap" 9 "go.uber.org/zap/zapcore" 10 ) 11 12 // logpath 日志文件路径 13 // loglevel 日志级别 14 func initLogger(logpath string, loglevel string) *zap.Logger { 15 16 hook := lumberjack.Logger{ 17 Filename: logpath, // 日志文件路径 18 MaxSize: 128, // megabytes 19 MaxBackups: 30, // 最多保留300个备份 20 MaxAge: 7, // days 21 Compress: true, // 是否压缩 disabled by default 22 } 23 24 w := zapcore.AddSync(&hook) 25 26 // 设置日志级别,debug可以打印出info,debug,warn;info级别可以打印warn,info;warn只能打印warn 27 // debug->info->warn->error 28 var level zapcore.Level 29 switch loglevel { 30 case "debug": 31 level = zap.DebugLevel 32 case "info": 33 level = zap.InfoLevel 34 case "error": 35 level = zap.ErrorLevel 36 default: 37 level = zap.InfoLevel 38 } 39 encoderConfig := zap.NewProductionEncoderConfig() 40 // 时间格式 41 encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder 42 core := zapcore.NewCore( 43 zapcore.NewConsoleEncoder(encoderConfig), 44 w, 45 level, 46 ) 47 48 logger := zap.New(core) 49 logger.Info("DefaultLogger init success") 50 51 return logger 52 } 53 54 type Test struct { 55 Name string `json:"name"` 56 Age int `json:"age"` 57 } 58 59 func main() { 60 t := &Test{ 61 Name: "xiaoming", 62 Age: 12, 63 } 64 data, err := json.Marshal(t) 65 if err != nil { 66 fmt.Println("marshal is failed,err: ", err) 67 } 68 69 // 历史记录日志名字为:all-2018-11-15T07-45-51.763.log,服务重新启动,日志会追加,不会删除 70 logger := initLogger("./all.log", "debug") 71 for i := 0; i < 6; i++ { 72 logger.Info(fmt.Sprint("test log ", i), zap.Int("line", 47)) 73 logger.Debug(fmt.Sprint("debug log ", i), zap.ByteString("level", data)) 74 logger.Info(fmt.Sprint("Info log ", i), zap.String("level", `{"a":"4","b":"5"}`)) 75 logger.Warn(fmt.Sprint("Info log ", i), zap.String("level", `{"a":"7","b":"8"}`)) 76 } 77 78 }
all.log内容
2018-11-15T16:19:30.269+0800 info DefaultLogger init success 2018-11-15T16:19:30.289+0800 info test log 0 {"line": 47} 2018-11-15T16:19:30.289+0800 info Info log 0 {"level": "{\"a\":\"4\",\"b\":\"5\"}"} 2018-11-15T16:19:30.289+0800 warn Info log 0 {"level": "{\"a\":\"7\",\"b\":\"8\"}"} 2018-11-15T16:19:30.289+0800 info test log 1 {"line": 47} 2018-11-15T16:19:30.289+0800 info Info log 1 {"level": "{\"a\":\"4\",\"b\":\"5\"}"} 2018-11-15T16:19:30.289+0800 warn Info log 1 {"level": "{\"a\":\"7\",\"b\":\"8\"}"} 2018-11-15T16:19:30.289+0800 info test log 2 {"line": 47} 2018-11-15T16:19:30.289+0800 info Info log 2 {"level": "{\"a\":\"4\",\"b\":\"5\"}"} 2018-11-15T16:19:30.289+0800 warn Info log 2 {"level": "{\"a\":\"7\",\"b\":\"8\"}"} 2018-11-15T16:19:30.289+0800 info test log 3 {"line": 47} 2018-11-15T16:19:30.289+0800 info Info log 3 {"level": "{\"a\":\"4\",\"b\":\"5\"}"} 2018-11-15T16:19:30.289+0800 warn Info log 3 {"level": "{\"a\":\"7\",\"b\":\"8\"}"} 2018-11-15T16:19:30.289+0800 info test log 4 {"line": 47} 2018-11-15T16:19:30.289+0800 info Info log 4 {"level": "{\"a\":\"4\",\"b\":\"5\"}"} 2018-11-15T16:19:30.289+0800 warn Info log 4 {"level": "{\"a\":\"7\",\"b\":\"8\"}"} 2018-11-15T16:19:30.289+0800 info test log 5 {"line": 47} 2018-11-15T16:19:30.289+0800 info Info log 5 {"level": "{\"a\":\"4\",\"b\":\"5\"}"} 2018-11-15T16:19:30.289+0800 warn Info log 5 {"level": "{\"a\":\"7\",\"b\":\"8\"}"}