slog: use constants for built-in keys
Change-Id: Ia79a646a92b596034cd0aba0793588921d10caa8
Reviewed-on: https://go-review.googlesource.com/c/exp/+/438995
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
diff --git a/slog/handler.go b/slog/handler.go
index 554578b..f0c1f9e 100644
--- a/slog/handler.go
+++ b/slog/handler.go
@@ -123,6 +123,14 @@
ReplaceAttr func(a Attr) Attr
}
+// 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
+)
+
type commonHandler struct {
json bool // true => output JSON; false => output text
opts HandlerOptions
@@ -173,7 +181,7 @@
}
// time
if !r.Time().IsZero() {
- key := "time"
+ key := timeKey
val := r.Time().Round(0) // strip monotonic to match Attr behavior
if rep == nil {
state.appendKey(key)
@@ -183,7 +191,7 @@
}
}
// level
- key := "level"
+ key := levelKey
val := r.Level()
if rep == nil {
state.appendKey(key)
@@ -195,7 +203,7 @@
if h.opts.AddSource {
file, line := r.SourceLine()
if file != "" {
- key := "source"
+ key := sourceKey
if rep == nil {
state.appendKey(key)
state.appendSource(file, line)
@@ -210,7 +218,7 @@
}
}
}
- key = "msg"
+ key = messageKey
msg := r.Message()
if rep == nil {
state.appendKey(key)