Skip to content

Commit

Permalink
slogutil: jsonhybrid
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Oct 22, 2024
1 parent 1eb6fbc commit abce6ff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
19 changes: 12 additions & 7 deletions logutil/slogutil/jsonhybrid_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func ExampleJSONHybridHandler() {
AddSource: false,
Level: slog.LevelDebug,
// Use slogutil.RemoveTime to make the example reproducible.
ReplaceAttr: slogutil.RemoveTime,
ReplaceAttr: slogutil.RenameAttrs,
}, true)
l := slog.New(h)

Expand All @@ -26,11 +26,16 @@ func ExampleJSONHybridHandler() {
l.Info("new info with no attributes")
l.Info("new info with attributes", "number", 123)

l.Error("error with no attributes")
l.Error("error with attributes", "number", 123)

// Output:
// {"level":"DEBUG","msg":"debug with no attributes; attrs: level=DEBUG"}
// {"level":"DEBUG","msg":"debug with attributes; attrs: level=DEBUG number=123"}
// {"level":"INFO","msg":"info with no attributes; attrs: level=INFO"}
// {"level":"INFO","msg":"info with attributes; attrs: level=INFO number=123"}
// {"level":"INFO","msg":"new info with no attributes; attrs: level=INFO attr=abc"}
// {"level":"INFO","msg":"new info with attributes; attrs: level=INFO attr=abc number=123"}
// {"severity":"NORMAL","message":"debug with no attributes; attrs: level=DEBUG"}
// {"severity":"NORMAL","message":"debug with attributes; attrs: level=DEBUG number=123"}
// {"severity":"NORMAL","message":"info with no attributes; attrs: level=INFO"}
// {"severity":"NORMAL","message":"info with attributes; attrs: level=INFO number=123"}
// {"severity":"NORMAL","message":"new info with no attributes; attrs: level=INFO attr=abc"}
// {"severity":"NORMAL","message":"new info with attributes; attrs: level=INFO attr=abc number=123"}
// {"severity":"ERROR","message":"error with no attributes; attrs: level=ERROR attr=abc"}
// {"severity":"ERROR","message":"error with attributes; attrs: level=ERROR attr=abc number=123"}
}
6 changes: 3 additions & 3 deletions logutil/slogutil/slogutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func New(c *Config) (l *slog.Logger) {
case FormatJSONHybrid:
h = NewJSONHybridHandler(output, &slog.HandlerOptions{
Level: lvl,
ReplaceAttr: renameAttrs,
ReplaceAttr: RenameAttrs,
}, !c.AddTimestamp)
case FormatText:
h = slog.NewTextHandler(output, &slog.HandlerOptions{
Expand Down Expand Up @@ -148,11 +148,11 @@ func ReplaceLevel(groups []string, a slog.Attr) (res slog.Attr) {
// normalAttrValue is a NORMAL value under the [slog.LevelKey] key.
var normalAttrValue = slog.StringValue("NORMAL")

// renameAttrs is a [slog.HandlerOptions.ReplaceAttr] function that renames the
// RenameAttrs is a [slog.HandlerOptions.ReplaceAttr] function that renames the
// [slog.LevelKey] key to [keySeverity], and the [slog.MessageKey] key to
// [keyMessage]. It also sets the level value to "NORMAL" for levels less than
// [LevelError].
func renameAttrs(groups []string, a slog.Attr) (res slog.Attr) {
func RenameAttrs(groups []string, a slog.Attr) (res slog.Attr) {
if len(groups) > 0 {
return a
}
Expand Down

0 comments on commit abce6ff

Please sign in to comment.