12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package logx
- import (
- "fmt"
- "io"
- "time"
- "github.com/tal-tech/go-zero/core/timex"
- )
- const durationCallerDepth = 3
- type durationLogger logEntry
- func WithDuration(d time.Duration) Logger {
- return &durationLogger{
- Duration: timex.ReprOfDuration(d),
- }
- }
- func (l *durationLogger) Error(v ...interface{}) {
- if shouldLog(ErrorLevel) {
- l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth))
- }
- }
- func (l *durationLogger) Errorf(format string, v ...interface{}) {
- if shouldLog(ErrorLevel) {
- l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth))
- }
- }
- func (l *durationLogger) Info(v ...interface{}) {
- if shouldLog(InfoLevel) {
- l.write(infoLog, levelInfo, fmt.Sprint(v...))
- }
- }
- func (l *durationLogger) Infof(format string, v ...interface{}) {
- if shouldLog(InfoLevel) {
- l.write(infoLog, levelInfo, fmt.Sprintf(format, v...))
- }
- }
- func (l *durationLogger) Slow(v ...interface{}) {
- if shouldLog(ErrorLevel) {
- l.write(slowLog, levelSlow, fmt.Sprint(v...))
- }
- }
- func (l *durationLogger) Slowf(format string, v ...interface{}) {
- 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) {
- l.Timestamp = getTimestamp()
- l.Level = level
- l.Content = content
- outputJson(writer, logEntry(*l))
- }
|