chore: refactor field keys in logx (#5104)

This commit is contained in:
Kevin Wan
2025-08-20 20:48:47 +08:00
committed by GitHub
parent 20f56ae1d0
commit 5f54f06ee5
5 changed files with 114 additions and 119 deletions

View File

@@ -1,7 +1,8 @@
package logx package logx
type (
// A LogConf is a logging config. // A LogConf is a logging config.
type LogConf struct { LogConf struct {
// ServiceName represents the service name. // ServiceName represents the service name.
ServiceName string `json:",optional"` ServiceName string `json:",optional"`
// Mode represents the logging mode, default is `console`. // Mode represents the logging mode, default is `console`.
@@ -44,11 +45,11 @@ type LogConf struct {
Rotation string `json:",default=daily,options=[daily,size]"` Rotation string `json:",default=daily,options=[daily,size]"`
// FileTimeFormat represents the time format for file name, default is `2006-01-02T15:04:05.000Z07:00`. // FileTimeFormat represents the time format for file name, default is `2006-01-02T15:04:05.000Z07:00`.
FileTimeFormat string `json:",optional"` FileTimeFormat string `json:",optional"`
// LogKey represents the log key. // FieldKeys represents the field keys.
LogKey logKeyConf `json:",optional"` FieldKeys fieldKeyConf `json:",optional"`
} }
type logKeyConf struct { fieldKeyConf struct {
// CallerKey represents the caller key. // CallerKey represents the caller key.
CallerKey string `json:",default=caller"` CallerKey string `json:",default=caller"`
// ContentKey represents the content key. // ContentKey represents the content key.
@@ -66,3 +67,4 @@ type logKeyConf struct {
// TruncatedKey represents the truncated key. // TruncatedKey represents the truncated key.
TruncatedKey string `json:",default=truncated"` TruncatedKey string `json:",default=truncated"`
} }
)

View File

@@ -276,8 +276,8 @@ func SetUp(c LogConf) (err error) {
// Because multiple services in one process might call SetUp respectively. // Because multiple services in one process might call SetUp respectively.
// Need to wait for the first caller to complete the execution. // Need to wait for the first caller to complete the execution.
setupOnce.Do(func() { setupOnce.Do(func() {
setupLogLevel(c) setupLogLevel(c.Level)
setupLogKey(c.LogKey) setupFieldKeys(c.FieldKeys)
if !c.Stat { if !c.Stat {
DisableStat() DisableStat()
@@ -481,8 +481,35 @@ func handleOptions(opts []LogOption) {
} }
} }
func setupLogLevel(c LogConf) { func setupFieldKeys(c fieldKeyConf) {
switch c.Level { if len(c.CallerKey) > 0 {
callerKey = c.CallerKey
}
if len(c.ContentKey) > 0 {
contentKey = c.ContentKey
}
if len(c.DurationKey) > 0 {
durationKey = c.DurationKey
}
if len(c.LevelKey) > 0 {
levelKey = c.LevelKey
}
if len(c.SpanKey) > 0 {
spanKey = c.SpanKey
}
if len(c.TimestampKey) > 0 {
timestampKey = c.TimestampKey
}
if len(c.TraceKey) > 0 {
traceKey = c.TraceKey
}
if len(c.TruncatedKey) > 0 {
truncatedKey = c.TruncatedKey
}
}
func setupLogLevel(level string) {
switch level {
case levelDebug: case levelDebug:
SetLevel(DebugLevel) SetLevel(DebugLevel)
case levelInfo: case levelInfo:
@@ -580,30 +607,3 @@ func writeStack(msg string) {
func writeStat(msg string) { func writeStat(msg string) {
getWriter().Stat(msg, mergeGlobalFields(addCaller())...) getWriter().Stat(msg, mergeGlobalFields(addCaller())...)
} }
func setupLogKey(c logKeyConf) {
if c.CallerKey != "" {
callerKey = c.CallerKey
}
if c.ContentKey != "" {
contentKey = c.ContentKey
}
if c.DurationKey != "" {
durationKey = c.DurationKey
}
if c.LevelKey != "" {
levelKey = c.LevelKey
}
if c.SpanKey != "" {
spanKey = c.SpanKey
}
if c.TimestampKey != "" {
timestampKey = c.TimestampKey
}
if c.TraceKey != "" {
traceKey = c.TraceKey
}
if c.TruncatedKey != "" {
truncatedKey = c.TruncatedKey
}
}

View File

@@ -779,15 +779,9 @@ func TestSetup(t *testing.T) {
MaxBackups: 3, MaxBackups: 3,
MaxSize: 1024 * 1024, MaxSize: 1024 * 1024,
})) }))
setupLogLevel(LogConf{ setupLogLevel(levelInfo)
Level: levelInfo, setupLogLevel(levelError)
}) setupLogLevel(levelSevere)
setupLogLevel(LogConf{
Level: levelError,
})
setupLogLevel(LogConf{
Level: levelSevere,
})
_, err := createOutput("") _, err := createOutput("")
assert.NotNil(t, err) assert.NotNil(t, err)
Disable() Disable()
@@ -1167,7 +1161,7 @@ func TestLogKey(t *testing.T) {
Mode: "console", Mode: "console",
Encoding: "json", Encoding: "json",
TimeFormat: timeFormat, TimeFormat: timeFormat,
LogKey: logKeyConf{ FieldKeys: fieldKeyConf{
CallerKey: "_caller", CallerKey: "_caller",
ContentKey: "_content", ContentKey: "_content",
DurationKey: "_duration", DurationKey: "_duration",
@@ -1180,7 +1174,7 @@ func TestLogKey(t *testing.T) {
}) })
t.Cleanup(func() { t.Cleanup(func() {
setupLogKey(logKeyConf{ setupFieldKeys(fieldKeyConf{
CallerKey: defaultCallerKey, CallerKey: defaultCallerKey,
ContentKey: defaultContentKey, ContentKey: defaultContentKey,
DurationKey: defaultDurationKey, DurationKey: defaultDurationKey,

View File

@@ -63,17 +63,6 @@ const (
defaultTruncatedKey = "truncated" defaultTruncatedKey = "truncated"
) )
var (
callerKey = defaultCallerKey
contentKey = defaultContentKey
durationKey = defaultDurationKey
levelKey = defaultLevelKey
spanKey = defaultSpanKey
timestampKey = defaultTimestampKey
traceKey = defaultTraceKey
truncatedKey = defaultTruncatedKey
)
var ( var (
// ErrLogPathNotSet is an error that indicates the log path is not set. // ErrLogPathNotSet is an error that indicates the log path is not set.
ErrLogPathNotSet = errors.New("log path must be set") ErrLogPathNotSet = errors.New("log path must be set")
@@ -84,3 +73,14 @@ var (
truncatedField = Field(truncatedKey, true) truncatedField = Field(truncatedKey, true)
) )
var (
callerKey = defaultCallerKey
contentKey = defaultContentKey
durationKey = defaultDurationKey
levelKey = defaultLevelKey
spanKey = defaultSpanKey
timestampKey = defaultTimestampKey
traceKey = defaultTraceKey
truncatedKey = defaultTruncatedKey
)

View File

@@ -212,7 +212,6 @@ func newFileWriter(c LogConf) (Writer, error) {
statFile := path.Join(c.Path, statFilename) statFile := path.Join(c.Path, statFilename)
handleOptions(opts) handleOptions(opts)
setupLogLevel(c)
if infoLog, err = createOutput(accessFile); err != nil { if infoLog, err = createOutput(accessFile); err != nil {
return nil, err return nil, err