@@ -38,7 +38,7 @@ Each of these methods has a corresponding top-level function that uses the
38
38
default logger.
39
39
40
40
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.
42
42
43
43
2022/11/08 15:28:26 INFO hello count=3
44
44
@@ -66,6 +66,11 @@ produces this output:
66
66
67
67
{"time":"2022-11-08T15:28:26.000000000-05:00","level":"INFO","msg":"hello","count":3}
68
68
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
+
69
74
Setting a logger as the default with
70
75
71
76
slog.SetDefault(logger)
@@ -108,12 +113,25 @@ is the most efficient way to achieve the same output as
108
113
109
114
slog.Info("hello", "count", 3)
110
115
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
+
111
129
112
130
# Levels
113
131
114
132
A [Level] is an integer representing the importance or severity of a log event.
115
133
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,
117
135
but any int can be used as a level.
118
136
119
137
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.
122
140
The built-in handlers can be configured with the minimum level to output by
123
141
setting [HandlerOptions.Level].
124
142
The program's `main` function typically does this.
143
+ The default value is LevelInfo.
125
144
126
145
Setting the [HandlerOptions.Level] field to a [Level] value
127
146
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:
143
162
programLevel.Set(slog.LevelDebug)
144
163
145
164
146
- # Configuring the built-in handlers
165
+ # Groups
147
166
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.
149
172
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.
151
203
152
204
# Contexts
153
205
154
206
# Advanced topics
155
207
208
+
209
+
156
210
## Customizing a type's logging behavior
157
211
158
212
TODO: discuss LogValuer
@@ -165,6 +219,11 @@ TODO: discuss LogDepth, LogAttrDepth
165
219
166
220
TODO: discuss NewRecord, Record.AddAttrs
167
221
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
+
168
227
## Writing a handler
169
228
170
229
*/
0 commit comments