@@ -44,7 +44,7 @@ For more control over the output format, create a logger with a different handle
44
44
This statement uses [New] to create a new logger with a TextHandler
45
45
that writes structured records in text form to standard error:
46
46
47
- logger := slog.New(slog.NewTextHandler(os.Stderr))
47
+ logger := slog.New(slog.NewTextHandler(os.Stderr, nil ))
48
48
49
49
[TextHandler] output is a sequence of key=value pairs, easily and unambiguously
50
50
parsed by machine. This statement:
@@ -57,14 +57,14 @@ produces this output:
57
57
58
58
The package also provides [JSONHandler], whose output is line-delimited JSON:
59
59
60
- logger := slog.New(slog.NewJSONHandler(os.Stdout))
60
+ logger := slog.New(slog.NewJSONHandler(os.Stdout, nil ))
61
61
logger.Info("hello", "count", 3)
62
62
63
63
produces this output:
64
64
65
65
{"time":"2022-11-08T15:28:26.000000000-05:00","level":"INFO","msg":"hello","count":3}
66
66
67
- Both [TextHandler] and [JSONHandler] can be configured with a [HandlerOptions].
67
+ Both [TextHandler] and [JSONHandler] can be configured with [HandlerOptions].
68
68
There are options for setting the minimum level (see Levels, below),
69
69
displaying the source file and line of the log call, and
70
70
modifying attributes before they are logged.
@@ -78,38 +78,6 @@ will cause the top-level functions like [Info] to use it.
78
78
so that existing applications that use [log.Printf] and related functions
79
79
will send log records to the logger's handler without needing to be rewritten.
80
80
81
- # Attrs and Values
82
-
83
- An [Attr] is a key-value pair. The Logger output methods accept Attrs as well as
84
- alternating keys and values. The statement
85
-
86
- slog.Info("hello", slog.Int("count", 3))
87
-
88
- behaves the same as
89
-
90
- slog.Info("hello", "count", 3)
91
-
92
- There are convenience constructors for [Attr] such as [Int], [String], and [Bool]
93
- for common types, as well as the function [Any] for constructing Attrs of any
94
- type.
95
-
96
- The value part of an Attr is a type called [Value].
97
- Like an [any], a Value can hold any Go value,
98
- but it can represent typical values, including all numbers and strings,
99
- without an allocation.
100
-
101
- For the most efficient log output, use [Logger.LogAttrs].
102
- It is similar to [Logger.Log] but accepts only Attrs, not alternating
103
- keys and values; this allows it, too, to avoid allocation.
104
-
105
- The call
106
-
107
- logger.LogAttrs(nil, slog.LevelInfo, "hello", slog.Int("count", 3))
108
-
109
- is the most efficient way to achieve the same output as
110
-
111
- slog.Info("hello", "count", 3)
112
-
113
81
Some attributes are common to many log calls.
114
82
For example, you may wish to include the URL or trace identifier of a server request
115
83
with all log events arising from the request.
@@ -149,7 +117,7 @@ a global LevelVar:
149
117
150
118
Then use the LevelVar to construct a handler, and make it the default:
151
119
152
- h := slog.HandlerOptions{Level: programLevel}.NewJSONHandler(os.Stderr )
120
+ h := slog.NewJSONHandler(os.Stderr, &slog. HandlerOptions{Level: programLevel})
153
121
slog.SetDefault(slog.New(h))
154
122
155
123
Now the program can change its logging level with a single statement:
@@ -164,11 +132,11 @@ How this qualification is displayed depends on the handler.
164
132
[TextHandler] separates the group and attribute names with a dot.
165
133
[JSONHandler] treats each group as a separate JSON object, with the group name as the key.
166
134
167
- Use [Group] to create a Group Attr from a name and a list of Attrs :
135
+ Use [Group] to create a Group attribute from a name and a list of key-value pairs :
168
136
169
137
slog.Group("request",
170
- slog.String( "method", r.Method) ,
171
- slog.Any( "url", r.URL) )
138
+ "method", r.Method,
139
+ "url", r.URL)
172
140
173
141
TextHandler would display this group as
174
142
@@ -199,7 +167,7 @@ so even if it uses the common key "id", the log line will have distinct keys.
199
167
200
168
Some handlers may wish to include information from the [context.Context] that is
201
169
available at the call site. One example of such information
202
- is the identifier for the current span when tracing is is enabled.
170
+ is the identifier for the current span when tracing is enabled.
203
171
204
172
The [Logger.Log] and [Logger.LogAttrs] methods take a context as a first
205
173
argument, as do their corresponding top-level functions.
@@ -212,6 +180,38 @@ in "Ctx" do. For example,
212
180
213
181
It is recommended to pass a context to an output method if one is available.
214
182
183
+ # Attrs and Values
184
+
185
+ An [Attr] is a key-value pair. The Logger output methods accept Attrs as well as
186
+ alternating keys and values. The statement
187
+
188
+ slog.Info("hello", slog.Int("count", 3))
189
+
190
+ behaves the same as
191
+
192
+ slog.Info("hello", "count", 3)
193
+
194
+ There are convenience constructors for [Attr] such as [Int], [String], and [Bool]
195
+ for common types, as well as the function [Any] for constructing Attrs of any
196
+ type.
197
+
198
+ The value part of an Attr is a type called [Value].
199
+ Like an [any], a Value can hold any Go value,
200
+ but it can represent typical values, including all numbers and strings,
201
+ without an allocation.
202
+
203
+ For the most efficient log output, use [Logger.LogAttrs].
204
+ It is similar to [Logger.Log] but accepts only Attrs, not alternating
205
+ keys and values; this allows it, too, to avoid allocation.
206
+
207
+ The call
208
+
209
+ logger.LogAttrs(nil, slog.LevelInfo, "hello", slog.Int("count", 3))
210
+
211
+ is the most efficient way to achieve the same output as
212
+
213
+ slog.Info("hello", "count", 3)
214
+
215
215
# Customizing a type's logging behavior
216
216
217
217
If a type implements the [LogValuer] interface, the [Value] returned from its LogValue
0 commit comments