@@ -9,11 +9,11 @@ This document is maintained by Jonathan Amsterdam `
[email protected] `.
9
9
10
10
1 . [ Introduction] ( #introduction )
11
11
1 . [ Loggers and their handlers] ( #loggers-and-their-handlers )
12
- 1 . [ Implementing ` Handler ` methods] ( #implementing-` handler` -methods )
13
- 1. [The `Enabled` method](#the-` enabled` -method)
14
- 1. [The `Handle` method](#the-` handle` -method)
15
- 1. [The `WithAttrs` method](#the-` withattrs` -method)
16
- 1. [The `WithGroup` method](#the-` withgroup` -method)
12
+ 1 . [ Implementing ` Handler ` methods] ( #implementing-handler-methods )
13
+ 1. [The `Enabled` method](#the-enabled-method)
14
+ 1. [The `Handle` method](#the-handle-method)
15
+ 1. [The `WithAttrs` method](#the-withattrs-method)
16
+ 1. [The `WithGroup` method](#the-withgroup-method)
17
17
1. [Testing](#testing)
18
18
1 . [ General considerations] ( #general-considerations )
19
19
1. [Copying records](#copying-records)
@@ -28,7 +28,7 @@ The standard library’s `log/slog` package has a two-part design.
28
28
A "frontend," implemented by the ` Logger ` type,
29
29
gathers structured log information like a message, level, and attributes,
30
30
and passes them to a "backend," an implementation of the ` Handler ` interface.
31
- The package comes with two built-in handlers that usually should be adequate.
31
+ The package comes with two built-in handlers that should usually be adequate.
32
32
But you may need to write your own handler, and that is not always straightforward.
33
33
This guide is here to help.
34
34
@@ -41,7 +41,7 @@ types work together.
41
41
Each logger contains a handler. Certain ` Logger ` methods do some preliminary work,
42
42
such as gathering key-value pairs into ` Attr ` s, and then call one or more
43
43
` Handler ` methods. These ` Logger ` methods are ` With ` , ` WithGroup ` ,
44
- and the output methods.
44
+ and the output methods like ` Info ` , ` Error ` and so on .
45
45
46
46
An output method fulfills the main role of a logger: producing log output.
47
47
Here is a call to an output method:
@@ -107,6 +107,11 @@ so it will sometimes produce invalid YAML.
107
107
For example, it doesn't quote keys that have colons in them.
108
108
We'll call it ` IndentHandler ` to forestall disappointment.
109
109
110
+ A brief aside before we start: it is tempting to embed ` slog.Handler ` in your
111
+ custom handler and implement only the methods that you need.
112
+ Loggers and handlers are too tightly coupled for that to work. You should
113
+ implement all four handler methods.
114
+
110
115
We begin with the ` IndentHandler ` type
111
116
and the ` New ` function that constructs it from an ` io.Writer ` and options:
112
117
@@ -439,7 +444,7 @@ Most of the fields of `IndentHandler` can be copied shallowly, but the slice of
439
444
the same underlying array. If we used ` append ` instead of making an explicit
440
445
copy, we would introduce that subtle aliasing bug.
441
446
442
- Using ` withGroupOrAttrs ` , the ` With ` methods are easy:
447
+ The ` With ` methods are easy to write using ` withGroupOrAttrs ` :
443
448
444
449
```
445
450
func (h *IndentHandler) WithGroup(name string) slog.Handler {
@@ -543,7 +548,7 @@ See [this bug report](https://go.dev/issue/61321) for more detail.
543
548
544
549
### With pre-formatting
545
550
546
- Our second implementation implements pre-formatting.
551
+ Our second version of the ` WithGroup ` and ` WithAttrs ` methods provides pre-formatting.
547
552
This implementation is more complicated than the previous one.
548
553
Is the extra complexity worth it?
549
554
That depends on your circumstances, but here is one circumstance where
@@ -600,7 +605,7 @@ We also need to track how many groups we've opened, which we can do
600
605
with a simple counter, since an opened group's only effect is to change the
601
606
indentation level.
602
607
603
- The ` WithGroup ` implementation is a lot like the previous one: just remember the
608
+ This ` WithGroup ` is a lot like the previous one: it just remembers the
604
609
new group, which is unopened initially.
605
610
606
611
```
0 commit comments