File size: 1,012 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
Simplifying Logging
There's Gotta Be A Better Way
* Using a Single Value
Currently we pass an error, and a message in a lot of places.
if httpError(w, err, "was doing something") {
return
}
log.WithError(err).Errorln("was doing something")
Instead, we can just use `errors.Wrap`, which returns nil if err is nil.
if httpError(w, errors.Wrap(err, "do something")) {
return
}
* Adding Context
Having context when logging allows us to do things like:
- Enable debugging per-request, or per-subsystem
- Log a RequestID so we see all errors associated with a request
if httpError(ctx, w, errors.Wrap(err, "do something")) {
return
}
* A Simple Interface
The `httpError` example earlier, would call some type of logger.
Let's define what that could look like.
We care about 2 things:
- Logging things we always need to know (errors, requests)
- Logging things when debugging
So the simplest interface could be:
Log(ctx, err)
Debug(ctx, err)
* |