Skip to content

Commit 5f54f06

Browse files
authored
chore: refactor field keys in logx (#5104)
1 parent 20f56ae commit 5f54f06

File tree

5 files changed

+114
-119
lines changed

5 files changed

+114
-119
lines changed

core/logx/config.go

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,70 @@
11
package logx
22

3-
// A LogConf is a logging config.
4-
type LogConf struct {
5-
// ServiceName represents the service name.
6-
ServiceName string `json:",optional"`
7-
// Mode represents the logging mode, default is `console`.
8-
// console: log to console.
9-
// file: log to file.
10-
// volume: used in k8s, prepend the hostname to the log file name.
11-
Mode string `json:",default=console,options=[console,file,volume]"`
12-
// Encoding represents the encoding type, default is `json`.
13-
// json: json encoding.
14-
// plain: plain text encoding, typically used in development.
15-
Encoding string `json:",default=json,options=[json,plain]"`
16-
// TimeFormat represents the time format, default is `2006-01-02T15:04:05.000Z07:00`.
17-
TimeFormat string `json:",optional"`
18-
// Path represents the log file path, default is `logs`.
19-
Path string `json:",default=logs"`
20-
// Level represents the log level, default is `info`.
21-
Level string `json:",default=info,options=[debug,info,error,severe]"`
22-
// MaxContentLength represents the max content bytes, default is no limit.
23-
MaxContentLength uint32 `json:",optional"`
24-
// Compress represents whether to compress the log file, default is `false`.
25-
Compress bool `json:",optional"`
26-
// Stat represents whether to log statistics, default is `true`.
27-
Stat bool `json:",default=true"`
28-
// KeepDays represents how many days the log files will be kept. Default to keep all files.
29-
// Only take effect when Mode is `file` or `volume`, both work when Rotation is `daily` or `size`.
30-
KeepDays int `json:",optional"`
31-
// StackCooldownMillis represents the cooldown time for stack logging, default is 100ms.
32-
StackCooldownMillis int `json:",default=100"`
33-
// MaxBackups represents how many backup log files will be kept. 0 means all files will be kept forever.
34-
// Only take effect when RotationRuleType is `size`.
35-
// Even though `MaxBackups` sets 0, log files will still be removed
36-
// if the `KeepDays` limitation is reached.
37-
MaxBackups int `json:",default=0"`
38-
// MaxSize represents how much space the writing log file takes up. 0 means no limit. The unit is `MB`.
39-
// Only take effect when RotationRuleType is `size`
40-
MaxSize int `json:",default=0"`
41-
// Rotation represents the type of log rotation rule. Default is `daily`.
42-
// daily: daily rotation.
43-
// size: size limited rotation.
44-
Rotation string `json:",default=daily,options=[daily,size]"`
45-
// FileTimeFormat represents the time format for file name, default is `2006-01-02T15:04:05.000Z07:00`.
46-
FileTimeFormat string `json:",optional"`
47-
// LogKey represents the log key.
48-
LogKey logKeyConf `json:",optional"`
49-
}
3+
type (
4+
// A LogConf is a logging config.
5+
LogConf struct {
6+
// ServiceName represents the service name.
7+
ServiceName string `json:",optional"`
8+
// Mode represents the logging mode, default is `console`.
9+
// console: log to console.
10+
// file: log to file.
11+
// volume: used in k8s, prepend the hostname to the log file name.
12+
Mode string `json:",default=console,options=[console,file,volume]"`
13+
// Encoding represents the encoding type, default is `json`.
14+
// json: json encoding.
15+
// plain: plain text encoding, typically used in development.
16+
Encoding string `json:",default=json,options=[json,plain]"`
17+
// TimeFormat represents the time format, default is `2006-01-02T15:04:05.000Z07:00`.
18+
TimeFormat string `json:",optional"`
19+
// Path represents the log file path, default is `logs`.
20+
Path string `json:",default=logs"`
21+
// Level represents the log level, default is `info`.
22+
Level string `json:",default=info,options=[debug,info,error,severe]"`
23+
// MaxContentLength represents the max content bytes, default is no limit.
24+
MaxContentLength uint32 `json:",optional"`
25+
// Compress represents whether to compress the log file, default is `false`.
26+
Compress bool `json:",optional"`
27+
// Stat represents whether to log statistics, default is `true`.
28+
Stat bool `json:",default=true"`
29+
// KeepDays represents how many days the log files will be kept. Default to keep all files.
30+
// Only take effect when Mode is `file` or `volume`, both work when Rotation is `daily` or `size`.
31+
KeepDays int `json:",optional"`
32+
// StackCooldownMillis represents the cooldown time for stack logging, default is 100ms.
33+
StackCooldownMillis int `json:",default=100"`
34+
// MaxBackups represents how many backup log files will be kept. 0 means all files will be kept forever.
35+
// Only take effect when RotationRuleType is `size`.
36+
// Even though `MaxBackups` sets 0, log files will still be removed
37+
// if the `KeepDays` limitation is reached.
38+
MaxBackups int `json:",default=0"`
39+
// MaxSize represents how much space the writing log file takes up. 0 means no limit. The unit is `MB`.
40+
// Only take effect when RotationRuleType is `size`
41+
MaxSize int `json:",default=0"`
42+
// Rotation represents the type of log rotation rule. Default is `daily`.
43+
// daily: daily rotation.
44+
// size: size limited rotation.
45+
Rotation string `json:",default=daily,options=[daily,size]"`
46+
// FileTimeFormat represents the time format for file name, default is `2006-01-02T15:04:05.000Z07:00`.
47+
FileTimeFormat string `json:",optional"`
48+
// FieldKeys represents the field keys.
49+
FieldKeys fieldKeyConf `json:",optional"`
50+
}
5051

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"`
68-
}
52+
fieldKeyConf struct {
53+
// CallerKey represents the caller key.
54+
CallerKey string `json:",default=caller"`
55+
// ContentKey represents the content key.
56+
ContentKey string `json:",default=content"`
57+
// DurationKey represents the duration key.
58+
DurationKey string `json:",default=duration"`
59+
// LevelKey represents the level key.
60+
LevelKey string `json:",default=level"`
61+
// SpanKey represents the span key.
62+
SpanKey string `json:",default=span"`
63+
// TimestampKey represents the timestamp key.
64+
TimestampKey string `json:",default=@timestamp"`
65+
// TraceKey represents the trace key.
66+
TraceKey string `json:",default=trace"`
67+
// TruncatedKey represents the truncated key.
68+
TruncatedKey string `json:",default=truncated"`
69+
}
70+
)

