From 872e75e10d1d64fb97d9f2464fc53bb635564098 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Tue, 10 Aug 2021 16:55:38 +0800 Subject: [PATCH] add logx.DisableStat() to disable stat logs (#893) * add logx.DisableStat() to disable stat logs * refactor logx code --- core/logx/durationlogger.go | 12 ++++++------ core/logx/logs.go | 36 +++++++++++++++++++++++------------- core/logx/logs_test.go | 11 +++++++++++ core/logx/tracelogger.go | 12 ++++++------ 4 files changed, 46 insertions(+), 25 deletions(-) diff --git a/core/logx/durationlogger.go b/core/logx/durationlogger.go index d6832076..4979f7e7 100644 --- a/core/logx/durationlogger.go +++ b/core/logx/durationlogger.go @@ -20,37 +20,37 @@ func WithDuration(d time.Duration) Logger { } func (l *durationLogger) Error(v ...interface{}) { - if shouldLog(ErrorLevel) { + if shallLog(ErrorLevel) { l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth)) } } func (l *durationLogger) Errorf(format string, v ...interface{}) { - if shouldLog(ErrorLevel) { + if shallLog(ErrorLevel) { l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth)) } } func (l *durationLogger) Info(v ...interface{}) { - if shouldLog(InfoLevel) { + if shallLog(InfoLevel) { l.write(infoLog, levelInfo, fmt.Sprint(v...)) } } func (l *durationLogger) Infof(format string, v ...interface{}) { - if shouldLog(InfoLevel) { + if shallLog(InfoLevel) { l.write(infoLog, levelInfo, fmt.Sprintf(format, v...)) } } func (l *durationLogger) Slow(v ...interface{}) { - if shouldLog(ErrorLevel) { + if shallLog(ErrorLevel) { l.write(slowLog, levelSlow, fmt.Sprint(v...)) } } func (l *durationLogger) Slowf(format string, v ...interface{}) { - if shouldLog(ErrorLevel) { + if shallLog(ErrorLevel) { l.write(slowLog, levelSlow, fmt.Sprintf(format, v...)) } } diff --git a/core/logx/logs.go b/core/logx/logs.go index 9e5c759d..03025625 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -65,12 +65,14 @@ var ( timeFormat = "2006-01-02T15:04:05.000Z07" writeConsole bool logLevel uint32 - infoLog io.WriteCloser - errorLog io.WriteCloser - severeLog io.WriteCloser - slowLog io.WriteCloser - statLog io.WriteCloser - stackLog io.Writer + // use uint32 for atomic operations + disableStat uint32 + infoLog io.WriteCloser + errorLog io.WriteCloser + severeLog io.WriteCloser + slowLog io.WriteCloser + statLog io.WriteCloser + stackLog io.Writer once sync.Once initialized uint32 @@ -195,6 +197,10 @@ func Disable() { }) } +func DisableStat() { + atomic.StoreUint32(&disableStat, 1) +} + // Error writes v into error log. func Error(v ...interface{}) { ErrorCaller(1, v...) @@ -313,7 +319,7 @@ func createOutput(path string) (io.WriteCloser, error) { } func errorSync(msg string, callDepth int) { - if shouldLog(ErrorLevel) { + if shallLog(ErrorLevel) { outputError(errorLog, msg, callDepth) } } @@ -363,7 +369,7 @@ func handleOptions(opts []LogOption) { } func infoSync(msg string) { - if shouldLog(InfoLevel) { + if shallLog(InfoLevel) { output(infoLog, levelInfo, msg) } } @@ -481,29 +487,33 @@ func setupWithVolume(c LogConf) error { } func severeSync(msg string) { - if shouldLog(SevereLevel) { + if shallLog(SevereLevel) { output(severeLog, levelSevere, fmt.Sprintf("%s\n%s", msg, string(debug.Stack()))) } } -func shouldLog(level uint32) bool { +func shallLog(level uint32) bool { return atomic.LoadUint32(&logLevel) <= level } +func shallLogStat() bool { + return atomic.LoadUint32(&disableStat) == 0 +} + func slowSync(msg string) { - if shouldLog(ErrorLevel) { + if shallLog(ErrorLevel) { output(slowLog, levelSlow, msg) } } func stackSync(msg string) { - if shouldLog(ErrorLevel) { + if shallLog(ErrorLevel) { output(stackLog, levelError, fmt.Sprintf("%s\n%s", msg, string(debug.Stack()))) } } func statSync(msg string) { - if shouldLog(InfoLevel) { + if shallLogStat() && shallLog(InfoLevel) { output(statLog, levelStat, msg) } } diff --git a/core/logx/logs_test.go b/core/logx/logs_test.go index ad2cf04d..5db9ef14 100644 --- a/core/logx/logs_test.go +++ b/core/logx/logs_test.go @@ -246,6 +246,17 @@ func TestDisable(t *testing.T) { assert.Nil(t, Close()) } +func TestDisableStat(t *testing.T) { + DisableStat() + + const message = "hello there" + writer := new(mockWriter) + statLog = writer + atomic.StoreUint32(&initialized, 1) + Stat(message) + assert.Equal(t, 0, writer.builder.Len()) +} + func TestWithGzip(t *testing.T) { fn := WithGzip() var opt logOptions diff --git a/core/logx/tracelogger.go b/core/logx/tracelogger.go index d8826174..af305ee7 100644 --- a/core/logx/tracelogger.go +++ b/core/logx/tracelogger.go @@ -18,37 +18,37 @@ type traceLogger struct { } func (l *traceLogger) Error(v ...interface{}) { - if shouldLog(ErrorLevel) { + if shallLog(ErrorLevel) { l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth)) } } func (l *traceLogger) Errorf(format string, v ...interface{}) { - if shouldLog(ErrorLevel) { + if shallLog(ErrorLevel) { l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth)) } } func (l *traceLogger) Info(v ...interface{}) { - if shouldLog(InfoLevel) { + if shallLog(InfoLevel) { l.write(infoLog, levelInfo, fmt.Sprint(v...)) } } func (l *traceLogger) Infof(format string, v ...interface{}) { - if shouldLog(InfoLevel) { + if shallLog(InfoLevel) { l.write(infoLog, levelInfo, fmt.Sprintf(format, v...)) } } func (l *traceLogger) Slow(v ...interface{}) { - if shouldLog(ErrorLevel) { + if shallLog(ErrorLevel) { l.write(slowLog, levelSlow, fmt.Sprint(v...)) } } func (l *traceLogger) Slowf(format string, v ...interface{}) { - if shouldLog(ErrorLevel) { + if shallLog(ErrorLevel) { l.write(slowLog, levelSlow, fmt.Sprintf(format, v...)) } }