2020-07-26 17:09:05 +08:00
|
|
|
package logx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-09-22 17:34:39 +08:00
|
|
|
"time"
|
2020-07-26 17:09:05 +08:00
|
|
|
|
2020-09-22 17:34:39 +08:00
|
|
|
"github.com/tal-tech/go-zero/core/timex"
|
2021-09-06 14:56:46 +08:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-07-26 17:09:05 +08:00
|
|
|
)
|
|
|
|
|
2020-09-22 17:34:39 +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
|
|
|
}
|
|
|
|
|
2020-09-22 17:34:39 +08:00
|
|
|
func (l *traceLogger) Error(v ...interface{}) {
|
2021-08-10 16:55:38 +08:00
|
|
|
if shallLog(ErrorLevel) {
|
2020-09-22 17:34:39 +08:00
|
|
|
l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth))
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 17:34:39 +08:00
|
|
|
func (l *traceLogger) Errorf(format string, v ...interface{}) {
|
2021-08-10 16:55:38 +08:00
|
|
|
if shallLog(ErrorLevel) {
|
2020-09-22 17:34:39 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 17:34:39 +08:00
|
|
|
func (l *traceLogger) Info(v ...interface{}) {
|
2021-08-10 16:55:38 +08:00
|
|
|
if shallLog(InfoLevel) {
|
2020-07-26 17:09:05 +08:00
|
|
|
l.write(infoLog, levelInfo, fmt.Sprint(v...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 17:34:39 +08:00
|
|
|
func (l *traceLogger) Infof(format string, v ...interface{}) {
|
2021-08-10 16:55:38 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 17:34:39 +08:00
|
|
|
func (l *traceLogger) Slow(v ...interface{}) {
|
2021-08-10 16:55:38 +08:00
|
|
|
if shallLog(ErrorLevel) {
|
2020-07-26 17:09:05 +08:00
|
|
|
l.write(slowLog, levelSlow, fmt.Sprint(v...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 17:34:39 +08:00
|
|
|
func (l *traceLogger) Slowf(format string, v ...interface{}) {
|
2021-08-10 16:55:38 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 17:34:39 +08:00
|
|
|
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 {
|
2020-09-22 17:34:39 +08:00
|
|
|
return &traceLogger{
|
2020-07-26 17:09:05 +08:00
|
|
|
ctx: ctx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func spanIdFromContext(ctx context.Context) string {
|
2021-10-03 20:53:50 +08:00
|
|
|
spanCtx := trace.SpanContextFromContext(ctx)
|
|
|
|
if spanCtx.HasSpanID() {
|
|
|
|
return spanCtx.SpanID().String()
|
2021-09-06 14:31:54 +08:00
|
|
|
}
|
|
|
|
|
2021-10-03 20:53:50 +08:00
|
|
|
return ""
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func traceIdFromContext(ctx context.Context) string {
|
2021-10-03 20:53:50 +08:00
|
|
|
spanCtx := trace.SpanContextFromContext(ctx)
|
|
|
|
if spanCtx.HasTraceID() {
|
|
|
|
return spanCtx.TraceID().String()
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
|
2021-10-03 20:53:50 +08:00
|
|
|
return ""
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|