Skip to content

Commit f8bc3a0

Browse files
bitluxgopherbot
authored andcommitted
example/slog-handler-guide: run make
This change is the result of running `make` to produce README.md from guide.md. It appears that the last time `make` was run was 2024-09-06 for 4e46ff5. I confirmed that all diffs to README.md were deliberately introduced to guide.md (in 398e112 and 40afcb7). In addition, one change to README.md (32022ca) was backported to guide.md. Change-Id: I6a27b8d128630f7f7e5799ad963fd96fb33ac3df Reviewed-on: https://go-review.googlesource.com/c/example/+/668075 Reviewed-by: Junyang Shao <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Jonathan Amsterdam <[email protected]>
1 parent 24b880c commit f8bc3a0

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

slog-handler-guide/README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ This document is maintained by Jonathan Amsterdam `[email protected]`.
99

1010
1. [Introduction](#introduction)
1111
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)
1717
1. [Testing](#testing)
1818
1. [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.
2828
A "frontend," implemented by the `Logger` type,
2929
gathers structured log information like a message, level, and attributes,
3030
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.
3232
But you may need to write your own handler, and that is not always straightforward.
3333
This guide is here to help.
3434

@@ -41,7 +41,7 @@ types work together.
4141
Each logger contains a handler. Certain `Logger` methods do some preliminary work,
4242
such 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

4646
An output method fulfills the main role of a logger: producing log output.
4747
Here is a call to an output method:
@@ -107,6 +107,11 @@ so it will sometimes produce invalid YAML.
107107
For example, it doesn't quote keys that have colons in them.
108108
We'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+
110115
We begin with the `IndentHandler` type
111116
and 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
439444
the same underlying array. If we used `append` instead of making an explicit
440445
copy, 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
```
445450
func (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.
547552
This implementation is more complicated than the previous one.
548553
Is the extra complexity worth it?
549554
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
600605
with a simple counter, since an opened group's only effect is to change the
601606
indentation 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
604609
new group, which is unopened initially.
605610

606611
```

slog-handler-guide/guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ original remains unchanged.
5454
All subsequent output from `logger` will include those attributes.
5555
A logger's `With` method calls its handler's `WithAttrs` method.
5656

57-
The `WithGroup` method is used to avoid avoid key collisions in large programs
57+
The `WithGroup` method is used to avoid key collisions in large programs
5858
by establishing separate namespaces.
5959
This call creates a new `Logger` value with a group named "g":
6060

0 commit comments

Comments
 (0)