go-zero/core/logx/tracelogger.go

129 lines
2.7 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package logx
import (
"context"
"fmt"
"io"
"time"
2020-07-26 17:09:05 +08:00
"github.com/tal-tech/go-zero/core/timex"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/core/trace/tracespec"
2021-09-06 14:56:46 +08:00
"go.opentelemetry.io/otel/trace"
2020-07-26 17:09:05 +08:00
)
type traceLogger struct {
2020-07-26 17:09:05 +08:00
logEntry
2020-09-21 22:41:14 +08:00
Trace string `json:"trace,omitempty"`
Span string `json:"span,omitempty"`
ctx context.Context
2020-07-26 17:09:05 +08:00
}
func (l *traceLogger) Error(v ...interface{}) {
if shallLog(ErrorLevel) {
l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth))
2020-07-26 17:09:05 +08:00
}
}
func (l *traceLogger) Errorf(format string, v ...interface{}) {
if shallLog(ErrorLevel) {
l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth))
2020-07-26 17:09:05 +08:00
}
}
2021-08-13 18:28:39 +08:00
func (l *traceLogger) Errorv(v interface{}) {
if shallLog(ErrorLevel) {
l.write(errorLog, levelError, v)
}
}
func (l *traceLogger) Info(v ...interface{}) {
if shallLog(InfoLevel) {
2020-07-26 17:09:05 +08:00
l.write(infoLog, levelInfo, fmt.Sprint(v...))
}
}
func (l *traceLogger) Infof(format string, v ...interface{}) {
if shallLog(InfoLevel) {
2020-07-26 17:09:05 +08:00
l.write(infoLog, levelInfo, fmt.Sprintf(format, v...))
}
}
2021-08-13 18:28:39 +08:00
func (l *traceLogger) Infov(v interface{}) {
if shallLog(InfoLevel) {
l.write(infoLog, levelInfo, v)
}
}
func (l *traceLogger) Slow(v ...interface{}) {
if shallLog(ErrorLevel) {
2020-07-26 17:09:05 +08:00
l.write(slowLog, levelSlow, fmt.Sprint(v...))
}
}
func (l *traceLogger) Slowf(format string, v ...interface{}) {
if shallLog(ErrorLevel) {
2020-07-26 17:09:05 +08:00
l.write(slowLog, levelSlow, fmt.Sprintf(format, v...))
}
}
2021-08-13 18:28:39 +08:00
func (l *traceLogger) Slowv(v interface{}) {
if shallLog(ErrorLevel) {
l.write(slowLog, levelSlow, v)
}
}
func (l *traceLogger) WithDuration(duration time.Duration) Logger {
l.Duration = timex.ReprOfDuration(duration)
return l
}
2021-08-13 18:28:39 +08:00
func (l *traceLogger) write(writer io.Writer, level string, val interface{}) {
2020-07-26 17:09:05 +08:00
l.Timestamp = getTimestamp()
l.Level = level
2021-08-13 18:28:39 +08:00
l.Content = val
2020-07-26 17:09:05 +08:00
l.Trace = traceIdFromContext(l.ctx)
l.Span = spanIdFromContext(l.ctx)
outputJson(writer, l)
}
2021-02-23 13:53:19 +08:00
// WithContext sets ctx to log, for keeping tracing information.
2020-07-26 17:09:05 +08:00
func WithContext(ctx context.Context) Logger {
return &traceLogger{
2020-07-26 17:09:05 +08:00
ctx: ctx,
}
}
func spanIdFromContext(ctx context.Context) string {
span := trace.SpanFromContext(ctx)
if span.IsRecording() {
spanCtx := span.SpanContext()
if spanCtx.IsValid() {
return spanCtx.SpanID().String()
}
}
2020-07-26 17:09:05 +08:00
t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace)
if !ok {
return ""
}
return t.SpanId()
}
func traceIdFromContext(ctx context.Context) string {
span := trace.SpanFromContext(ctx)
if span.IsRecording() {
spanCtx := span.SpanContext()
if spanCtx.IsValid() {
return span.SpanContext().TraceID().String()
}
}
2020-07-26 17:09:05 +08:00
t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace)
if !ok {
return ""
}
return t.TraceId()
}