go-zero/core/logx/contextlogger.go

146 lines
2.9 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package logx
import (
"context"
"fmt"
"time"
2020-07-26 17:09:05 +08:00
"github.com/zeromicro/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
)
// WithContext sets ctx to log, for keeping tracing information.
func WithContext(ctx context.Context) Logger {
2022-07-11 23:19:26 +08:00
return &contextLogger{
ctx: ctx,
}
}
2022-07-11 23:19:26 +08:00
type contextLogger struct {
2020-07-26 17:09:05 +08:00
logEntry
ctx context.Context
2020-07-26 17:09:05 +08:00
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) Error(v ...interface{}) {
l.err(fmt.Sprint(v...))
2020-07-26 17:09:05 +08:00
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) Errorf(format string, v ...interface{}) {
l.err(fmt.Sprintf(format, v...))
2020-07-26 17:09:05 +08:00
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) Errorv(v interface{}) {
l.err(fmt.Sprint(v))
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) Errorw(msg string, fields ...LogField) {
l.err(msg, fields...)
2021-08-13 18:28:39 +08:00
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) Info(v ...interface{}) {
l.info(fmt.Sprint(v...))
2020-07-26 17:09:05 +08:00
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) Infof(format string, v ...interface{}) {
l.info(fmt.Sprintf(format, v...))
2020-07-26 17:09:05 +08:00
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) Infov(v interface{}) {
l.info(v)
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) Infow(msg string, fields ...LogField) {
l.info(msg, fields...)
2021-08-13 18:28:39 +08:00
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) Slow(v ...interface{}) {
l.slow(fmt.Sprint(v...))
2020-07-26 17:09:05 +08:00
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) Slowf(format string, v ...interface{}) {
l.slow(fmt.Sprintf(format, v...))
2020-07-26 17:09:05 +08:00
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) Slowv(v interface{}) {
l.slow(v)
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) Sloww(msg string, fields ...LogField) {
l.slow(msg, fields...)
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) WithContext(ctx context.Context) Logger {
if ctx == nil {
return l
2021-08-13 18:28:39 +08:00
}
l.ctx = ctx
return l
2021-08-13 18:28:39 +08:00
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) WithDuration(duration time.Duration) Logger {
l.Duration = timex.ReprOfDuration(duration)
return l
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) buildFields(fields ...LogField) []LogField {
if len(l.Duration) > 0 {
fields = append(fields, Field(durationKey, l.Duration))
}
2022-07-11 23:19:26 +08:00
traceID := traceIdFromContext(l.ctx)
if len(traceID) > 0 {
fields = append(fields, Field(traceKey, traceID))
}
2022-07-11 23:19:26 +08:00
spanID := spanIdFromContext(l.ctx)
if len(spanID) > 0 {
fields = append(fields, Field(spanKey, spanID))
}
2022-07-11 23:19:26 +08:00
val := l.ctx.Value(fieldsContextKey)
if val != nil {
if arr, ok := val.([]LogField); ok {
fields = append(fields, arr...)
}
}
return fields
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) err(v interface{}, fields ...LogField) {
if shallLog(ErrorLevel) {
getWriter().Error(v, l.buildFields(fields...)...)
}
2020-07-26 17:09:05 +08:00
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) info(v interface{}, fields ...LogField) {
if shallLog(InfoLevel) {
getWriter().Info(v, l.buildFields(fields...)...)
}
}
2022-07-11 23:19:26 +08:00
func (l *contextLogger) slow(v interface{}, fields ...LogField) {
if shallLog(ErrorLevel) {
getWriter().Slow(v, l.buildFields(fields...)...)
2020-07-26 17:09:05 +08:00
}
}
func spanIdFromContext(ctx context.Context) string {
spanCtx := trace.SpanContextFromContext(ctx)
if spanCtx.HasSpanID() {
return spanCtx.SpanID().String()
}
return ""
2020-07-26 17:09:05 +08:00
}
func traceIdFromContext(ctx context.Context) string {
spanCtx := trace.SpanContextFromContext(ctx)
if spanCtx.HasTraceID() {
return spanCtx.TraceID().String()
2020-07-26 17:09:05 +08:00
}
return ""
2020-07-26 17:09:05 +08:00
}