Skip to content

Commit 3c43f8b

Browse files
committed
slog: document HandlerOptions and groups
Change-Id: If4bf0e1b53ebc6b5ffa500f64dd4056a184062a6 Reviewed-on: https://go-review.googlesource.com/c/exp/+/456621 Run-TryBot: Jonathan Amsterdam <[email protected]> Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 0915cd7 commit 3c43f8b

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

slog/doc.go

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Each of these methods has a corresponding top-level function that uses the
3838
default logger.
3939
4040
The default handler formats the log record's message, time, level, and attributes
41-
as a string and passes it to the [log] package."
41+
as a string and passes it to the [log] package.
4242
4343
2022/11/08 15:28:26 INFO hello count=3
4444
@@ -66,6 +66,11 @@ produces this output:
6666
6767
{"time":"2022-11-08T15:28:26.000000000-05:00","level":"INFO","msg":"hello","count":3}
6868
69+
Both [TextHandler] and [JSONHandler] can be configured with a [HandlerOptions].
70+
There are options for setting the minimum level (see Levels, below),
71+
displaying the source file and line of the log call, and
72+
modifying attributes before they are logged.
73+
6974
Setting a logger as the default with
7075
7176
slog.SetDefault(logger)
@@ -108,12 +113,25 @@ is the most efficient way to achieve the same output as
108113
109114
slog.Info("hello", "count", 3)
110115
116+
Some attributes are common to many log calls.
117+
For example, you may wish to include the URL or trace identifier of a server request
118+
with all log events arising from the request.
119+
Rather than repeat the attribute with every log call, you can use [Logger.With]
120+
to construct a new Logger containing the attributes:
121+
122+
logger2 := logger.With("url", r.URL)
123+
124+
The arguments to With are the same key-value pairs used in [Logger.Info].
125+
The result is a new Logger with the same handler as the original, but additional
126+
attributes that will appear in the output of every call.
127+
128+
111129
112130
# Levels
113131
114132
A [Level] is an integer representing the importance or severity of a log event.
115133
The higher the level, the more severe the event.
116-
This package defines four constants for the most common levels,
134+
This package defines constants for the most common levels,
117135
but any int can be used as a level.
118136
119137
In an application, you may wish to log messages only at a certain level or greater.
@@ -122,6 +140,7 @@ suppressing debug logging until it is needed.
122140
The built-in handlers can be configured with the minimum level to output by
123141
setting [HandlerOptions.Level].
124142
The program's `main` function typically does this.
143+
The default value is LevelInfo.
125144
126145
Setting the [HandlerOptions.Level] field to a [Level] value
127146
fixes the handler's minimum level throughout its lifetime.
@@ -143,16 +162,51 @@ Now the program can change its logging level with a single statement:
143162
programLevel.Set(slog.LevelDebug)
144163
145164
146-
# Configuring the built-in handlers
165+
# Groups
147166
148-
TODO: cover HandlerOptions
167+
Attributes can be collected into groups.
168+
A group has a name that is used to qualify the names of its attributes.
169+
How this qualification is displayed depends on the handler.
170+
[TextHandler] separates the group and attribute names with a dot.
171+
[JSONHandler] treats each group as a separate JSON object, with the group name as the key.
149172
150-
# Groups
173+
Use [Group] to create a Group Attr from a name and a list of Attrs:
174+
175+
slog.Group("request",
176+
slog.String("method", r.Method),
177+
slog.Any("url", r.URL))
178+
179+
TextHandler would display this group as
180+
181+
request.method=GET request.url=http://example.com
182+
183+
JSONHandler would display it as
184+
185+
"request":{"method":"GET","url":"http://example.com"}
186+
187+
Use [Logger.WithGroup] to qualify all of a Logger's output
188+
with a group name. Calling WithGroup on a Logger results in a
189+
new Logger with the same Handler as the original, but with all
190+
its attributes qualified by the group name.
191+
192+
This can help prevent duplicate attribute keys in large systems,
193+
where subsystems might use the same keys.
194+
Pass each subsystem a different Logger with its own group name so that
195+
potential duplicates are qualified:
196+
197+
logger := slog.Default().With("id", systemID)
198+
parserLogger := logger.WithGroup("parser")
199+
parseInput(input, parserLogger)
200+
201+
When parseInput logs with parserLogger, its keys will be qualified with "parser",
202+
so even if it uses the common key "id", the log line will have distinct keys.
151203
152204
# Contexts
153205
154206
# Advanced topics
155207
208+
209+
156210
## Customizing a type's logging behavior
157211
158212
TODO: discuss LogValuer
@@ -165,6 +219,11 @@ TODO: discuss LogDepth, LogAttrDepth
165219
166220
TODO: discuss NewRecord, Record.AddAttrs
167221
222+
## Performance considerations
223+
224+
TODO: mention to avoid unnecessary work (like calling URL.String()) in log args in case
225+
the log statement is disabled.
226+
168227
## Writing a handler
169228
170229
*/

0 commit comments

Comments
 (0)