Skip to content

Commit f15fe1c

Browse files
committed
slog-handler-guide: handler example: Enabled method
Change-Id: I193980beda71ab8e20ccdd8a4eb0d72a9d2b32a0 Reviewed-on: https://go-review.googlesource.com/c/example/+/509956 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> Reviewed-by: Ian Cottrell <[email protected]>
1 parent f37d043 commit f15fe1c

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

slog-handler-guide/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,14 @@ A handler's `Enabled` method could report whether the argument level
174174
is greater than or equal to the context value, allowing the verbosity
175175
of the work done by each request to be controlled independently.
176176

177-
TODO(jba): include Enabled example
177+
Our `IndentHandler` doesn't use the context. It just compares the argument level
178+
with its configured minimum level:
179+
180+
```
181+
func (h *IndentHandler) Enabled(ctx context.Context, level slog.Level) bool {
182+
return level >= h.opts.Level.Level()
183+
}
184+
```
178185

179186
## The `Handle` method
180187

slog-handler-guide/guide.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ A handler's `Enabled` method could report whether the argument level
137137
is greater than or equal to the context value, allowing the verbosity
138138
of the work done by each request to be controlled independently.
139139

140-
TODO(jba): include Enabled example
140+
Our `IndentHandler` doesn't use the context. It just compares the argument level
141+
with its configured minimum level:
142+
143+
%include indenthandler1/indent_handler.go enabled -
141144

142145
## The `Handle` method
143146

slog-handler-guide/indenthandler1/indent_handler.go

+9
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ func New(out io.Writer, opts *Options) *IndentHandler {
4040

4141
//!-types
4242

43+
// !+enabled
4344
func (h *IndentHandler) Enabled(ctx context.Context, level slog.Level) bool {
4445
return level >= h.opts.Level.Level()
4546
}
4647

48+
//!-enabled
49+
4750
func (h *IndentHandler) WithGroup(name string) slog.Handler {
4851
// TODO: implement.
4952
return h
@@ -54,6 +57,7 @@ func (h *IndentHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
5457
return h
5558
}
5659

60+
// !+handle
5761
func (h *IndentHandler) Handle(ctx context.Context, r slog.Record) error {
5862
buf := make([]byte, 0, 1024)
5963
if !r.Time.IsZero() {
@@ -79,6 +83,9 @@ func (h *IndentHandler) Handle(ctx context.Context, r slog.Record) error {
7983
return err
8084
}
8185

86+
//!-handle
87+
88+
// !+appendAttr
8289
func (h *IndentHandler) appendAttr(buf []byte, a slog.Attr, indentLevel int) []byte {
8390
// Resolve the Attr's value before doing anything else.
8491
a.Value = a.Value.Resolve()
@@ -115,3 +122,5 @@ func (h *IndentHandler) appendAttr(buf []byte, a slog.Attr, indentLevel int) []b
115122
}
116123
return buf
117124
}
125+
126+
//!-appendAttr

0 commit comments

Comments
 (0)