6 "github.com/Sirupsen/logrus"
10 loggerCtxKey = new(int)
11 rootLogger = logrus.New()
14 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
16 // contextWithLogger returns a new child context such that
17 // logger(child) returns the given logger.
18 func contextWithLogger(ctx context.Context, logger *logrus.Entry) context.Context {
19 return context.WithValue(ctx, loggerCtxKey, logger)
22 // logger returns the logger suitable for the given context -- the one
23 // attached by contextWithLogger() if applicable, otherwise the
24 // top-level logger with no fields/values.
25 func logger(ctx context.Context) *logrus.Entry {
27 if logger, ok := ctx.Value(loggerCtxKey).(*logrus.Entry); ok {
31 return rootLogger.WithFields(nil)
34 // loggerConfig sets up logging to behave as configured.
35 func loggerConfig(cfg Config) {
36 lvl, err := logrus.ParseLevel(cfg.LogLevel)
40 rootLogger.Level = lvl
41 switch cfg.LogFormat {
43 rootLogger.Formatter = &logrus.TextFormatter{
45 TimestampFormat: rfc3339NanoFixed,
48 rootLogger.Formatter = &logrus.JSONFormatter{
49 TimestampFormat: rfc3339NanoFixed,
52 logrus.WithField("LogFormat", cfg.LogFormat).Fatal("unknown log format")