Skip to content

Commit a50819e

Browse files
committed
Document implementation tradeoff and reason for choosing to favor a cheap WIth method at the expense of the Log method.
1 parent 803da74 commit a50819e

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

log/levels/levels.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ type Levels struct {
1010
ctx log.Context
1111
levelKey string
1212

13+
// We have a choice between storing level values in string fields or
14+
// making a separate context for each level. When using string fields the
15+
// Log method must combine the base context, the level data, and the
16+
// logged keyvals; but the With method only requires updating one context.
17+
// If we instead keep a separate context for each level the Log method
18+
// must only append the new keyvals; but the With method would have to
19+
// update all five contexts.
20+
21+
// Roughly speaking, storing multiple contexts breaks even if the ratio of
22+
// Log/With calls is more than the number of levels. We have chosen to
23+
// make the With method cheap and the Log method a bit more costly because
24+
// we do not expect most applications to Log more than five times for each
25+
// call to With.
26+
1327
debugValue string
1428
infoValue string
1529
warnValue string

0 commit comments

Comments
 (0)