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,68 +1,70 @@
package logx package logx
// A LogConf is a logging config. type (
type LogConf struct { // A LogConf is a logging config.
// ServiceName represents the service name. LogConf struct {
ServiceName string `json:",optional"` // ServiceName represents the service name.
// Mode represents the logging mode, default is `console`. ServiceName string `json:",optional"`
// console: log to console. // Mode represents the logging mode, default is `console`.
// file: log to file. // console: log to console.
// volume: used in k8s, prepend the hostname to the log file name. // file: log to file.
Mode string `json:",default=console,options=[console,file,volume]"` // volume: used in k8s, prepend the hostname to the log file name.
// Encoding represents the encoding type, default is `json`. Mode string `json:",default=console,options=[console,file,volume]"`
// json: json encoding. // Encoding represents the encoding type, default is `json`.
// plain: plain text encoding, typically used in development. // json: json encoding.
Encoding string `json:",default=json,options=[json,plain]"` // plain: plain text encoding, typically used in development.
// TimeFormat represents the time format, default is `2006-01-02T15:04:05.000Z07:00`. Encoding string `json:",default=json,options=[json,plain]"`
TimeFormat string `json:",optional"` // TimeFormat represents the time format, default is `2006-01-02T15:04:05.000Z07:00`.
// Path represents the log file path, default is `logs`. TimeFormat string `json:",optional"`
Path string `json:",default=logs"` // Path represents the log file path, default is `logs`.
// Level represents the log level, default is `info`. Path string `json:",default=logs"`
Level string `json:",default=info,options=[debug,info,error,severe]"` // Level represents the log level, default is `info`.
// MaxContentLength represents the max content bytes, default is no limit. Level string `json:",default=info,options=[debug,info,error,severe]"`
MaxContentLength uint32 `json:",optional"` // MaxContentLength represents the max content bytes, default is no limit.
// Compress represents whether to compress the log file, default is `false`. MaxContentLength uint32 `json:",optional"`
Compress bool `json:",optional"` // Compress represents whether to compress the log file, default is `false`.
// Stat represents whether to log statistics, default is `true`. Compress bool `json:",optional"`
Stat bool `json:",default=true"` // Stat represents whether to log statistics, default is `true`.
// KeepDays represents how many days the log files will be kept. Default to keep all files. Stat bool `json:",default=true"`
// Only take effect when Mode is `file` or `volume`, both work when Rotation is `daily` or `size`. // KeepDays represents how many days the log files will be kept. Default to keep all files.
KeepDays int `json:",optional"` // Only take effect when Mode is `file` or `volume`, both work when Rotation is `daily` or `size`.
// StackCooldownMillis represents the cooldown time for stack logging, default is 100ms. KeepDays int `json:",optional"`
StackCooldownMillis int `json:",default=100"` // StackCooldownMillis represents the cooldown time for stack logging, default is 100ms.
// MaxBackups represents how many backup log files will be kept. 0 means all files will be kept forever. StackCooldownMillis int `json:",default=100"`
// Only take effect when RotationRuleType is `size`. // MaxBackups represents how many backup log files will be kept. 0 means all files will be kept forever.
// Even though `MaxBackups` sets 0, log files will still be removed // Only take effect when RotationRuleType is `size`.
// if the `KeepDays` limitation is reached. // Even though `MaxBackups` sets 0, log files will still be removed
MaxBackups int `json:",default=0"` // if the `KeepDays` limitation is reached.
// MaxSize represents how much space the writing log file takes up. 0 means no limit. The unit is `MB`. MaxBackups int `json:",default=0"`
// Only take effect when RotationRuleType is `size` // MaxSize represents how much space the writing log file takes up. 0 means no limit. The unit is `MB`.
MaxSize int `json:",default=0"` // Only take effect when RotationRuleType is `size`
// Rotation represents the type of log rotation rule. Default is `daily`. MaxSize int `json:",default=0"`
// daily: daily rotation. // Rotation represents the type of log rotation rule. Default is `daily`.
// size: size limited rotation. // daily: daily rotation.
Rotation string `json:",default=daily,options=[daily,size]"` // size: size limited rotation.
// FileTimeFormat represents the time format for file name, default is `2006-01-02T15:04:05.000Z07:00`. Rotation string `json:",default=daily,options=[daily,size]"`
FileTimeFormat string `json:",optional"` // FileTimeFormat represents the time format for file name, default is `2006-01-02T15:04:05.000Z07:00`.
// LogKey represents the log key. FileTimeFormat string `json:",optional"`
LogKey logKeyConf `json:",optional"` // FieldKeys represents the field keys.
} 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.
ContentKey string `json:",default=content"` ContentKey string `json:",default=content"`
// DurationKey represents the duration key. // DurationKey represents the duration key.
DurationKey string `json:",default=duration"` DurationKey string `json:",default=duration"`
// LevelKey represents the level key. // LevelKey represents the level key.
LevelKey string `json:",default=level"` LevelKey string `json:",default=level"`
// SpanKey represents the span key. // SpanKey represents the span key.
SpanKey string `json:",default=span"` SpanKey string `json:",default=span"`
// TimestampKey represents the timestamp key. // TimestampKey represents the timestamp key.
TimestampKey string `json:",default=@timestamp"` TimestampKey string `json:",default=@timestamp"`
// TraceKey represents the trace key. // TraceKey represents the trace key.
TraceKey string `json:",default=trace"` TraceKey string `json:",default=trace"`
// 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