forked from golang/example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
slog-handler-guide: discuss Record.Clone
Change-Id: I55c6367da953b9a59c4d5cd8ca817d3c051e12de Reviewed-on: https://go-review.googlesource.com/c/example/+/513138 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Cottrell <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]>
- Loading branch information
Showing
2 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ This document is maintained by Jonathan Amsterdam `[email protected]`. | |
1. [The `WithGroup` method](#the-`withgroup`-method) | ||
1. [Testing](#testing) | ||
1. [General considerations](#general-considerations) | ||
1. [Copying records](#copying-records) | ||
1. [Concurrency safety](#concurrency-safety) | ||
1. [Robustness](#robustness) | ||
1. [Speed](#speed) | ||
|
@@ -766,7 +767,44 @@ in 65 lines. | |
|
||
# General considerations | ||
|
||
TODO(jba): reintroduce the material on Record.Clone that used to be here. | ||
## Copying records | ||
|
||
Most handlers won't need to copy the `slog.Record` that is passed | ||
to the `Handle` method. | ||
Those that do must take special care in some cases. | ||
|
||
A handler can make a single copy of a `Record` with an ordinary Go | ||
assignment, channel send or function call if it doesn't retain the | ||
original. | ||
But if its actions result in more than one copy, it should call `Record.Clone` | ||
to make the copies so that they don't share state. | ||
This `Handle` method passes the record to a single handler, so it doesn't require `Clone`: | ||
|
||
type Handler1 struct { | ||
h slog.Handler | ||
// ... | ||
} | ||
|
||
func (h *Handler1) Handle(ctx context.Context, r slog.Record) error { | ||
return h.h.Handle(ctx, r) | ||
} | ||
|
||
This `Handle` method might pass the record to more than one handler, so it | ||
should use `Clone`: | ||
|
||
type Handler2 struct { | ||
hs []slog.Handler | ||
// ... | ||
} | ||
|
||
func (h *Handler2) Handle(ctx context.Context, r slog.Record) error { | ||
for _, hh := range h.hs { | ||
if err := hh.Handle(ctx, r.Clone()); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
## Concurrency safety | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters