@@ -177,7 +177,7 @@ By default `log` writes log messages to `os.Stdout`. The following example shows
177177how to change the log output:
178178
179179``` go
180- ctx := log.Context (context.Background (), log.WithOutputs (log. Output {Writer: os.Stderr , Format : log. FormatTerminal } ))
180+ ctx := log.Context (context.Background (), log.WithOutput ( os.Stderr ))
181181log.Printf (ctx, " hello world" )
182182```
183183
@@ -187,8 +187,17 @@ The example above logs the following message to stderr:
187187INFO[0000] msg="hello world"
188188```
189189
190- Each ` log.Output ` specifies both the destination writer and the formatter used
191- to turn log entries into bytes.
190+ The ` WithOutput ` function accepts any type that implements the ` io.Writer `
191+ interface.
192+
193+ For multiple outputs with independent formats, use ` WithOutputs ` :
194+
195+ ``` go
196+ ctx := log.Context (context.Background (), log.WithOutputs (
197+ log.Output {Writer: os.Stdout , Format : log.FormatTerminal },
198+ log.Output {Writer: logfile, Format : log.FormatJSON },
199+ ))
200+ ```
192201
193202## Log Format
194203
@@ -205,7 +214,7 @@ The text format is the default format used when the application is not running
205214in a terminal.
206215
207216``` go
208- ctx := log.Context (context.Background (), log.WithOutputs (log.Output {Writer: os. Stdout , Format : log. FormatText } ))
217+ ctx := log.Context (context.Background (), log.WithFormat (log.FormatText ))
209218log.Printf (ctx, " hello world" )
210219```
211220
@@ -223,7 +232,7 @@ The terminal format is the default format used when the application is running
223232in a terminal.
224233
225234``` go
226- ctx := log.Context (context.Background (), log.WithOutputs (log.Output {Writer: os. Stdout , Format : log. FormatTerminal } ))
235+ ctx := log.Context (context.Background (), log.WithFormat (log.FormatTerminal ))
227236log.Printf (ctx, " hello world" )
228237```
229238
@@ -242,7 +251,7 @@ blue for info entries and red for errors).
242251The JSON format prints entries in JSON.
243252
244253``` go
245- ctx := log.Context (context.Background (), log.WithOutputs (log.Output {Writer: os. Stdout , Format : log. FormatJSON } ))
254+ ctx := log.Context (context.Background (), log.WithFormat (log.FormatJSON ))
246255log.Printf (ctx, " hello world" )
247256```
248257
@@ -263,7 +272,7 @@ func formatFunc(entry *log.Entry) []byte {
263272 return []byte (fmt.Sprintf (" %s : %s " , entry.Severity , entry.KeyVals [0 ].V ))
264273}
265274
266- ctx := log.Context (context.Background (), log.WithOutputs (log. Output {Writer: os. Stdout , Format : formatFunc} ))
275+ ctx := log.Context (context.Background (), log.WithFormat ( formatFunc))
267276log.Printf (ctx, " hello world" )
268277```
269278
0 commit comments