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{}) {
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...))
}
}

@ -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)
}
}

@ -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

@ -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...))
}
}

Loading…
Cancel
Save