|
@@ -4,54 +4,61 @@ import (
|
|
"context"
|
|
"context"
|
|
"fmt"
|
|
"fmt"
|
|
"io"
|
|
"io"
|
|
|
|
+ "time"
|
|
|
|
|
|
|
|
+ "github.com/tal-tech/go-zero/core/timex"
|
|
"github.com/tal-tech/go-zero/core/trace/tracespec"
|
|
"github.com/tal-tech/go-zero/core/trace/tracespec"
|
|
)
|
|
)
|
|
|
|
|
|
-type tracingEntry struct {
|
|
|
|
|
|
+type traceLogger struct {
|
|
logEntry
|
|
logEntry
|
|
Trace string `json:"trace,omitempty"`
|
|
Trace string `json:"trace,omitempty"`
|
|
Span string `json:"span,omitempty"`
|
|
Span string `json:"span,omitempty"`
|
|
ctx context.Context
|
|
ctx context.Context
|
|
}
|
|
}
|
|
|
|
|
|
-func (l tracingEntry) Error(v ...interface{}) {
|
|
|
|
|
|
+func (l *traceLogger) Error(v ...interface{}) {
|
|
if shouldLog(ErrorLevel) {
|
|
if shouldLog(ErrorLevel) {
|
|
- l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), customCallerDepth))
|
|
|
|
|
|
+ l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (l tracingEntry) Errorf(format string, v ...interface{}) {
|
|
|
|
|
|
+func (l *traceLogger) Errorf(format string, v ...interface{}) {
|
|
if shouldLog(ErrorLevel) {
|
|
if shouldLog(ErrorLevel) {
|
|
- l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), customCallerDepth))
|
|
|
|
|
|
+ l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (l tracingEntry) Info(v ...interface{}) {
|
|
|
|
|
|
+func (l *traceLogger) Info(v ...interface{}) {
|
|
if shouldLog(InfoLevel) {
|
|
if shouldLog(InfoLevel) {
|
|
l.write(infoLog, levelInfo, fmt.Sprint(v...))
|
|
l.write(infoLog, levelInfo, fmt.Sprint(v...))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (l tracingEntry) Infof(format string, v ...interface{}) {
|
|
|
|
|
|
+func (l *traceLogger) Infof(format string, v ...interface{}) {
|
|
if shouldLog(InfoLevel) {
|
|
if shouldLog(InfoLevel) {
|
|
l.write(infoLog, levelInfo, fmt.Sprintf(format, v...))
|
|
l.write(infoLog, levelInfo, fmt.Sprintf(format, v...))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (l tracingEntry) Slow(v ...interface{}) {
|
|
|
|
|
|
+func (l *traceLogger) Slow(v ...interface{}) {
|
|
if shouldLog(ErrorLevel) {
|
|
if shouldLog(ErrorLevel) {
|
|
l.write(slowLog, levelSlow, fmt.Sprint(v...))
|
|
l.write(slowLog, levelSlow, fmt.Sprint(v...))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (l tracingEntry) Slowf(format string, v ...interface{}) {
|
|
|
|
|
|
+func (l *traceLogger) Slowf(format string, v ...interface{}) {
|
|
if shouldLog(ErrorLevel) {
|
|
if shouldLog(ErrorLevel) {
|
|
l.write(slowLog, levelSlow, fmt.Sprintf(format, v...))
|
|
l.write(slowLog, levelSlow, fmt.Sprintf(format, v...))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (l tracingEntry) write(writer io.Writer, level, content string) {
|
|
|
|
|
|
+func (l *traceLogger) WithDuration(duration time.Duration) Logger {
|
|
|
|
+ l.Duration = timex.ReprOfDuration(duration)
|
|
|
|
+ return l
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (l *traceLogger) write(writer io.Writer, level, content string) {
|
|
l.Timestamp = getTimestamp()
|
|
l.Timestamp = getTimestamp()
|
|
l.Level = level
|
|
l.Level = level
|
|
l.Content = content
|
|
l.Content = content
|
|
@@ -61,7 +68,7 @@ func (l tracingEntry) write(writer io.Writer, level, content string) {
|
|
}
|
|
}
|
|
|
|
|
|
func WithContext(ctx context.Context) Logger {
|
|
func WithContext(ctx context.Context) Logger {
|
|
- return tracingEntry{
|
|
|
|
|
|
+ return &traceLogger{
|
|
ctx: ctx,
|
|
ctx: ctx,
|
|
}
|
|
}
|
|
}
|
|
}
|