Bunyan's child loggers are handy for prepopulating context-specific log fields. We could use a similar pattern to tag each request—create a child logger with the request ID, then pass that logger to downstream functions.
For example:
func (h *EndpointHandler) UpdateHandler(resp http.ResponseWriter, req *http.Request) {
log := h.logger.Child(LogFields{"rid": req.Header.Get(HeaderID)})
// ...
if err := updateStore(uaid, chid, version, log); err != nil {
log.Warn("handlers_endpoint",
"This log message will be tagged with the request ID...",
LogFields{"error": err.Error()})
}
}
func updateStore(uaid, chid string, version int64, data string, log *SimpleLogger) error {
log.Info("storage", "...And so will this", nil)
return nil
}
Bunyan's child loggers are handy for prepopulating context-specific log fields. We could use a similar pattern to tag each request—create a child logger with the request ID, then pass that logger to downstream functions.
For example: