Allow logging formatter to be configured

This changeset simply adds hooks into the configuration system to support
multiple different kinds of output formats. These formatters are provided by
logrus and include options such as "text" and "json". The configuraiton
documentation has been updated accordingly.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2015-03-23 20:13:35 -07:00
parent 00e6b0d2b8
commit a75f0f26f7
4 changed files with 88 additions and 20 deletions

View file

@ -9,6 +9,7 @@ import (
"os"
log "github.com/Sirupsen/logrus"
"github.com/Sirupsen/logrus/formatters/logstash"
"github.com/bugsnag/bugsnag-go"
"github.com/docker/distribution/configuration"
ctxu "github.com/docker/distribution/context"
@ -49,7 +50,10 @@ func main() {
fatalf("configuration error: %v", err)
}
log.SetLevel(logLevel(config.Loglevel))
if err := configureLogging(config); err != nil {
fatalf("error configuring logger: %v", err)
}
ctx = context.WithValue(ctx, "version", version.Version)
ctx = ctxu.WithLogger(ctx, ctxu.GetLogger(ctx, "version"))
@ -111,16 +115,6 @@ func resolveConfiguration() (*configuration.Configuration, error) {
return config, nil
}
func logLevel(level configuration.Loglevel) log.Level {
l, err := log.ParseLevel(string(level))
if err != nil {
log.Warnf("error parsing level %q: %v", level, err)
l = log.InfoLevel
}
return l
}
func configureReporting(app *handlers.App) http.Handler {
var handler http.Handler = app
@ -157,6 +151,48 @@ func configureReporting(app *handlers.App) http.Handler {
return handler
}
// configureLogging the default logger using the configuration. Must be called
// before creating any contextual loggers.
func configureLogging(config *configuration.Configuration) error {
if config.Log.Level == "" && config.Log.Formatter == "" {
// If no config for logging is set, fallback to deprecated "Loglevel".
log.SetLevel(logLevel(config.Loglevel))
return nil
}
log.SetLevel(logLevel(config.Log.Level))
switch config.Log.Formatter {
case "json":
log.SetFormatter(&log.JSONFormatter{})
case "text":
log.SetFormatter(&log.TextFormatter{})
case "logstash":
log.SetFormatter(&logstash.LogstashFormatter{Type: "registry"})
default:
// just let the library use default on empty string.
if config.Log.Formatter != "" {
return fmt.Errorf("unsupported logging formatter: %q", config.Log.Formatter)
}
}
if config.Log.Formatter != "" {
log.Debugf("using %q logging formatter", config.Log.Formatter)
}
return nil
}
func logLevel(level configuration.Loglevel) log.Level {
l, err := log.ParseLevel(string(level))
if err != nil {
l = log.InfoLevel
log.Warnf("error parsing level %q: %v, using %q ", level, err, l)
}
return l
}
// debugServer starts the debug server with pprof, expvar among other
// endpoints. The addr should not be exposed externally. For most of these to
// work, tls cannot be enabled on the endpoint, so it is generally separate.