slog: export built-in keys
Change-Id: If3679079b162761d94a0a8a7b79a19ea2e090332
Reviewed-on: https://go-review.googlesource.com/c/exp/+/442788
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
diff --git a/slog/handler.go b/slog/handler.go
index 4395ec0..5a12bff 100644
--- a/slog/handler.go
+++ b/slog/handler.go
@@ -122,10 +122,18 @@
// Keys for "built-in" attributes.
const (
- timeKey = "time" // time.Time: when log method is called
- levelKey = "level" // Level: level of log method
- messageKey = "msg" // string: message of log method
- sourceKey = "source" // string: file:line of log call
+ // TimeKey is the key used by the built-in handlers for the time
+ // when the log method is called. The associated Value is a [time.Time].
+ TimeKey = "time"
+ // LevelKey is the key used by the built-in handlers for the level
+ // of the log call. The associated value is a [Level].
+ LevelKey = "level"
+ // MessageKey is the key used by the built-in handlers for the
+ // message of the log call. The associated value is a string.
+ MessageKey = "msg"
+ // SourceKey is the key used by the built-in handlers for the source file
+ // and line of the log call. The associated value is a string.
+ SourceKey = "source"
)
type commonHandler struct {
@@ -205,7 +213,7 @@
// Built-in attributes. They are not scoped.
// time
if !r.Time().IsZero() {
- key := timeKey
+ key := TimeKey
val := r.Time().Round(0) // strip monotonic to match Attr behavior
if rep == nil {
state.appendKey(key)
@@ -215,7 +223,7 @@
}
}
// level
- key := levelKey
+ key := LevelKey
val := r.Level()
if rep == nil {
state.appendKey(key)
@@ -227,7 +235,7 @@
if h.opts.AddSource {
file, line := r.SourceLine()
if file != "" {
- key := sourceKey
+ key := SourceKey
if rep == nil {
state.appendKey(key)
state.appendSource(file, line)
@@ -242,7 +250,7 @@
}
}
}
- key = messageKey
+ key = MessageKey
msg := r.Message()
if rep == nil {
state.appendKey(key)
diff --git a/slog/handler_test.go b/slog/handler_test.go
index 8a967ef..af847d8 100644
--- a/slog/handler_test.go
+++ b/slog/handler_test.go
@@ -180,14 +180,14 @@
},
{
name: "remove built-in",
- replace: removeKeys(timeKey, levelKey, messageKey),
+ replace: removeKeys(TimeKey, LevelKey, MessageKey),
attrs: attrs,
wantText: "a=one b=2",
wantJSON: `{"a":"one","b":2}`,
},
{
name: "preformatted remove built-in",
- replace: removeKeys(timeKey, levelKey, messageKey),
+ replace: removeKeys(TimeKey, LevelKey, MessageKey),
with: func(h Handler) Handler { return h.With(preAttrs) },
attrs: attrs,
wantText: "pre=3 x=y a=one b=2",
@@ -195,7 +195,7 @@
},
{
name: "groups",
- replace: removeKeys(timeKey, levelKey), // to simplify the result
+ replace: removeKeys(TimeKey, LevelKey), // to simplify the result
attrs: []Attr{
Int("a", 1),
Group("g",
@@ -209,14 +209,14 @@
},
{
name: "empty group",
- replace: removeKeys(timeKey, levelKey),
+ replace: removeKeys(TimeKey, LevelKey),
attrs: []Attr{Group("g"), Group("h", Int("a", 1))},
wantText: "msg=message h·a=1",
wantJSON: `{"msg":"message","g":{},"h":{"a":1}}`,
},
{
name: "escapes",
- replace: removeKeys(timeKey, levelKey),
+ replace: removeKeys(TimeKey, LevelKey),
attrs: []Attr{
String("a b", "x\t\n\000y"),
Group(" b.c=\"\\x2E\t",
@@ -228,7 +228,7 @@
},
{
name: "LogValuer",
- replace: removeKeys(timeKey, levelKey),
+ replace: removeKeys(TimeKey, LevelKey),
attrs: []Attr{
Int("a", 1),
Any("name", logValueName{"Ren", "Hoek"}),
@@ -239,7 +239,7 @@
},
{
name: "scope",
- replace: removeKeys(timeKey, levelKey),
+ replace: removeKeys(TimeKey, LevelKey),
with: func(h Handler) Handler { return h.With(preAttrs).WithScope("s") },
attrs: attrs,
wantText: "msg=message pre=3 x=y s·a=one s·b=2",
@@ -247,7 +247,7 @@
},
{
name: "preformatted scopes",
- replace: removeKeys(timeKey, levelKey),
+ replace: removeKeys(TimeKey, LevelKey),
with: func(h Handler) Handler {
return h.With([]Attr{Int("p1", 1)}).
WithScope("s1").
@@ -260,7 +260,7 @@
},
{
name: "two scopes",
- replace: removeKeys(timeKey, levelKey),
+ replace: removeKeys(TimeKey, LevelKey),
with: func(h Handler) Handler {
return h.With([]Attr{Int("p1", 1)}).
WithScope("s1").