github.com/vicanso/pike@v1.0.1-0.20210630235453-9099e041f6ec/log/log.go (about) 1 // MIT License 2 3 // Copyright (c) 2020 Tree Xie 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 package log 24 25 import ( 26 "net/url" 27 "strconv" 28 29 "go.uber.org/zap" 30 "go.uber.org/zap/zapcore" 31 "gopkg.in/natefinch/lumberjack.v2" 32 ) 33 34 func init() { 35 err := zap.RegisterSink("lumberjack", newLumberJack) 36 if err != nil { 37 panic(err) 38 } 39 } 40 41 var defaultLogger = newLoggerX("") 42 43 type LumberjackLogger struct { 44 lumberjack.Logger 45 } 46 47 func (ll *LumberjackLogger) Sync() error { 48 return nil 49 } 50 51 func newLumberJack(u *url.URL) (zap.Sink, error) { 52 maxSize := 0 53 v := u.Query().Get("maxSize") 54 if v != "" { 55 maxSize, _ = strconv.Atoi(v) 56 } 57 maxAge := 0 58 v = u.Query().Get("maxAge") 59 if v != "" { 60 maxAge, _ = strconv.Atoi(v) 61 } 62 if maxAge == 0 { 63 maxAge = 1 64 } 65 compress := false 66 if u.Query().Get("compress") == "true" { 67 compress = true 68 } 69 70 return &LumberjackLogger{ 71 Logger: lumberjack.Logger{ 72 MaxSize: maxSize, 73 MaxAge: maxAge, 74 Filename: u.Path, 75 Compress: compress, 76 }, 77 }, nil 78 } 79 80 // newLoggerX 初始化logger 81 func newLoggerX(outputPath string) *zap.Logger { 82 83 c := zap.NewProductionConfig() 84 if outputPath != "" { 85 c.OutputPaths = []string{ 86 outputPath, 87 } 88 c.ErrorOutputPaths = []string{ 89 outputPath, 90 } 91 } 92 93 // 在一秒钟内, 如果某个级别的日志输出量超过了 Initial, 那么在超过之后, 每 Thereafter 条日志才会输出一条, 其余的日志都将被删除 94 // 如果需要输出所有日志,则设置为nil 95 c.Sampling = nil 96 // pike的日志比较简单,因此不添加caller 97 c.DisableCaller = true 98 99 c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder 100 // 只针对panic 以上的日志增加stack trace 101 l, err := c.Build(zap.AddStacktrace(zap.DPanicLevel)) 102 if err != nil { 103 panic(err) 104 } 105 return l 106 } 107 108 func SetOutputPath(outputPath string) { 109 defaultLogger = newLoggerX(outputPath) 110 } 111 112 // Default get default logger 113 func Default() *zap.Logger { 114 return defaultLogger 115 }