You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/logx/durationlogger.go

69 lines
1.5 KiB
Go

4 years ago
package logx
import (
"fmt"
"io"
"time"
"github.com/tal-tech/go-zero/core/timex"
4 years ago
)
const durationCallerDepth = 3
4 years ago
type durationLogger logEntry
4 years ago
// WithDuration returns a Logger which logs the given duration.
4 years ago
func WithDuration(d time.Duration) Logger {
return &durationLogger{
4 years ago
Duration: timex.ReprOfDuration(d),
}
}
func (l *durationLogger) Error(v ...interface{}) {
4 years ago
if shouldLog(ErrorLevel) {
l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth))
4 years ago
}
}
func (l *durationLogger) Errorf(format string, v ...interface{}) {
4 years ago
if shouldLog(ErrorLevel) {
l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth))
4 years ago
}
}
func (l *durationLogger) Info(v ...interface{}) {
4 years ago
if shouldLog(InfoLevel) {
l.write(infoLog, levelInfo, fmt.Sprint(v...))
}
}
func (l *durationLogger) Infof(format string, v ...interface{}) {
4 years ago
if shouldLog(InfoLevel) {
l.write(infoLog, levelInfo, fmt.Sprintf(format, v...))
}
}
func (l *durationLogger) Slow(v ...interface{}) {
4 years ago
if shouldLog(ErrorLevel) {
l.write(slowLog, levelSlow, fmt.Sprint(v...))
}
}
func (l *durationLogger) Slowf(format string, v ...interface{}) {
4 years ago
if shouldLog(ErrorLevel) {
l.write(slowLog, levelSlow, fmt.Sprintf(format, v...))
}
}
func (l *durationLogger) WithDuration(duration time.Duration) Logger {
l.Duration = timex.ReprOfDuration(duration)
return l
}
func (l *durationLogger) write(writer io.Writer, level, content string) {
4 years ago
l.Timestamp = getTimestamp()
l.Level = level
l.Content = content
outputJson(writer, logEntry(*l))
4 years ago
}