@@ -44,7 +44,7 @@ For more control over the output format, create a logger with a different handle
4444This statement uses [New] to create a new logger with a TextHandler
4545that writes structured records in text form to standard error:
4646
47- logger := slog.New(slog.NewTextHandler(os.Stderr))
47+ logger := slog.New(slog.NewTextHandler(os.Stderr, nil ))
4848
4949[TextHandler] output is a sequence of key=value pairs, easily and unambiguously
5050parsed by machine. This statement:
@@ -57,14 +57,14 @@ produces this output:
5757
5858The package also provides [JSONHandler], whose output is line-delimited JSON:
5959
60- logger := slog.New(slog.NewJSONHandler(os.Stdout))
60+ logger := slog.New(slog.NewJSONHandler(os.Stdout, nil ))
6161 logger.Info("hello", "count", 3)
6262
6363produces this output:
6464
6565 {"time":"2022-11-08T15:28:26.000000000-05:00","level":"INFO","msg":"hello","count":3}
6666
67- Both [TextHandler] and [JSONHandler] can be configured with a [HandlerOptions].
67+ Both [TextHandler] and [JSONHandler] can be configured with [HandlerOptions].
6868There are options for setting the minimum level (see Levels, below),
6969displaying the source file and line of the log call, and
7070modifying attributes before they are logged.
@@ -78,38 +78,6 @@ will cause the top-level functions like [Info] to use it.
7878so that existing applications that use [log.Printf] and related functions
7979will send log records to the logger's handler without needing to be rewritten.
8080
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-
11381Some attributes are common to many log calls.
11482For example, you may wish to include the URL or trace identifier of a server request
11583with all log events arising from the request.
@@ -149,7 +117,7 @@ a global LevelVar:
149117
150118Then use the LevelVar to construct a handler, and make it the default:
151119
152- h := slog.HandlerOptions{Level: programLevel}.NewJSONHandler(os.Stderr )
120+ h := slog.NewJSONHandler(os.Stderr, &slog. HandlerOptions{Level: programLevel})
153121 slog.SetDefault(slog.New(h))
154122
155123Now the program can change its logging level with a single statement:
@@ -164,11 +132,11 @@ How this qualification is displayed depends on the handler.
164132[TextHandler] separates the group and attribute names with a dot.
165133[JSONHandler] treats each group as a separate JSON object, with the group name as the key.
166134
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 :
168136
169137 slog.Group("request",
170- slog.String( "method", r.Method) ,
171- slog.Any( "url", r.URL) )
138+ "method", r.Method,
139+ "url", r.URL)
172140
173141TextHandler would display this group as
174142
@@ -199,7 +167,7 @@ so even if it uses the common key "id", the log line will have distinct keys.
199167
200168Some handlers may wish to include information from the [context.Context] that is
201169available 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.
203171
204172The [Logger.Log] and [Logger.LogAttrs] methods take a context as a first
205173argument, as do their corresponding top-level functions.
@@ -212,6 +180,38 @@ in "Ctx" do. For example,
212180
213181It is recommended to pass a context to an output method if one is available.
214182
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+
215215# Customizing a type's logging behavior
216216
217217If a type implements the [LogValuer] interface, the [Value] returned from its LogValue
0 commit comments