|
| 1 | +package log |
| 2 | + |
| 3 | +// Wrapper package around logrus whose main purpose is to support having |
| 4 | +// different output streams for error and non-error messages. |
| 5 | +// |
| 6 | +// Does not wrap every method of logrus package. If you need direct access, |
| 7 | +// use log.Log() to get the actual logrus logger object. |
| 8 | +// |
| 9 | +// It might seem redundant, but we really want the different output streams. |
| 10 | + |
| 11 | +import ( |
| 12 | + "fmt" |
| 13 | + "io" |
| 14 | + "os" |
| 15 | + "strings" |
| 16 | + "sync" |
| 17 | + |
| 18 | + logger "github.com/sirupsen/logrus" |
| 19 | +) |
| 20 | + |
| 21 | +// LogContext contains a structured context for logging |
| 22 | +type LogContext struct { |
| 23 | + fields logger.Fields |
| 24 | + normalOut io.Writer |
| 25 | + errorOut io.Writer |
| 26 | + mutex sync.RWMutex |
| 27 | +} |
| 28 | + |
| 29 | +// NewContext returns a LogContext with default settings |
| 30 | +func NewContext() *LogContext { |
| 31 | + var logctx LogContext |
| 32 | + logctx.fields = make(logger.Fields) |
| 33 | + logctx.normalOut = os.Stdout |
| 34 | + logctx.errorOut = os.Stderr |
| 35 | + return &logctx |
| 36 | +} |
| 37 | + |
| 38 | +// SetLogLevel sets the log level to use for the logger |
| 39 | +func SetLogLevel(logLevel string) error { |
| 40 | + switch strings.ToLower(logLevel) { |
| 41 | + case "trace": |
| 42 | + logger.SetLevel(logger.TraceLevel) |
| 43 | + case "debug": |
| 44 | + logger.SetLevel(logger.DebugLevel) |
| 45 | + case "info": |
| 46 | + logger.SetLevel(logger.InfoLevel) |
| 47 | + case "warn": |
| 48 | + logger.SetLevel(logger.WarnLevel) |
| 49 | + case "error": |
| 50 | + logger.SetLevel(logger.ErrorLevel) |
| 51 | + default: |
| 52 | + return fmt.Errorf("invalid loglevel: %s", logLevel) |
| 53 | + } |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +// WithContext is an alias for NewContext |
| 58 | +func WithContext() *LogContext { |
| 59 | + return NewContext() |
| 60 | +} |
| 61 | + |
| 62 | +// AddField adds a structured field to logctx |
| 63 | +func (logctx *LogContext) AddField(key string, value interface{}) *LogContext { |
| 64 | + logctx.mutex.Lock() |
| 65 | + logctx.fields[key] = value |
| 66 | + logctx.mutex.Unlock() |
| 67 | + return logctx |
| 68 | +} |
| 69 | + |
| 70 | +// Tracef logs a debug message for logctx to stdout |
| 71 | +func (logctx *LogContext) Tracef(format string, args ...interface{}) { |
| 72 | + logger.SetOutput(logctx.normalOut) |
| 73 | + if logctx.fields != nil && len(logctx.fields) > 0 { |
| 74 | + logger.WithFields(logctx.fields).Tracef(format, args...) |
| 75 | + } else { |
| 76 | + logger.Tracef(format, args...) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Debugf logs a debug message for logctx to stdout |
| 81 | +func (logctx *LogContext) Debugf(format string, args ...interface{}) { |
| 82 | + logger.SetOutput(logctx.normalOut) |
| 83 | + if logctx.fields != nil && len(logctx.fields) > 0 { |
| 84 | + logger.WithFields(logctx.fields).Debugf(format, args...) |
| 85 | + } else { |
| 86 | + logger.Debugf(format, args...) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// Infof logs an informational message for logctx to stdout |
| 91 | +func (logctx *LogContext) Infof(format string, args ...interface{}) { |
| 92 | + logger.SetOutput(logctx.normalOut) |
| 93 | + if logctx.fields != nil && len(logctx.fields) > 0 { |
| 94 | + logger.WithFields(logctx.fields).Infof(format, args...) |
| 95 | + } else { |
| 96 | + logger.Infof(format, args...) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// Warnf logs a warning message for logctx to stdout |
| 101 | +func (logctx *LogContext) Warnf(format string, args ...interface{}) { |
| 102 | + logger.SetOutput(logctx.normalOut) |
| 103 | + if logctx.fields != nil && len(logctx.fields) > 0 { |
| 104 | + logger.WithFields(logctx.fields).Warnf(format, args...) |
| 105 | + } else { |
| 106 | + logger.Warnf(format, args...) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Errorf logs a non-fatal error message for logctx to stdout |
| 111 | +func (logctx *LogContext) Errorf(format string, args ...interface{}) { |
| 112 | + logger.SetOutput(logctx.errorOut) |
| 113 | + if logctx.fields != nil && len(logctx.fields) > 0 { |
| 114 | + logger.WithFields(logctx.fields).Errorf(format, args...) |
| 115 | + } else { |
| 116 | + logger.Errorf(format, args...) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// Fatalf logs a fatal error message for logctx to stdout |
| 121 | +func (logctx *LogContext) Fatalf(format string, args ...interface{}) { |
| 122 | + logger.SetOutput(logctx.errorOut) |
| 123 | + if logctx.fields != nil && len(logctx.fields) > 0 { |
| 124 | + logger.WithFields(logctx.fields).Fatalf(format, args...) |
| 125 | + } else { |
| 126 | + logger.Fatalf(format, args...) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// Debugf logs a warning message without context to stdout |
| 131 | +func Tracef(format string, args ...interface{}) { |
| 132 | + logCtx := NewContext() |
| 133 | + logCtx.Tracef(format, args...) |
| 134 | +} |
| 135 | + |
| 136 | +// Debugf logs a warning message without context to stdout |
| 137 | +func Debugf(format string, args ...interface{}) { |
| 138 | + logCtx := NewContext() |
| 139 | + logCtx.Debugf(format, args...) |
| 140 | +} |
| 141 | + |
| 142 | +// Infof logs a warning message without context to stdout |
| 143 | +func Infof(format string, args ...interface{}) { |
| 144 | + logCtx := NewContext() |
| 145 | + logCtx.Infof(format, args...) |
| 146 | +} |
| 147 | + |
| 148 | +// Warnf logs a warning message without context to stdout |
| 149 | +func Warnf(format string, args ...interface{}) { |
| 150 | + logCtx := NewContext() |
| 151 | + logCtx.Warnf(format, args...) |
| 152 | +} |
| 153 | + |
| 154 | +// Errorf logs an error message without context to stderr |
| 155 | +func Errorf(format string, args ...interface{}) { |
| 156 | + logCtx := NewContext() |
| 157 | + logCtx.Errorf(format, args...) |
| 158 | +} |
| 159 | + |
| 160 | +// Fatalf logs a non-recoverable error message without context to stderr |
| 161 | +func Fatalf(format string, args ...interface{}) { |
| 162 | + logCtx := NewContext() |
| 163 | + logCtx.Fatalf(format, args...) |
| 164 | +} |
| 165 | + |
| 166 | +func disableLogColors() bool { |
| 167 | + return strings.ToLower(os.Getenv("ENABLE_LOG_COLORS")) == "false" |
| 168 | +} |
| 169 | + |
| 170 | +// Initializes the logging subsystem with default values |
| 171 | +func init() { |
| 172 | + logger.SetFormatter(&logger.TextFormatter{ |
| 173 | + DisableColors: disableLogColors(), |
| 174 | + FullTimestamp: true, |
| 175 | + }) |
| 176 | + logger.SetLevel(logger.DebugLevel) |
| 177 | +} |
0 commit comments