1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
10 "github.com/Sirupsen/logrus"
14 loggerCtxKey = new(int)
15 rootLogger = logrus.New()
18 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
20 // Context returns a new child context such that FromContext(child)
21 // returns the given logger.
22 func Context(ctx context.Context, logger *logrus.Entry) context.Context {
23 return context.WithValue(ctx, loggerCtxKey, logger)
26 // FromContext returns the logger suitable for the given context -- the one
27 // attached by contextWithLogger() if applicable, otherwise the
28 // top-level logger with no fields/values.
29 func FromContext(ctx context.Context) *logrus.Entry {
31 if logger, ok := ctx.Value(loggerCtxKey).(*logrus.Entry); ok {
35 return rootLogger.WithFields(nil)
38 // SetLevel sets the current logging level. See logrus for level
40 func SetLevel(level string) {
41 lvl, err := logrus.ParseLevel(level)
45 rootLogger.Level = lvl
48 // SetFormat sets the current logging format to "json" or "text".
49 func SetFormat(format string) {
52 rootLogger.Formatter = &logrus.TextFormatter{
54 TimestampFormat: rfc3339NanoFixed,
57 rootLogger.Formatter = &logrus.JSONFormatter{
58 TimestampFormat: rfc3339NanoFixed,
61 logrus.WithField("LogFormat", format).Fatal("unknown log format")