Skip to content

Commit

Permalink
slogutil: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Oct 1, 2024
1 parent f3def15 commit e9e5ef0
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions logutil/slogutil/slogutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func New(c *Config) (l *slog.Logger) {
return newDefault(output, lvl, c.AddTimestamp)
}

replaceAttr := replaceAttributes(!c.AddTimestamp)
replaceAttr := newReplaceAttr(!c.AddTimestamp)

var h slog.Handler
switch format {
Expand Down Expand Up @@ -102,9 +102,9 @@ func newDefault(output io.Writer, lvl slog.Level, addTimestamp bool) (l *slog.Lo
return slog.New(h)
}

// replaceAttributes is a function that returns
// [slog.HandlerOptions.ReplaceAttr] for provided parameters.
func replaceAttributes(removeTime bool) func(groups []string, a slog.Attr) (res slog.Attr) {
// newReplaceAttr is a function that returns [slog.HandlerOptions.ReplaceAttr]
// function for provided parameters.
func newReplaceAttr(removeTime bool) func(groups []string, a slog.Attr) (res slog.Attr) {
if !removeTime {
return ReplaceLevel
}
Expand All @@ -114,6 +114,9 @@ func replaceAttributes(removeTime bool) func(groups []string, a slog.Attr) (res
}
}

// traceAttrValue is a value for [LevelTrace] slog attributes.
var traceAttrValue = slog.StringValue("TRACE")

// ReplaceLevel is a function for [slog.HandlerOptions.ReplaceAttr] that adds
// [LevelTrace] custom name for level attribute.
func ReplaceLevel(groups []string, a slog.Attr) (res slog.Attr) {
Expand All @@ -124,7 +127,7 @@ func ReplaceLevel(groups []string, a slog.Attr) (res slog.Attr) {
if a.Key == slog.LevelKey {
lvl := a.Value.Any().(slog.Level)
if lvl == LevelTrace {
a.Value = slog.StringValue("TRACE")
a.Value = traceAttrValue
}
}

Expand Down

0 comments on commit e9e5ef0

Please sign in to comment.