Skip to content

Commit 20f56ae

Browse files
authored
feat: support customize of log keys (#5103)
1 parent 73d6fcf commit 20f56ae

File tree

4 files changed

+133
-8
lines changed

4 files changed

+133
-8
lines changed

core/logx/config.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,25 @@ type LogConf struct {
4444
Rotation string `json:",default=daily,options=[daily,size]"`
4545
// FileTimeFormat represents the time format for file name, default is `2006-01-02T15:04:05.000Z07:00`.
4646
FileTimeFormat string `json:",optional"`
47+
// LogKey represents the log key.
48+
LogKey logKeyConf `json:",optional"`
49+
}
50+
51+
type logKeyConf struct {
52+
// CallerKey represents the caller key.
53+
CallerKey string `json:",default=caller"`
54+
// ContentKey represents the content key.
55+
ContentKey string `json:",default=content"`
56+
// DurationKey represents the duration key.
57+
DurationKey string `json:",default=duration"`
58+
// LevelKey represents the level key.
59+
LevelKey string `json:",default=level"`
60+
// SpanKey represents the span key.
61+
SpanKey string `json:",default=span"`
62+
// TimestampKey represents the timestamp key.
63+
TimestampKey string `json:",default=@timestamp"`
64+
// TraceKey represents the trace key.
65+
TraceKey string `json:",default=trace"`
66+
// TruncatedKey represents the truncated key.
67+
TruncatedKey string `json:",default=truncated"`
4768
}

core/logx/logs.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ func SetUp(c LogConf) (err error) {
277277
// Need to wait for the first caller to complete the execution.
278278
setupOnce.Do(func() {
279279
setupLogLevel(c)
280+
setupLogKey(c.LogKey)
280281

281282
if !c.Stat {
282283
DisableStat()
@@ -579,3 +580,30 @@ func writeStack(msg string) {
579580
func writeStat(msg string) {
580581
getWriter().Stat(msg, mergeGlobalFields(addCaller())...)
581582
}
583+
584+
func setupLogKey(c logKeyConf) {
585+
if c.CallerKey != "" {
586+
callerKey = c.CallerKey
587+
}
588+
if c.ContentKey != "" {
589+
contentKey = c.ContentKey
590+
}
591+
if c.DurationKey != "" {
592+
durationKey = c.DurationKey
593+
}
594+
if c.LevelKey != "" {
595+
levelKey = c.LevelKey
596+
}
597+
if c.SpanKey != "" {
598+
spanKey = c.SpanKey
599+
}
600+
if c.TimestampKey != "" {
601+
timestampKey = c.TimestampKey
602+
}
603+
if c.TraceKey != "" {
604+
traceKey = c.TraceKey
605+
}
606+
if c.TruncatedKey != "" {
607+
truncatedKey = c.TruncatedKey
608+
}
609+
}

core/logx/logs_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"time"
1818

1919
"github.com/stretchr/testify/assert"
20+
"go.opentelemetry.io/otel"
21+
sdktrace "go.opentelemetry.io/otel/sdk/trace"
2022
)
2123

2224
var (
@@ -1157,3 +1159,66 @@ func (s *countingStringer) String() string {
11571159
atomic.AddInt32(&s.count, 1)
11581160
return "countingStringer"
11591161
}
1162+
1163+
func TestLogKey(t *testing.T) {
1164+
setupOnce = sync.Once{}
1165+
MustSetup(LogConf{
1166+
ServiceName: "any",
1167+
Mode: "console",
1168+
Encoding: "json",
1169+
TimeFormat: timeFormat,
1170+
LogKey: logKeyConf{
1171+
CallerKey: "_caller",
1172+
ContentKey: "_content",
1173+
DurationKey: "_duration",
1174+
LevelKey: "_level",
1175+
SpanKey: "_span",
1176+
TimestampKey: "_timestamp",
1177+
TraceKey: "_trace",
1178+
TruncatedKey: "_truncated",
1179+
},
1180+
})
1181+
1182+
t.Cleanup(func() {
1183+
setupLogKey(logKeyConf{
1184+
CallerKey: defaultCallerKey,
1185+
ContentKey: defaultContentKey,
1186+
DurationKey: defaultDurationKey,
1187+
LevelKey: defaultLevelKey,
1188+
SpanKey: defaultSpanKey,
1189+
TimestampKey: defaultTimestampKey,
1190+
TraceKey: defaultTraceKey,
1191+
TruncatedKey: defaultTruncatedKey,
1192+
})
1193+
})
1194+
1195+
const message = "hello there"
1196+
w := new(mockWriter)
1197+
old := writer.Swap(w)
1198+
defer writer.Store(old)
1199+
1200+
otp := otel.GetTracerProvider()
1201+
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
1202+
otel.SetTracerProvider(tp)
1203+
defer otel.SetTracerProvider(otp)
1204+
1205+
ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
1206+
defer span.End()
1207+
1208+
WithContext(ctx).WithDuration(time.Second).Info(message)
1209+
now := time.Now()
1210+
1211+
var m map[string]string
1212+
if err := json.Unmarshal([]byte(w.String()), &m); err != nil {
1213+
t.Error(err)
1214+
}
1215+
assert.Equal(t, "info", m["_level"])
1216+
assert.Equal(t, message, m["_content"])
1217+
assert.Equal(t, "1000.0ms", m["_duration"])
1218+
assert.Regexp(t, `logx/logs_test.go:\d+`, m["_caller"])
1219+
assert.NotEmpty(t, m["_trace"])
1220+
assert.NotEmpty(t, m["_span"])
1221+
parsedTime, err := time.Parse(timeFormat, m["_timestamp"])
1222+
assert.True(t, err == nil)
1223+
assert.Equal(t, now.Minute(), parsedTime.Minute())
1224+
}

core/logx/vars.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,25 @@ const (
5353
)
5454

5555
const (
56-
callerKey = "caller"
57-
contentKey = "content"
58-
durationKey = "duration"
59-
levelKey = "level"
60-
spanKey = "span"
61-
timestampKey = "@timestamp"
62-
traceKey = "trace"
63-
truncatedKey = "truncated"
56+
defaultCallerKey = "caller"
57+
defaultContentKey = "content"
58+
defaultDurationKey = "duration"
59+
defaultLevelKey = "level"
60+
defaultSpanKey = "span"
61+
defaultTimestampKey = "@timestamp"
62+
defaultTraceKey = "trace"
63+
defaultTruncatedKey = "truncated"
64+
)
65+
66+
var (
67+
callerKey = defaultCallerKey
68+
contentKey = defaultContentKey
69+
durationKey = defaultDurationKey
70+
levelKey = defaultLevelKey
71+
spanKey = defaultSpanKey
72+
timestampKey = defaultTimestampKey
73+
traceKey = defaultTraceKey
74+
truncatedKey = defaultTruncatedKey
6475
)
6576

6677
var (

0 commit comments

Comments
 (0)