Commit d6b395b
authored
Update lazy logger not to materialize unless it's being written to (#1519)
Lazy logger is frequently used to delay (expensive) logger
initialization unless we actually write to it.
This works for a pattern like:
```golang
logger := logger.With(.../*fields)
err := foo()
if err != nil {
logger.Info("foo: %w, err)
}
```
Another common pattern, though, is
```golang
logger := logger.With(.../*fields)
// a lot of code
logger.Debug("something happened") // only Info is enabled
```
Here, despite the logs being discarded, we still initialize the logger
even though it might be expensive.
This diff attempts to mitigate this by using the original core for level
checking. This, afaiu, should be safe as `With` is only for field
creation, and should not be changing log levels.
Preserving the original core to avoid race conditions (torn interfaces)
on actual `With` initialization.1 parent 4b9cea0 commit d6b395b
2 files changed
+48
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
35 | | - | |
36 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
41 | 43 | | |
42 | | - | |
| 44 | + | |
43 | 45 | | |
44 | 46 | | |
45 | 47 | | |
| |||
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
52 | 60 | | |
53 | 61 | | |
54 | 62 | | |
55 | 63 | | |
56 | 64 | | |
57 | | - | |
58 | | - | |
| 65 | + | |
| 66 | + | |
59 | 67 | | |
60 | 68 | | |
61 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| 68 | + | |
68 | 69 | | |
69 | 70 | | |
70 | 71 | | |
| |||
94 | 95 | | |
95 | 96 | | |
96 | 97 | | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
97 | 130 | | |
98 | 131 | | |
99 | 132 | | |
| |||
142 | 175 | | |
143 | 176 | | |
144 | 177 | | |
145 | | - | |
| 178 | + | |
146 | 179 | | |
147 | 180 | | |
148 | 181 | | |
| |||
0 commit comments