github.com/shuguocloud/go-zero@v1.3.0/core/logx/durationlogger.go (about) 1 package logx 2 3 import ( 4 "fmt" 5 "io" 6 "time" 7 8 "github.com/shuguocloud/go-zero/core/timex" 9 ) 10 11 const durationCallerDepth = 3 12 13 type durationLogger logEntry 14 15 // WithDuration returns a Logger which logs the given duration. 16 func WithDuration(d time.Duration) Logger { 17 return &durationLogger{ 18 Duration: timex.ReprOfDuration(d), 19 } 20 } 21 22 func (l *durationLogger) Error(v ...interface{}) { 23 if shallLog(ErrorLevel) { 24 l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth)) 25 } 26 } 27 28 func (l *durationLogger) Errorf(format string, v ...interface{}) { 29 if shallLog(ErrorLevel) { 30 l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth)) 31 } 32 } 33 34 func (l *durationLogger) Errorv(v interface{}) { 35 if shallLog(ErrorLevel) { 36 l.write(errorLog, levelError, v) 37 } 38 } 39 40 func (l *durationLogger) Info(v ...interface{}) { 41 if shallLog(InfoLevel) { 42 l.write(infoLog, levelInfo, fmt.Sprint(v...)) 43 } 44 } 45 46 func (l *durationLogger) Infof(format string, v ...interface{}) { 47 if shallLog(InfoLevel) { 48 l.write(infoLog, levelInfo, fmt.Sprintf(format, v...)) 49 } 50 } 51 52 func (l *durationLogger) Infov(v interface{}) { 53 if shallLog(InfoLevel) { 54 l.write(infoLog, levelInfo, v) 55 } 56 } 57 58 func (l *durationLogger) Slow(v ...interface{}) { 59 if shallLog(ErrorLevel) { 60 l.write(slowLog, levelSlow, fmt.Sprint(v...)) 61 } 62 } 63 64 func (l *durationLogger) Slowf(format string, v ...interface{}) { 65 if shallLog(ErrorLevel) { 66 l.write(slowLog, levelSlow, fmt.Sprintf(format, v...)) 67 } 68 } 69 70 func (l *durationLogger) Slowv(v interface{}) { 71 if shallLog(ErrorLevel) { 72 l.write(slowLog, levelSlow, v) 73 } 74 } 75 76 func (l *durationLogger) WithDuration(duration time.Duration) Logger { 77 l.Duration = timex.ReprOfDuration(duration) 78 return l 79 } 80 81 func (l *durationLogger) write(writer io.Writer, level string, val interface{}) { 82 switch encoding { 83 case plainEncodingType: 84 writePlainAny(writer, level, val, l.Duration) 85 default: 86 outputJson(writer, &durationLogger{ 87 Timestamp: getTimestamp(), 88 Level: level, 89 Content: val, 90 Duration: l.Duration, 91 }) 92 } 93 }