go-zero/core/logx/durationlogger.go

102 lines
2.1 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package logx
import (
"context"
2020-07-26 17:09:05 +08:00
"fmt"
"time"
"github.com/zeromicro/go-zero/core/timex"
2020-07-26 17:09:05 +08:00
)
2021-02-20 22:45:58 +08:00
// WithDuration returns a Logger which logs the given duration.
2020-07-26 17:09:05 +08:00
func WithDuration(d time.Duration) Logger {
return &durationLogger{
2020-07-26 17:09:05 +08:00
Duration: timex.ReprOfDuration(d),
}
}
type durationLogger logEntry
func (l *durationLogger) Error(v ...interface{}) {
l.err(fmt.Sprint(v...))
2020-07-26 17:09:05 +08:00
}
func (l *durationLogger) Errorf(format string, v ...interface{}) {
l.err(fmt.Sprintf(format, v...))
2020-07-26 17:09:05 +08:00
}
2021-08-13 18:28:39 +08:00
func (l *durationLogger) Errorv(v interface{}) {
l.err(v)
}
func (l *durationLogger) Errorw(msg string, fields ...LogField) {
l.err(msg, fields...)
2021-08-13 18:28:39 +08:00
}
func (l *durationLogger) Info(v ...interface{}) {
l.info(fmt.Sprint(v...))
2020-07-26 17:09:05 +08:00
}
func (l *durationLogger) Infof(format string, v ...interface{}) {
l.info(fmt.Sprintf(format, v...))
2020-07-26 17:09:05 +08:00
}
2021-08-13 18:28:39 +08:00
func (l *durationLogger) Infov(v interface{}) {
l.info(v)
}
func (l *durationLogger) Infow(msg string, fields ...LogField) {
l.info(msg, fields...)
2021-08-13 18:28:39 +08:00
}
func (l *durationLogger) Slow(v ...interface{}) {
l.slow(fmt.Sprint(v...))
2020-07-26 17:09:05 +08:00
}
func (l *durationLogger) Slowf(format string, v ...interface{}) {
l.slow(fmt.Sprintf(format, v...))
2020-07-26 17:09:05 +08:00
}
2021-08-13 18:28:39 +08:00
func (l *durationLogger) Slowv(v interface{}) {
l.slow(v)
}
func (l *durationLogger) Sloww(msg string, fields ...LogField) {
l.slow(msg, fields...)
}
func (l *durationLogger) WithContext(ctx context.Context) Logger {
2022-07-11 23:19:26 +08:00
return &contextLogger{
ctx: ctx,
logEntry: logEntry{
Duration: l.Duration,
},
2021-08-13 18:28:39 +08:00
}
}
func (l *durationLogger) WithDuration(duration time.Duration) Logger {
l.Duration = timex.ReprOfDuration(duration)
return l
}
func (l *durationLogger) err(v interface{}, fields ...LogField) {
if shallLog(ErrorLevel) {
fields = append(fields, Field(durationKey, l.Duration))
getWriter().Error(v, fields...)
}
}
func (l *durationLogger) info(v interface{}, fields ...LogField) {
if shallLog(InfoLevel) {
fields = append(fields, Field(durationKey, l.Duration))
getWriter().Info(v, fields...)
}
}
func (l *durationLogger) slow(v interface{}, fields ...LogField) {
if shallLog(ErrorLevel) {
fields = append(fields, Field(durationKey, l.Duration))
getWriter().Slow(v, fields...)
}
2020-07-26 17:09:05 +08:00
}