Document implementation tradeoff and reason for choosing to favor a cheap WIth method at the expense of the Log method.
Chris Hines
8 years ago
8 | 8 | type Levels struct { |
9 | 9 | ctx log.Context |
10 | 10 | levelKey string |
11 | ||
12 | // We have a choice between storing level values in string fields or | |
13 | // making a separate context for each level. When using string fields the | |
14 | // Log method must combine the base context, the level data, and the | |
15 | // logged keyvals; but the With method only requires updating one context. | |
16 | // If we instead keep a separate context for each level the Log method | |
17 | // must only append the new keyvals; but the With method would have to | |
18 | // update all five contexts. | |
19 | ||
20 | // Roughly speaking, storing multiple contexts breaks even if the ratio of | |
21 | // Log/With calls is more than the number of levels. We have chosen to | |
22 | // make the With method cheap and the Log method a bit more costly because | |
23 | // we do not expect most applications to Log more than five times for each | |
24 | // call to With. | |
11 | 25 | |
12 | 26 | debugValue string |
13 | 27 | infoValue string |