core/logx/logs.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ func SetUp(c LogConf) (err error) {
276276
// Because multiple services in one process might call SetUp respectively.
277277
// Need to wait for the first caller to complete the execution.
278278
setupOnce.Do(func() {
279-
setupLogLevel(c)
280-
setupLogKey(c.LogKey)
279+
setupLogLevel(c.Level)
280+
setupFieldKeys(c.FieldKeys)
281281

282282
if !c.Stat {
283283
DisableStat()
@@ -481,8 +481,35 @@ func handleOptions(opts []LogOption) {
481481
}
482482
}
483483

484-
func setupLogLevel(c LogConf) {
485-
switch c.Level {
484+
func setupFieldKeys(c fieldKeyConf) {
485+
if len(c.CallerKey) > 0 {
486+
callerKey = c.CallerKey
487+
}
488+
if len(c.ContentKey) > 0 {
489+
contentKey = c.ContentKey
490+
}
491+
if len(c.DurationKey) > 0 {
492+
durationKey = c.DurationKey
493+
}
494+
if len(c.LevelKey) > 0 {
495+
levelKey = c.LevelKey
496+
}
497+
if len(c.SpanKey) > 0 {
498+
spanKey = c.SpanKey
499+
}
500+
if len(c.TimestampKey) > 0 {
501+
timestampKey = c.TimestampKey
502+
}
503+
if len(c.TraceKey) > 0 {
504+
traceKey = c.TraceKey
505+
}
506+
if len(c.TruncatedKey) > 0 {
507+
truncatedKey = c.TruncatedKey
508+
}
509+
}
510+
511+
func setupLogLevel(level string) {
512+
switch level {
486513
case levelDebug:
487514
SetLevel(DebugLevel)
488515
case levelInfo:
@@ -580,30 +607,3 @@ func writeStack(msg string) {
580607
func writeStat(msg string) {
581608
getWriter().Stat(msg, mergeGlobalFields(addCaller())...)
582609
}
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: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -779,15 +779,9 @@ func TestSetup(t *testing.T) {
779779
MaxBackups: 3,
780780
MaxSize: 1024 * 1024,
781781
}))
782-
setupLogLevel(LogConf{
783-
Level: levelInfo,
784-
})
785-
setupLogLevel(LogConf{
786-
Level: levelError,
787-
})
788-
setupLogLevel(LogConf{
789-
Level: levelSevere,
790-
})
782+
setupLogLevel(levelInfo)
783+
setupLogLevel(levelError)
784+
setupLogLevel(levelSevere)
791785
_, err := createOutput("")
792786
assert.NotNil(t, err)
793787
Disable()
@@ -1167,7 +1161,7 @@ func TestLogKey(t *testing.T) {
11671161
Mode: "console",
11681162
Encoding: "json",
11691163
TimeFormat: timeFormat,
1170-
LogKey: logKeyConf{
1164+
FieldKeys: fieldKeyConf{
11711165
CallerKey: "_caller",
11721166
ContentKey: "_content",
11731167
DurationKey: "_duration",
@@ -1180,7 +1174,7 @@ func TestLogKey(t *testing.T) {
11801174
})
11811175

11821176
t.Cleanup(func() {
1183-
setupLogKey(logKeyConf{
1177+
setupFieldKeys(fieldKeyConf{
11841178
CallerKey: defaultCallerKey,
11851179
ContentKey: defaultContentKey,
11861180
DurationKey: defaultDurationKey,

core/logx/vars.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,6 @@ const (
6363
defaultTruncatedKey = "truncated"
6464
)
6565

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
75-
)
76-
7766
var (
7867
// ErrLogPathNotSet is an error that indicates the log path is not set.
7968
ErrLogPathNotSet = errors.New("log path must be set")
@@ -84,3 +73,14 @@ var (
8473

8574
truncatedField = Field(truncatedKey, true)
8675
)
76+
77+
var (
78+
callerKey = defaultCallerKey
79+
contentKey = defaultContentKey
80+
durationKey = defaultDurationKey
81+
levelKey = defaultLevelKey
82+
spanKey = defaultSpanKey
83+
timestampKey = defaultTimestampKey
84+
traceKey = defaultTraceKey
85+
truncatedKey = defaultTruncatedKey
86+
)

core/logx/writer.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ func newFileWriter(c LogConf) (Writer, error) {
212212
statFile := path.Join(c.Path, statFilename)
213213

214214
handleOptions(opts)
215-
setupLogLevel(c)
216215

217216
if infoLog, err = createOutput(accessFile); err != nil {
218217
return nil, err

0 commit comments

Comments
 (0)