| 1 | |
| 2 | [](https://github.com/bep/logg/actions?query=workflow:Test) |
| 3 | [](https://goreportcard.com/report/github.com/bep/logg) |
| 4 | [](https://godoc.org/github.com/bep/logg) |
| 5 | |
| 6 | This is a fork of the exellent [Apex Log](https://github.com/apex/log) library. |
| 7 | |
| 8 | Main changes: |
| 9 | |
| 10 | * Trim unneeded dependencies. |
| 11 | * Make `Fields` into a slice to preserve log order. |
| 12 | * Split the old `Interface` in two and remove all but one `Log` method (see below). |
| 13 | * This allows for lazy creation of messages in `Log(fmt.Stringer)` and ignoring fields added in `LevelLogger`s with levels below the `Logger`s. |
| 14 | * The pointer passed to `HandleLog` is not safe to use outside of the current log chain, and needs to be cloned with `Clone` first if that's needed. |
| 15 | * See [Benchmarks](#benchmarks) for more info. |
| 16 | |
| 17 | This is probably the very fastest structured log library when logging is disabled: |
| 18 | |
| 19 | <img width="492" alt="image" src="https://user-images.githubusercontent.com/394382/184383985-265a4b0e-ee36-405e-9dc1-4686d0cad57a.png"> |
| 20 | |
| 21 | > One can never have enough log libraries! |
| 22 | |
| 23 | ```go |
| 24 | // Logger is the main interface for the logger. |
| 25 | type Logger interface { |
| 26 | // WithLevel returns a new entry with `level` set. |
| 27 | WithLevel(Level) *Entry |
| 28 | } |
| 29 | |
| 30 | // LevelLogger is the logger at a given level. |
| 31 | type LevelLogger interface { |
| 32 | // Log logs a message at the given level using the string from calling s.String(). |
| 33 | // Note that s.String() will not be called if the level is not enabled. |
| 34 | Log(s fmt.Stringer) |
| 35 | |
| 36 | // Logf logs a message at the given level using the format and args from calling fmt.Sprintf(). |
| 37 | // Note that fmt.Sprintf() will not be called if the level is not enabled. |
| 38 | Logf(format string, a ...any) |
| 39 | |
| 40 | // WithLevel returns a new entry with `level` set. |
| 41 | WithLevel(Level) *Entry |
| 42 | |
| 43 | // WithFields returns a new entry with the`fields` in fields set. |
| 44 | // This is a noop if LevelLogger's level is less than Logger's. |
| 45 | WithFields(fields Fielder) *Entry |
| 46 | |
| 47 | // WithLevel returns a new entry with the field f set with value v |
| 48 | // This is a noop if LevelLogger's level is less than Logger's. |
| 49 | WithField(f string, v any) *Entry |
| 50 | |
| 51 | // WithDuration returns a new entry with the "duration" field set |
| 52 | // to the given duration in milliseconds. |
| 53 | // This is a noop if LevelLogger's level is less than Logger's. |
| 54 | WithDuration(time.Duration) *Entry |
| 55 | |
| 56 | // WithError returns a new entry with the "error" set to `err`. |
| 57 | // This is a noop if err is nil or LevelLogger's level is less than Logger's. |
| 58 | WithError(error) *Entry |
| 59 | } |
| 60 | ``` |
| 61 | |
| 62 | ## Benchmarks |
| 63 | |
| 64 | Benchmarks below are borrowed and adapted from [Zap](https://github.com/uber-go/zap/tree/master/benchmarks). |
| 65 | |
| 66 | ### Logging at a disabled level without any structured context |
| 67 | |
| 68 | ``` |
| 69 | name time/op |
| 70 | DisabledWithoutFields/apex/log-10 33.9ns ± 0% |
| 71 | DisabledWithoutFields/bep/logg-10 0.28ns ± 0% |
| 72 | DisabledWithoutFields/sirupsen/logrus-10 6.54ns ± 0% |
| 73 | DisabledWithoutFields/rs/zerolog-10 0.31ns ± 0% |
| 74 | |
| 75 | name alloc/op |
| 76 | DisabledWithoutFields/apex/log-10 112B ± 0% |
| 77 | DisabledWithoutFields/bep/logg-10 0.00B |
| 78 | DisabledWithoutFields/sirupsen/logrus-10 16.0B ± 0% |
| 79 | DisabledWithoutFields/rs/zerolog-10 0.00B |
| 80 | |
| 81 | name allocs/op |
| 82 | DisabledWithoutFields/apex/log-10 1.00 ± 0% |
| 83 | DisabledWithoutFields/bep/logg-10 0.00 |
| 84 | DisabledWithoutFields/sirupsen/logrus-10 1.00 ± 0% |
| 85 | DisabledWithoutFields/rs/zerolog-10 0.00 |
| 86 | ``` |
| 87 | |
| 88 | |
| 89 | ### Logging at a disabled level with some accumulated context |
| 90 | |
| 91 | ``` |
| 92 | name time/op |
| 93 | DisabledAccumulatedContext/apex/log-10 0.29ns ± 0% |
| 94 | DisabledAccumulatedContext/bep/logg-10 0.27ns ± 0% |
| 95 | DisabledAccumulatedContext/sirupsen/logrus-10 6.61ns ± 0% |
| 96 | DisabledAccumulatedContext/rs/zerolog-10 0.32ns ± 0% |
| 97 | |
| 98 | name alloc/op |
| 99 | DisabledAccumulatedContext/apex/log-10 0.00B |
| 100 | DisabledAccumulatedContext/bep/logg-10 0.00B |
| 101 | DisabledAccumulatedContext/sirupsen/logrus-10 16.0B ± 0% |
| 102 | DisabledAccumulatedContext/rs/zerolog-10 0.00B |
| 103 | |
| 104 | name allocs/op |
| 105 | DisabledAccumulatedContext/apex/log-10 0.00 |
| 106 | DisabledAccumulatedContext/bep/logg-10 0.00 |
| 107 | DisabledAccumulatedContext/sirupsen/logrus-10 1.00 ± 0% |
| 108 | DisabledAccumulatedContext/rs/zerolog-10 0.00 |
| 109 | ``` |
| 110 | |
| 111 | ### Logging at a disabled level, adding context at each log site |
| 112 | |
| 113 | ``` |
| 114 | name time/op |
| 115 | DisabledAddingFields/apex/log-10 328ns ± 0% |
| 116 | DisabledAddingFields/bep/logg-10 0.38ns ± 0% |
| 117 | DisabledAddingFields/sirupsen/logrus-10 610ns ± 0% |
| 118 | DisabledAddingFields/rs/zerolog-10 10.5ns ± 0% |
| 119 | |
| 120 | name alloc/op |
| 121 | DisabledAddingFields/apex/log-10 886B ± 0% |
| 122 | DisabledAddingFields/bep/logg-10 0.00B |
| 123 | DisabledAddingFields/sirupsen/logrus-10 1.52kB ± 0% |
| 124 | DisabledAddingFields/rs/zerolog-10 24.0B ± 0% |
| 125 | |
| 126 | name allocs/op |
| 127 | DisabledAddingFields/apex/log-10 10.0 ± 0% |
| 128 | DisabledAddingFields/bep/logg-10 0.00 |
| 129 | DisabledAddingFields/sirupsen/logrus-10 12.0 ± 0% |
| 130 | DisabledAddingFields/rs/zerolog-10 1.00 ± 0% |
| 131 | ``` |
| 132 | |
| 133 | ### Logging without any structured context |
| 134 | |
| 135 | ``` |
| 136 | name time/op |
| 137 | WithoutFields/apex/log-10 964ns ± 0% |
| 138 | WithoutFields/bep/logg-10 100ns ± 0% |
| 139 | WithoutFields/go-kit/kit/log-10 232ns ± 0% |
| 140 | WithoutFields/inconshreveable/log15-10 2.13µs ± 0% |
| 141 | WithoutFields/sirupsen/logrus-10 866ns ± 0% |
| 142 | WithoutFields/stdlib.Println-10 7.08ns ± 0% |
| 143 | WithoutFields/stdlib.Printf-10 56.4ns ± 0% |
| 144 | WithoutFields/rs/zerolog-10 30.9ns ± 0% |
| 145 | WithoutFields/rs/zerolog.Formatting-10 1.33µs ± 0% |
| 146 | WithoutFields/rs/zerolog.Check-10 32.1ns ± 0% |
| 147 | |
| 148 | name alloc/op |
| 149 | WithoutFields/apex/log-10 352B ± 0% |
| 150 | WithoutFields/bep/logg-10 56.0B ± 0% |
| 151 | WithoutFields/go-kit/kit/log-10 520B ± 0% |
| 152 | WithoutFields/inconshreveable/log15-10 1.43kB ± 0% |
| 153 | WithoutFields/sirupsen/logrus-10 1.14kB ± 0% |
| 154 | WithoutFields/stdlib.Println-10 16.0B ± 0% |
| 155 | WithoutFields/stdlib.Printf-10 136B ± 0% |
| 156 | WithoutFields/rs/zerolog-10 0.00B |
| 157 | WithoutFields/rs/zerolog.Formatting-10 1.92kB ± 0% |
| 158 | WithoutFields/rs/zerolog.Check-10 0.00B |
| 159 | |
| 160 | name allocs/op |
| 161 | WithoutFields/apex/log-10 6.00 ± 0% |
| 162 | WithoutFields/bep/logg-10 2.00 ± 0% |
| 163 | WithoutFields/go-kit/kit/log-10 9.00 ± 0% |
| 164 | WithoutFields/inconshreveable/log15-10 20.0 ± 0% |
| 165 | WithoutFields/sirupsen/logrus-10 23.0 ± 0% |
| 166 | WithoutFields/stdlib.Println-10 1.00 ± 0% |
| 167 | WithoutFields/stdlib.Printf-10 6.00 ± 0% |
| 168 | WithoutFields/rs/zerolog-10 0.00 |
| 169 | WithoutFields/rs/zerolog.Formatting-10 58.0 ± 0% |
| 170 | WithoutFields/rs/zerolog.Check-10 0.00 |
| 171 | ``` |
| 172 | |
| 173 | |
| 174 | ### Logging with some accumulated context |
| 175 | |
| 176 | ``` |
| 177 | name time/op |
| 178 | AccumulatedContext/apex/log-10 12.7µs ± 0% |
| 179 | AccumulatedContext/bep/logg-10 1.52µs ± 0% |
| 180 | AccumulatedContext/go-kit/kit/log-10 2.52µs ± 0% |
| 181 | AccumulatedContext/inconshreveable/log15-10 9.36µs ± 0% |
| 182 | AccumulatedContext/sirupsen/logrus-10 3.41µs ± 0% |
| 183 | AccumulatedContext/rs/zerolog-10 37.9ns ± 0% |
| 184 | AccumulatedContext/rs/zerolog.Check-10 34.0ns ± 0% |
| 185 | AccumulatedContext/rs/zerolog.Formatting-10 1.36µs ± 0% |
| 186 | |
| 187 | name alloc/op |
| 188 | AccumulatedContext/apex/log-10 3.30kB ± 0% |
| 189 | AccumulatedContext/bep/logg-10 1.16kB ± 0% |
| 190 | AccumulatedContext/go-kit/kit/log-10 3.67kB ± 0% |
| 191 | AccumulatedContext/inconshreveable/log15-10 3.31kB ± 0% |
| 192 | AccumulatedContext/sirupsen/logrus-10 4.73kB ± 0% |
| 193 | AccumulatedContext/rs/zerolog-10 0.00B |
| 194 | AccumulatedContext/rs/zerolog.Check-10 0.00B |
| 195 | AccumulatedContext/rs/zerolog.Formatting-10 1.92kB ± 0% |
| 196 | |
| 197 | name allocs/op |
| 198 | AccumulatedContext/apex/log-10 53.0 ± 0% |
| 199 | AccumulatedContext/bep/logg-10 25.0 ± 0% |
| 200 | AccumulatedContext/go-kit/kit/log-10 56.0 ± 0% |
| 201 | AccumulatedContext/inconshreveable/log15-10 70.0 ± 0% |
| 202 | AccumulatedContext/sirupsen/logrus-10 68.0 ± 0% |
| 203 | AccumulatedContext/rs/zerolog-10 0.00 |
| 204 | AccumulatedContext/rs/zerolog.Check-10 0.00 |
| 205 | AccumulatedContext/rs/zerolog.Formatting-10 58.0 ± 0% |
| 206 | ``` |
| 207 | |
| 208 | |
| 209 | ## Logging with additional context at each log site |
| 210 | |
| 211 | ``` |
| 212 | name time/op |
| 213 | AddingFields/apex/log-10 13.2µs ± 0% |
| 214 | AddingFields/bep/logg-10 1.79µs ± 0% |
| 215 | AddingFields/go-kit/kit/log-10 2.23µs ± 0% |
| 216 | AddingFields/inconshreveable/log15-10 14.3µs ± 0% |
| 217 | AddingFields/sirupsen/logrus-10 4.46µs ± 0% |
| 218 | AddingFields/rs/zerolog-10 398ns ± 0% |
| 219 | AddingFields/rs/zerolog.Check-10 389ns ± 0% |
| 220 | |
| 221 | name alloc/op |
| 222 | AddingFields/apex/log-10 4.19kB ± 0% |
| 223 | AddingFields/bep/logg-10 2.02kB ± 0% |
| 224 | AddingFields/go-kit/kit/log-10 3.31kB ± 0% |
| 225 | AddingFields/inconshreveable/log15-10 6.68kB ± 0% |
| 226 | AddingFields/sirupsen/logrus-10 6.27kB ± 0% |
| 227 | AddingFields/rs/zerolog-10 24.0B ± 0% |
| 228 | AddingFields/rs/zerolog.Check-10 24.0B ± 0% |
| 229 | |
| 230 | name allocs/op |
| 231 | AddingFields/apex/log-10 63.0 ± 0% |
| 232 | AddingFields/bep/logg-10 34.0 ± 0% |
| 233 | AddingFields/go-kit/kit/log-10 57.0 ± 0% |
| 234 | AddingFields/inconshreveable/log15-10 74.0 ± 0% |
| 235 | AddingFields/sirupsen/logrus-10 79.0 ± 0% |
| 236 | AddingFields/rs/zerolog-10 1.00 ± 0% |
| 237 | AddingFields/rs/zerolog.Check-10 1.00 ± 0% |
| 238 | ``` |