Updated logging API
Requires: !25 (merged)
As part of gitlab-workhorse!402 (merged), I got to use this API for the first time, and found that having to add multiple package imports is clunky.
The previous solution was to wrap logrus.Fields in our own type, but this has performance implications as each log line needs to be wrapped and unwrapped.
In this change I've implemented an elegant solution which doesn't need multiple package imports in client code and also doesn't need wrapping/unwrapping.
The solution is to setup a type alias from gitlab.com/gitlab-org/labkit/log.Fields to logrus.Fields.
With this, the usage is much more fluent, and we don't need unnecessary wrapping of log entries.
This example reflects this new approach:
package log_test
import (
"gitlab.com/gitlab-org/labkit/log"
)
func ExampleInitialize() {
// Initialize the global logger
closer, err := log.Initialize(
log.WithFormatter("json"), // Use JSON formatting
log.WithLogLevel("info"), // Use info level
log.WithOutputName("stderr"), // Output to stderr
)
defer closer.Close()
log.WithError(err).Info("This has been logged")
log.WithFields(log.Fields{"field": "123"}).Info("Another log line");
}
Edited by Andrew Newdigate