6 "github.com/Sirupsen/logrus"
10 loggerCtxKey = new(int)
11 rootLogger = logrus.New()
14 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
16 // Context returns a new child context such that FromContext(child)
17 // returns the given logger.
18 func Context(ctx context.Context, logger *logrus.Entry) context.Context {
19 return context.WithValue(ctx, loggerCtxKey, logger)
22 // FromContext 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 FromContext(ctx context.Context) *logrus.Entry {
27 if logger, ok := ctx.Value(loggerCtxKey).(*logrus.Entry); ok {
31 return rootLogger.WithFields(nil)
34 // SetLevel sets the current logging level. See logrus for level
36 func SetLevel(level string) {
37 lvl, err := logrus.ParseLevel(level)
41 rootLogger.Level = lvl
44 // SetFormat sets the current logging format to "json" or "text".
45 func SetFormat(format string) {
48 rootLogger.Formatter = &logrus.TextFormatter{
50 TimestampFormat: rfc3339NanoFixed,
53 rootLogger.Formatter = &logrus.JSONFormatter{
54 TimestampFormat: rfc3339NanoFixed,
57 logrus.WithField("LogFormat", format).Fatal("unknown log format")