@@ -9,11 +9,11 @@ This document is maintained by Jonathan Amsterdam `jba@google.com`.
99
10101 . [ Introduction] ( #introduction )
11111 . [ 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)
1717 1. [Testing](#testing)
18181 . [ General considerations] ( #general-considerations )
1919 1. [Copying records](#copying-records)
@@ -28,7 +28,7 @@ The standard library’s `log/slog` package has a two-part design.
2828A "frontend," implemented by the ` Logger ` type,
2929gathers structured log information like a message, level, and attributes,
3030and 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.
3232But you may need to write your own handler, and that is not always straightforward.
3333This guide is here to help.
3434
@@ -41,7 +41,7 @@ types work together.
4141Each logger contains a handler. Certain ` Logger ` methods do some preliminary work,
4242such as gathering key-value pairs into ` Attr ` s, and then call one or more
4343` Handler ` methods. These ` Logger ` methods are ` With ` , ` WithGroup ` ,
44- and the output methods.
44+ and the output methods like ` Info ` , ` Error ` and so on .
4545
4646An output method fulfills the main role of a logger: producing log output.
4747Here is a call to an output method:
@@ -107,6 +107,11 @@ so it will sometimes produce invalid YAML.
107107For example, it doesn't quote keys that have colons in them.
108108We'll call it ` IndentHandler ` to forestall disappointment.
109109
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+
110115We begin with the ` IndentHandler ` type
111116and the ` New ` function that constructs it from an ` io.Writer ` and options:
112117
@@ -439,7 +444,7 @@ Most of the fields of `IndentHandler` can be copied shallowly, but the slice of
439444the same underlying array. If we used ` append ` instead of making an explicit
440445copy, we would introduce that subtle aliasing bug.
441446
442- Using ` withGroupOrAttrs ` , the ` With ` methods are easy:
447+ The ` With ` methods are easy to write using ` withGroupOrAttrs ` :
443448
444449```
445450func (h *IndentHandler) WithGroup(name string) slog.Handler {
@@ -543,7 +548,7 @@ See [this bug report](https://go.dev/issue/61321) for more detail.
543548
544549### With pre-formatting
545550
546- Our second implementation implements pre-formatting.
551+ Our second version of the ` WithGroup ` and ` WithAttrs ` methods provides pre-formatting.
547552This implementation is more complicated than the previous one.
548553Is the extra complexity worth it?
549554That 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
600605with a simple counter, since an opened group's only effect is to change the
601606indentation level.
602607
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
604609new group, which is unopened initially.
605610
606611```
0 commit comments