You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
2.9 KiB
Go
146 lines
2.9 KiB
Go
4 years ago
|
package logx
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
4 years ago
|
"time"
|
||
4 years ago
|
|
||
3 years ago
|
"github.com/zeromicro/go-zero/core/timex"
|
||
3 years ago
|
"go.opentelemetry.io/otel/trace"
|
||
4 years ago
|
)
|
||
|
|
||
3 years ago
|
// WithContext sets ctx to log, for keeping tracing information.
|
||
|
func WithContext(ctx context.Context) Logger {
|
||
2 years ago
|
return &contextLogger{
|
||
3 years ago
|
ctx: ctx,
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
type contextLogger struct {
|
||
4 years ago
|
logEntry
|
||
3 years ago
|
ctx context.Context
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) Error(v ...interface{}) {
|
||
3 years ago
|
l.err(fmt.Sprint(v...))
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) Errorf(format string, v ...interface{}) {
|
||
3 years ago
|
l.err(fmt.Sprintf(format, v...))
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) Errorv(v interface{}) {
|
||
3 years ago
|
l.err(fmt.Sprint(v))
|
||
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) Errorw(msg string, fields ...LogField) {
|
||
3 years ago
|
l.err(msg, fields...)
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) Info(v ...interface{}) {
|
||
3 years ago
|
l.info(fmt.Sprint(v...))
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) Infof(format string, v ...interface{}) {
|
||
3 years ago
|
l.info(fmt.Sprintf(format, v...))
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) Infov(v interface{}) {
|
||
3 years ago
|
l.info(v)
|
||
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) Infow(msg string, fields ...LogField) {
|
||
3 years ago
|
l.info(msg, fields...)
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) Slow(v ...interface{}) {
|
||
3 years ago
|
l.slow(fmt.Sprint(v...))
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) Slowf(format string, v ...interface{}) {
|
||
3 years ago
|
l.slow(fmt.Sprintf(format, v...))
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) Slowv(v interface{}) {
|
||
3 years ago
|
l.slow(v)
|
||
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) Sloww(msg string, fields ...LogField) {
|
||
3 years ago
|
l.slow(msg, fields...)
|
||
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) WithContext(ctx context.Context) Logger {
|
||
3 years ago
|
if ctx == nil {
|
||
|
return l
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
|
l.ctx = ctx
|
||
|
return l
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) WithDuration(duration time.Duration) Logger {
|
||
4 years ago
|
l.Duration = timex.ReprOfDuration(duration)
|
||
|
return l
|
||
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) buildFields(fields ...LogField) []LogField {
|
||
3 years ago
|
if len(l.Duration) > 0 {
|
||
|
fields = append(fields, Field(durationKey, l.Duration))
|
||
|
}
|
||
2 years ago
|
|
||
3 years ago
|
traceID := traceIdFromContext(l.ctx)
|
||
3 years ago
|
if len(traceID) > 0 {
|
||
|
fields = append(fields, Field(traceKey, traceID))
|
||
|
}
|
||
2 years ago
|
|
||
3 years ago
|
spanID := spanIdFromContext(l.ctx)
|
||
3 years ago
|
if len(spanID) > 0 {
|
||
|
fields = append(fields, Field(spanKey, spanID))
|
||
|
}
|
||
3 years ago
|
|
||
2 years ago
|
val := l.ctx.Value(fieldsContextKey)
|
||
|
if val != nil {
|
||
|
if arr, ok := val.([]LogField); ok {
|
||
|
fields = append(fields, arr...)
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
return fields
|
||
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) err(v interface{}, fields ...LogField) {
|
||
3 years ago
|
if shallLog(ErrorLevel) {
|
||
|
getWriter().Error(v, l.buildFields(fields...)...)
|
||
3 years ago
|
}
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) info(v interface{}, fields ...LogField) {
|
||
3 years ago
|
if shallLog(InfoLevel) {
|
||
|
getWriter().Info(v, l.buildFields(fields...)...)
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
func (l *contextLogger) slow(v interface{}, fields ...LogField) {
|
||
3 years ago
|
if shallLog(ErrorLevel) {
|
||
|
getWriter().Slow(v, l.buildFields(fields...)...)
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
func spanIdFromContext(ctx context.Context) string {
|
||
3 years ago
|
spanCtx := trace.SpanContextFromContext(ctx)
|
||
|
if spanCtx.HasSpanID() {
|
||
|
return spanCtx.SpanID().String()
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
return ""
|
||
4 years ago
|
}
|
||
|
|
||
|
func traceIdFromContext(ctx context.Context) string {
|
||
3 years ago
|
spanCtx := trace.SpanContextFromContext(ctx)
|
||
|
if spanCtx.HasTraceID() {
|
||
|
return spanCtx.TraceID().String()
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
return ""
|
||
4 years ago
|
}
|