add logx.DisableStat() to disable stat logs (#893)

* add logx.DisableStat() to disable stat logs

* refactor logx code
master
Kevin Wan 3 years ago committed by GitHub
parent af1730079e
commit 872e75e10d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -20,37 +20,37 @@ func WithDuration(d time.Duration) Logger {
} }
func (l *durationLogger) Error(v ...interface{}) { func (l *durationLogger) Error(v ...interface{}) {
if shouldLog(ErrorLevel) { if shallLog(ErrorLevel) {
l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth)) l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth))
} }
} }
func (l *durationLogger) Errorf(format string, v ...interface{}) { func (l *durationLogger) Errorf(format string, v ...interface{}) {
if shouldLog(ErrorLevel) { if shallLog(ErrorLevel) {
l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth)) l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth))
} }
} }
func (l *durationLogger) Info(v ...interface{}) { func (l *durationLogger) Info(v ...interface{}) {
if shouldLog(InfoLevel) { if shallLog(InfoLevel) {
l.write(infoLog, levelInfo, fmt.Sprint(v...)) l.write(infoLog, levelInfo, fmt.Sprint(v...))
} }
} }
func (l *durationLogger) Infof(format string, v ...interface{}) { func (l *durationLogger) Infof(format string, v ...interface{}) {
if shouldLog(InfoLevel) { if shallLog(InfoLevel) {
l.write(infoLog, levelInfo, fmt.Sprintf(format, v...)) l.write(infoLog, levelInfo, fmt.Sprintf(format, v...))
} }
} }
func (l *durationLogger) Slow(v ...interface{}) { func (l *durationLogger) Slow(v ...interface{}) {
if shouldLog(ErrorLevel) { if shallLog(ErrorLevel) {
l.write(slowLog, levelSlow, fmt.Sprint(v...)) l.write(slowLog, levelSlow, fmt.Sprint(v...))
} }
} }
func (l *durationLogger) Slowf(format string, v ...interface{}) { func (l *durationLogger) Slowf(format string, v ...interface{}) {
if shouldLog(ErrorLevel) { if shallLog(ErrorLevel) {
l.write(slowLog, levelSlow, fmt.Sprintf(format, v...)) l.write(slowLog, levelSlow, fmt.Sprintf(format, v...))
} }
} }

@ -65,6 +65,8 @@ var (
timeFormat = "2006-01-02T15:04:05.000Z07" timeFormat = "2006-01-02T15:04:05.000Z07"
writeConsole bool writeConsole bool
logLevel uint32 logLevel uint32
// use uint32 for atomic operations
disableStat uint32
infoLog io.WriteCloser infoLog io.WriteCloser
errorLog io.WriteCloser errorLog io.WriteCloser
severeLog io.WriteCloser severeLog io.WriteCloser
@ -195,6 +197,10 @@ func Disable() {
}) })
} }
func DisableStat() {
atomic.StoreUint32(&disableStat, 1)
}
// Error writes v into error log. // Error writes v into error log.
func Error(v ...interface{}) { func Error(v ...interface{}) {
ErrorCaller(1, v...) ErrorCaller(1, v...)
@ -313,7 +319,7 @@ func createOutput(path string) (io.WriteCloser, error) {
} }
func errorSync(msg string, callDepth int) { func errorSync(msg string, callDepth int) {
if shouldLog(ErrorLevel) { if shallLog(ErrorLevel) {
outputError(errorLog, msg, callDepth) outputError(errorLog, msg, callDepth)
} }
} }
@ -363,7 +369,7 @@ func handleOptions(opts []LogOption) {
} }
func infoSync(msg string) { func infoSync(msg string) {
if shouldLog(InfoLevel) { if shallLog(InfoLevel) {
output(infoLog, levelInfo, msg) output(infoLog, levelInfo, msg)
} }
} }
@ -481,29 +487,33 @@ func setupWithVolume(c LogConf) error {
} }
func severeSync(msg string) { func severeSync(msg string) {
if shouldLog(SevereLevel) { if shallLog(SevereLevel) {
output(severeLog, levelSevere, fmt.Sprintf("%s\n%s", msg, string(debug.Stack()))) 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 return atomic.LoadUint32(&logLevel) <= level
} }
func shallLogStat() bool {
return atomic.LoadUint32(&disableStat) == 0
}
func slowSync(msg string) { func slowSync(msg string) {
if shouldLog(ErrorLevel) { if shallLog(ErrorLevel) {
output(slowLog, levelSlow, msg) output(slowLog, levelSlow, msg)
} }
} }
func stackSync(msg string) { func stackSync(msg string) {
if shouldLog(ErrorLevel) { if shallLog(ErrorLevel) {
output(stackLog, levelError, fmt.Sprintf("%s\n%s", msg, string(debug.Stack()))) output(stackLog, levelError, fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
} }
} }
func statSync(msg string) { func statSync(msg string) {
if shouldLog(InfoLevel) { if shallLogStat() && shallLog(InfoLevel) {
output(statLog, levelStat, msg) output(statLog, levelStat, msg)
} }
} }

@ -246,6 +246,17 @@ func TestDisable(t *testing.T) {
assert.Nil(t, Close()) 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) { func TestWithGzip(t *testing.T) {
fn := WithGzip() fn := WithGzip()
var opt logOptions var opt logOptions

@ -18,37 +18,37 @@ type traceLogger struct {
} }
func (l *traceLogger) Error(v ...interface{}) { func (l *traceLogger) Error(v ...interface{}) {
if shouldLog(ErrorLevel) { if shallLog(ErrorLevel) {
l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth)) l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth))
} }
} }
func (l *traceLogger) Errorf(format string, v ...interface{}) { func (l *traceLogger) Errorf(format string, v ...interface{}) {
if shouldLog(ErrorLevel) { if shallLog(ErrorLevel) {
l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth)) l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth))
} }
} }
func (l *traceLogger) Info(v ...interface{}) { func (l *traceLogger) Info(v ...interface{}) {
if shouldLog(InfoLevel) { if shallLog(InfoLevel) {
l.write(infoLog, levelInfo, fmt.Sprint(v...)) l.write(infoLog, levelInfo, fmt.Sprint(v...))
} }
} }
func (l *traceLogger) Infof(format string, v ...interface{}) { func (l *traceLogger) Infof(format string, v ...interface{}) {
if shouldLog(InfoLevel) { if shallLog(InfoLevel) {
l.write(infoLog, levelInfo, fmt.Sprintf(format, v...)) l.write(infoLog, levelInfo, fmt.Sprintf(format, v...))
} }
} }
func (l *traceLogger) Slow(v ...interface{}) { func (l *traceLogger) Slow(v ...interface{}) {
if shouldLog(ErrorLevel) { if shallLog(ErrorLevel) {
l.write(slowLog, levelSlow, fmt.Sprint(v...)) l.write(slowLog, levelSlow, fmt.Sprint(v...))
} }
} }
func (l *traceLogger) Slowf(format string, v ...interface{}) { func (l *traceLogger) Slowf(format string, v ...interface{}) {
if shouldLog(ErrorLevel) { if shallLog(ErrorLevel) {
l.write(slowLog, levelSlow, fmt.Sprintf(format, v...)) l.write(slowLog, levelSlow, fmt.Sprintf(format, v...))
} }
} }

Loading…
Cancel
Save