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
type (
// A LogConf is a logging config.
type LogConf struct {
LogConf struct {
// ServiceName represents the service name.
ServiceName string `json:",optional"`
// Mode represents the logging mode, default is `console`.
@@ -44,11 +45,11 @@ type LogConf struct {
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 string `json:",optional"`
// LogKey represents the log key.
LogKey logKeyConf `json:",optional"`
// FieldKeys represents the field keys.
FieldKeys fieldKeyConf `json:",optional"`
}
type logKeyConf struct {
fieldKeyConf struct {
// CallerKey represents the caller key.
CallerKey string `json:",default=caller"`
// ContentKey represents the content key.
@@ -66,3 +67,4 @@ type logKeyConf struct {
// TruncatedKey represents the truncated key.
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.
// Need to wait for the first caller to complete the execution.
setupOnce.Do(func() {
setupLogLevel(c)
setupLogKey(c.LogKey)
setupLogLevel(c.Level)
setupFieldKeys(c.FieldKeys)
if !c.Stat {
DisableStat()
@@ -481,8 +481,35 @@ func handleOptions(opts []LogOption) {
}
}
func setupLogLevel(c LogConf) {
switch c.Level {
func setupFieldKeys(c fieldKeyConf) {
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:
SetLevel(DebugLevel)
case levelInfo:
@@ -580,30 +607,3 @@ func writeStack(msg string) {
func writeStat(msg string) {
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,
MaxSize: 1024 * 1024,
}))
setupLogLevel(LogConf{
Level: levelInfo,
})
setupLogLevel(LogConf{
Level: levelError,
})
setupLogLevel(LogConf{
Level: levelSevere,
})
setupLogLevel(levelInfo)
setupLogLevel(levelError)
setupLogLevel(levelSevere)
_, err := createOutput("")
assert.NotNil(t, err)
Disable()
@@ -1167,7 +1161,7 @@ func TestLogKey(t *testing.T) {
Mode: "console",
Encoding: "json",
TimeFormat: timeFormat,
LogKey: logKeyConf{
FieldKeys: fieldKeyConf{
CallerKey: "_caller",
ContentKey: "_content",
DurationKey: "_duration",
@@ -1180,7 +1174,7 @@ func TestLogKey(t *testing.T) {
})
t.Cleanup(func() {
setupLogKey(logKeyConf{
setupFieldKeys(fieldKeyConf{
CallerKey: defaultCallerKey,
ContentKey: defaultContentKey,
DurationKey: defaultDurationKey,

View File

@@ -63,17 +63,6 @@ const (
defaultTruncatedKey = "truncated"
)
var (
callerKey = defaultCallerKey
contentKey = defaultContentKey
durationKey = defaultDurationKey
levelKey = defaultLevelKey
spanKey = defaultSpanKey
timestampKey = defaultTimestampKey
traceKey = defaultTraceKey
truncatedKey = defaultTruncatedKey
)
var (
// ErrLogPathNotSet is an error that indicates the log path is not set.
ErrLogPathNotSet = errors.New("log path must be set")
@@ -84,3 +73,14 @@ var (
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)
handleOptions(opts)
setupLogLevel(c)
if infoLog, err = createOutput(accessFile); err != nil {
return nil, err