Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Girish Kumar | 950f21e | 2020-08-19 17:42:29 +0000 | [diff] [blame] | 17 | // Package log provides a structured Logger interface implemented using zap logger. It provides the following capabilities: |
| 18 | // 1. Package level logging - a go package can register itself (AddPackage) and have a logger created for that package. |
| 19 | // 2. Dynamic log level change - for all registered packages (SetAllLogLevel) |
| 20 | // 3. Dynamic log level change - for a given package (SetPackageLogLevel) |
| 21 | // 4. Provides a default logger for unregistered packages (however avoid its usage) |
| 22 | // 5. Allow key-value pairs to be added to a logger(UpdateLogger) or all loggers (UpdateAllLoggers) at run time |
| 23 | // 6. Add to the log output the location where the log was invoked (filename.functionname.linenumber) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 24 | // |
| 25 | // Using package-level logging (recommended approach). In the examples below, log refers to this log package. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 26 | // |
Girish Kumar | 950f21e | 2020-08-19 17:42:29 +0000 | [diff] [blame] | 27 | // 1. In the appropriate package, add the following in the init section of the package (usually in a common.go file) |
| 28 | // The log level can be changed and any number of default fields can be added as well. The log level specifies |
| 29 | // the lowest log level that will be in the output while the fields will be automatically added to all log printouts. |
| 30 | // However, as voltha components re-initialize the log level of each registered package to default initial loglevel |
| 31 | // passed as CLI argument, the log level passed in RegisterPackage call effectively has no effect. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 32 | // |
Girish Kumar | 950f21e | 2020-08-19 17:42:29 +0000 | [diff] [blame] | 33 | // var logger log.CLogger |
| 34 | // func init() { |
| 35 | // logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"key1": "value1"}) |
| 36 | // } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 37 | // |
Girish Kumar | 950f21e | 2020-08-19 17:42:29 +0000 | [diff] [blame] | 38 | // 2. In the calling package, use any of the publicly available functions of local package-level logger instance created |
| 39 | // in previous step. Here is an example to write an Info log with additional fields: |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 40 | // |
Girish Kumar | 950f21e | 2020-08-19 17:42:29 +0000 | [diff] [blame] | 41 | // logger.Infow("An example", mylog.Fields{"myStringOutput": "output", "myIntOutput": 2}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 42 | // |
Girish Kumar | 950f21e | 2020-08-19 17:42:29 +0000 | [diff] [blame] | 43 | // 3. To dynamically change the log level, you can use |
| 44 | // a) SetLogLevel from inside your package or |
| 45 | // b) SetPackageLogLevel from anywhere or |
| 46 | // c) SetAllLogLevel from anywhere. |
| 47 | // |
| 48 | // Dynamic Loglevel configuration feature also uses SetPackageLogLevel method based on triggers received due to |
| 49 | // Changes to configured loglevels |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 50 | |
| 51 | package log |
| 52 | |
| 53 | import ( |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 54 | "context" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 55 | "errors" |
| 56 | "fmt" |
| 57 | zp "go.uber.org/zap" |
| 58 | zc "go.uber.org/zap/zapcore" |
| 59 | "path" |
| 60 | "runtime" |
| 61 | "strings" |
| 62 | ) |
| 63 | |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 64 | type LogLevel int8 |
| 65 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 66 | const ( |
| 67 | // DebugLevel logs a message at debug level |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 68 | DebugLevel = LogLevel(iota) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 69 | // InfoLevel logs a message at info level |
| 70 | InfoLevel |
| 71 | // WarnLevel logs a message at warning level |
| 72 | WarnLevel |
| 73 | // ErrorLevel logs a message at error level |
| 74 | ErrorLevel |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 75 | // FatalLevel logs a message, then calls os.Exit(1). |
| 76 | FatalLevel |
| 77 | ) |
| 78 | |
| 79 | // CONSOLE formats the log for the console, mostly used during development |
| 80 | const CONSOLE = "console" |
| 81 | |
| 82 | // JSON formats the log using json format, mostly used by an automated logging system consumption |
| 83 | const JSON = "json" |
| 84 | |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 85 | // Context Aware Logger represents an abstract logging interface. Any logging implementation used |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 86 | // will need to abide by this interface |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 87 | type CLogger interface { |
| 88 | Debug(context.Context, ...interface{}) |
| 89 | Debugln(context.Context, ...interface{}) |
| 90 | Debugf(context.Context, string, ...interface{}) |
| 91 | Debugw(context.Context, string, Fields) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 92 | |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 93 | Info(context.Context, ...interface{}) |
| 94 | Infoln(context.Context, ...interface{}) |
| 95 | Infof(context.Context, string, ...interface{}) |
| 96 | Infow(context.Context, string, Fields) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 97 | |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 98 | Warn(context.Context, ...interface{}) |
| 99 | Warnln(context.Context, ...interface{}) |
| 100 | Warnf(context.Context, string, ...interface{}) |
| 101 | Warnw(context.Context, string, Fields) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 102 | |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 103 | Error(context.Context, ...interface{}) |
| 104 | Errorln(context.Context, ...interface{}) |
| 105 | Errorf(context.Context, string, ...interface{}) |
| 106 | Errorw(context.Context, string, Fields) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 107 | |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 108 | Fatal(context.Context, ...interface{}) |
| 109 | Fatalln(context.Context, ...interface{}) |
| 110 | Fatalf(context.Context, string, ...interface{}) |
| 111 | Fatalw(context.Context, string, Fields) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 112 | |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 113 | With(Fields) CLogger |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 114 | |
| 115 | // The following are added to be able to use this logger as a gRPC LoggerV2 if needed |
| 116 | // |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 117 | Warning(context.Context, ...interface{}) |
| 118 | Warningln(context.Context, ...interface{}) |
| 119 | Warningf(context.Context, string, ...interface{}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 120 | |
| 121 | // V reports whether verbosity level l is at least the requested verbose level. |
Scott Baker | d7c25f4 | 2020-02-21 08:12:06 -0800 | [diff] [blame] | 122 | V(l LogLevel) bool |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 123 | |
| 124 | //Returns the log level of this specific logger |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 125 | GetLogLevel() LogLevel |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Fields is used as key-value pairs for structured logging |
| 129 | type Fields map[string]interface{} |
| 130 | |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 131 | var defaultLogger *clogger |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 132 | var cfg zp.Config |
| 133 | |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 134 | var loggers map[string]*clogger |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 135 | var cfgs map[string]zp.Config |
| 136 | |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 137 | type clogger struct { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 138 | log *zp.SugaredLogger |
| 139 | parent *zp.Logger |
| 140 | packageName string |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 143 | func logLevelToAtomicLevel(l LogLevel) zp.AtomicLevel { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 144 | switch l { |
| 145 | case DebugLevel: |
| 146 | return zp.NewAtomicLevelAt(zc.DebugLevel) |
| 147 | case InfoLevel: |
| 148 | return zp.NewAtomicLevelAt(zc.InfoLevel) |
| 149 | case WarnLevel: |
| 150 | return zp.NewAtomicLevelAt(zc.WarnLevel) |
| 151 | case ErrorLevel: |
| 152 | return zp.NewAtomicLevelAt(zc.ErrorLevel) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 153 | case FatalLevel: |
| 154 | return zp.NewAtomicLevelAt(zc.FatalLevel) |
| 155 | } |
| 156 | return zp.NewAtomicLevelAt(zc.ErrorLevel) |
| 157 | } |
| 158 | |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 159 | func logLevelToLevel(l LogLevel) zc.Level { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 160 | switch l { |
| 161 | case DebugLevel: |
| 162 | return zc.DebugLevel |
| 163 | case InfoLevel: |
| 164 | return zc.InfoLevel |
| 165 | case WarnLevel: |
| 166 | return zc.WarnLevel |
| 167 | case ErrorLevel: |
| 168 | return zc.ErrorLevel |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 169 | case FatalLevel: |
| 170 | return zc.FatalLevel |
| 171 | } |
| 172 | return zc.ErrorLevel |
| 173 | } |
| 174 | |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 175 | func levelToLogLevel(l zc.Level) LogLevel { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 176 | switch l { |
| 177 | case zc.DebugLevel: |
| 178 | return DebugLevel |
| 179 | case zc.InfoLevel: |
| 180 | return InfoLevel |
| 181 | case zc.WarnLevel: |
| 182 | return WarnLevel |
| 183 | case zc.ErrorLevel: |
| 184 | return ErrorLevel |
Rohan Agrawal | 6a99a45 | 2020-01-14 07:58:25 +0000 | [diff] [blame] | 185 | case zc.FatalLevel: |
| 186 | return FatalLevel |
| 187 | } |
| 188 | return ErrorLevel |
| 189 | } |
| 190 | |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 191 | func StringToLogLevel(l string) (LogLevel, error) { |
| 192 | switch strings.ToUpper(l) { |
Rohan Agrawal | 6a99a45 | 2020-01-14 07:58:25 +0000 | [diff] [blame] | 193 | case "DEBUG": |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 194 | return DebugLevel, nil |
Rohan Agrawal | 6a99a45 | 2020-01-14 07:58:25 +0000 | [diff] [blame] | 195 | case "INFO": |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 196 | return InfoLevel, nil |
Rohan Agrawal | 6a99a45 | 2020-01-14 07:58:25 +0000 | [diff] [blame] | 197 | case "WARN": |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 198 | return WarnLevel, nil |
Rohan Agrawal | 6a99a45 | 2020-01-14 07:58:25 +0000 | [diff] [blame] | 199 | case "ERROR": |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 200 | return ErrorLevel, nil |
Rohan Agrawal | 6a99a45 | 2020-01-14 07:58:25 +0000 | [diff] [blame] | 201 | case "FATAL": |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 202 | return FatalLevel, nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 203 | } |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 204 | return 0, errors.New("Given LogLevel is invalid : " + l) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 205 | } |
| 206 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 207 | func LogLevelToString(l LogLevel) (string, error) { |
| 208 | switch l { |
| 209 | case DebugLevel: |
| 210 | return "DEBUG", nil |
| 211 | case InfoLevel: |
| 212 | return "INFO", nil |
| 213 | case WarnLevel: |
| 214 | return "WARN", nil |
| 215 | case ErrorLevel: |
| 216 | return "ERROR", nil |
| 217 | case FatalLevel: |
| 218 | return "FATAL", nil |
| 219 | } |
| 220 | return "", errors.New("Given LogLevel is invalid " + string(l)) |
| 221 | } |
| 222 | |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 223 | func getDefaultConfig(outputType string, level LogLevel, defaultFields Fields) zp.Config { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 224 | return zp.Config{ |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 225 | Level: logLevelToAtomicLevel(level), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 226 | Encoding: outputType, |
| 227 | Development: true, |
| 228 | OutputPaths: []string{"stdout"}, |
| 229 | ErrorOutputPaths: []string{"stderr"}, |
| 230 | InitialFields: defaultFields, |
| 231 | EncoderConfig: zc.EncoderConfig{ |
| 232 | LevelKey: "level", |
| 233 | MessageKey: "msg", |
| 234 | TimeKey: "ts", |
Girish Kumar | c9b0e71 | 2020-02-27 17:50:52 +0000 | [diff] [blame] | 235 | CallerKey: "caller", |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 236 | StacktraceKey: "stacktrace", |
| 237 | LineEnding: zc.DefaultLineEnding, |
| 238 | EncodeLevel: zc.LowercaseLevelEncoder, |
| 239 | EncodeTime: zc.ISO8601TimeEncoder, |
| 240 | EncodeDuration: zc.SecondsDurationEncoder, |
| 241 | EncodeCaller: zc.ShortCallerEncoder, |
| 242 | }, |
| 243 | } |
| 244 | } |
| 245 | |
Rohan Agrawal | ee87e64 | 2020-04-14 10:22:18 +0000 | [diff] [blame] | 246 | func ConstructZapConfig(outputType string, level LogLevel, fields Fields) zp.Config { |
| 247 | return getDefaultConfig(outputType, level, fields) |
| 248 | } |
| 249 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 250 | // SetLogger needs to be invoked before the logger API can be invoked. This function |
| 251 | // initialize the default logger (zap's sugaredlogger) |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 252 | func SetDefaultLogger(outputType string, level LogLevel, defaultFields Fields) (CLogger, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 253 | // Build a custom config using zap |
| 254 | cfg = getDefaultConfig(outputType, level, defaultFields) |
| 255 | |
Girish Kumar | c9b0e71 | 2020-02-27 17:50:52 +0000 | [diff] [blame] | 256 | l, err := cfg.Build(zp.AddCallerSkip(1)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 257 | if err != nil { |
| 258 | return nil, err |
| 259 | } |
| 260 | |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 261 | defaultLogger = &clogger{ |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 262 | log: l.Sugar(), |
| 263 | parent: l, |
| 264 | } |
| 265 | |
| 266 | return defaultLogger, nil |
| 267 | } |
| 268 | |
| 269 | // AddPackage registers a package to the log map. Each package gets its own logger which allows |
| 270 | // its config (loglevel) to be changed dynamically without interacting with the other packages. |
| 271 | // outputType is JSON, level is the lowest level log to output with this logger and defaultFields is a map of |
| 272 | // key-value pairs to always add to the output. |
| 273 | // Note: AddPackage also returns a reference to the actual logger. If a calling package uses this reference directly |
| 274 | //instead of using the publicly available functions in this log package then a number of functionalities will not |
| 275 | // be available to it, notably log tracing with filename.functionname.linenumber annotation. |
| 276 | // |
| 277 | // pkgNames parameter should be used for testing only as this function detects the caller's package. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 278 | func RegisterPackage(outputType string, level LogLevel, defaultFields Fields, pkgNames ...string) (CLogger, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 279 | if cfgs == nil { |
| 280 | cfgs = make(map[string]zp.Config) |
| 281 | } |
| 282 | if loggers == nil { |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 283 | loggers = make(map[string]*clogger) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | var pkgName string |
| 287 | for _, name := range pkgNames { |
| 288 | pkgName = name |
| 289 | break |
| 290 | } |
| 291 | if pkgName == "" { |
| 292 | pkgName, _, _, _ = getCallerInfo() |
| 293 | } |
| 294 | |
| 295 | if _, exist := loggers[pkgName]; exist { |
| 296 | return loggers[pkgName], nil |
| 297 | } |
| 298 | |
| 299 | cfgs[pkgName] = getDefaultConfig(outputType, level, defaultFields) |
| 300 | |
Girish Kumar | c9b0e71 | 2020-02-27 17:50:52 +0000 | [diff] [blame] | 301 | l, err := cfgs[pkgName].Build(zp.AddCallerSkip(1)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 302 | if err != nil { |
| 303 | return nil, err |
| 304 | } |
| 305 | |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 306 | loggers[pkgName] = &clogger{ |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 307 | log: l.Sugar(), |
| 308 | parent: l, |
| 309 | packageName: pkgName, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 310 | } |
| 311 | return loggers[pkgName], nil |
| 312 | } |
| 313 | |
| 314 | //UpdateAllLoggers create new loggers for all registered pacakges with the defaultFields. |
| 315 | func UpdateAllLoggers(defaultFields Fields) error { |
| 316 | for pkgName, cfg := range cfgs { |
| 317 | for k, v := range defaultFields { |
| 318 | if cfg.InitialFields == nil { |
| 319 | cfg.InitialFields = make(map[string]interface{}) |
| 320 | } |
| 321 | cfg.InitialFields[k] = v |
| 322 | } |
Girish Kumar | c9b0e71 | 2020-02-27 17:50:52 +0000 | [diff] [blame] | 323 | l, err := cfg.Build(zp.AddCallerSkip(1)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 324 | if err != nil { |
| 325 | return err |
| 326 | } |
| 327 | |
Girish Kumar | c9b0e71 | 2020-02-27 17:50:52 +0000 | [diff] [blame] | 328 | // Update the existing zap logger instance |
| 329 | loggers[pkgName].log = l.Sugar() |
| 330 | loggers[pkgName].parent = l |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 331 | } |
| 332 | return nil |
| 333 | } |
| 334 | |
| 335 | // Return a list of all packages that have individually-configured loggers |
| 336 | func GetPackageNames() []string { |
| 337 | i := 0 |
| 338 | keys := make([]string, len(loggers)) |
| 339 | for k := range loggers { |
| 340 | keys[i] = k |
| 341 | i++ |
| 342 | } |
| 343 | return keys |
| 344 | } |
| 345 | |
Girish Kumar | c9b0e71 | 2020-02-27 17:50:52 +0000 | [diff] [blame] | 346 | // UpdateLogger updates the logger associated with a caller's package with supplied defaultFields |
| 347 | func UpdateLogger(defaultFields Fields) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 348 | pkgName, _, _, _ := getCallerInfo() |
| 349 | if _, exist := loggers[pkgName]; !exist { |
Girish Kumar | c9b0e71 | 2020-02-27 17:50:52 +0000 | [diff] [blame] | 350 | return fmt.Errorf("package-%s-not-registered", pkgName) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | // Build a new logger |
| 354 | if _, exist := cfgs[pkgName]; !exist { |
Girish Kumar | c9b0e71 | 2020-02-27 17:50:52 +0000 | [diff] [blame] | 355 | return fmt.Errorf("config-%s-not-registered", pkgName) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | cfg := cfgs[pkgName] |
| 359 | for k, v := range defaultFields { |
| 360 | if cfg.InitialFields == nil { |
| 361 | cfg.InitialFields = make(map[string]interface{}) |
| 362 | } |
| 363 | cfg.InitialFields[k] = v |
| 364 | } |
Girish Kumar | c9b0e71 | 2020-02-27 17:50:52 +0000 | [diff] [blame] | 365 | l, err := cfg.Build(zp.AddCallerSkip(1)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 366 | if err != nil { |
Girish Kumar | c9b0e71 | 2020-02-27 17:50:52 +0000 | [diff] [blame] | 367 | return err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Girish Kumar | c9b0e71 | 2020-02-27 17:50:52 +0000 | [diff] [blame] | 370 | // Update the existing zap logger instance |
| 371 | loggers[pkgName].log = l.Sugar() |
| 372 | loggers[pkgName].parent = l |
| 373 | |
| 374 | return nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 377 | func setLevel(cfg zp.Config, level LogLevel) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 378 | switch level { |
| 379 | case DebugLevel: |
| 380 | cfg.Level.SetLevel(zc.DebugLevel) |
| 381 | case InfoLevel: |
| 382 | cfg.Level.SetLevel(zc.InfoLevel) |
| 383 | case WarnLevel: |
| 384 | cfg.Level.SetLevel(zc.WarnLevel) |
| 385 | case ErrorLevel: |
| 386 | cfg.Level.SetLevel(zc.ErrorLevel) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 387 | case FatalLevel: |
| 388 | cfg.Level.SetLevel(zc.FatalLevel) |
| 389 | default: |
| 390 | cfg.Level.SetLevel(zc.ErrorLevel) |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | //SetPackageLogLevel dynamically sets the log level of a given package to level. This is typically invoked at an |
| 395 | // application level during debugging |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 396 | func SetPackageLogLevel(packageName string, level LogLevel) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 397 | // Get proper config |
| 398 | if cfg, ok := cfgs[packageName]; ok { |
| 399 | setLevel(cfg, level) |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | //SetAllLogLevel sets the log level of all registered packages to level |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 404 | func SetAllLogLevel(level LogLevel) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 405 | // Get proper config |
| 406 | for _, cfg := range cfgs { |
| 407 | setLevel(cfg, level) |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | //GetPackageLogLevel returns the current log level of a package. |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 412 | func GetPackageLogLevel(packageName ...string) (LogLevel, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 413 | var name string |
| 414 | if len(packageName) == 1 { |
| 415 | name = packageName[0] |
| 416 | } else { |
| 417 | name, _, _, _ = getCallerInfo() |
| 418 | } |
| 419 | if cfg, ok := cfgs[name]; ok { |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 420 | return levelToLogLevel(cfg.Level.Level()), nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 421 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 422 | return 0, fmt.Errorf("unknown-package-%s", name) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | //GetDefaultLogLevel gets the log level used for packages that don't have specific loggers |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 426 | func GetDefaultLogLevel() LogLevel { |
| 427 | return levelToLogLevel(cfg.Level.Level()) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | //SetLogLevel sets the log level for the logger corresponding to the caller's package |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 431 | func SetLogLevel(level LogLevel) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 432 | pkgName, _, _, _ := getCallerInfo() |
| 433 | if _, exist := cfgs[pkgName]; !exist { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 434 | return fmt.Errorf("unregistered-package-%s", pkgName) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 435 | } |
| 436 | cfg := cfgs[pkgName] |
| 437 | setLevel(cfg, level) |
| 438 | return nil |
| 439 | } |
| 440 | |
| 441 | //SetDefaultLogLevel sets the log level used for packages that don't have specific loggers |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 442 | func SetDefaultLogLevel(level LogLevel) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 443 | setLevel(cfg, level) |
| 444 | } |
| 445 | |
| 446 | // CleanUp flushed any buffered log entries. Applications should take care to call |
| 447 | // CleanUp before exiting. |
| 448 | func CleanUp() error { |
| 449 | for _, logger := range loggers { |
| 450 | if logger != nil { |
| 451 | if logger.parent != nil { |
| 452 | if err := logger.parent.Sync(); err != nil { |
| 453 | return err |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | if defaultLogger != nil { |
| 459 | if defaultLogger.parent != nil { |
| 460 | if err := defaultLogger.parent.Sync(); err != nil { |
| 461 | return err |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | return nil |
| 466 | } |
| 467 | |
| 468 | func getCallerInfo() (string, string, string, int) { |
| 469 | // Since the caller of a log function is one stack frame before (in terms of stack higher level) the log.go |
| 470 | // filename, then first look for the last log.go filename and then grab the caller info one level higher. |
| 471 | maxLevel := 3 |
| 472 | skiplevel := 3 // Level with the most empirical success to see the last log.go stack frame. |
| 473 | pc := make([]uintptr, maxLevel) |
| 474 | n := runtime.Callers(skiplevel, pc) |
| 475 | packageName := "" |
| 476 | funcName := "" |
| 477 | fileName := "" |
| 478 | var line int |
| 479 | if n == 0 { |
| 480 | return packageName, fileName, funcName, line |
| 481 | } |
| 482 | frames := runtime.CallersFrames(pc[:n]) |
| 483 | var frame runtime.Frame |
| 484 | var foundFrame runtime.Frame |
| 485 | more := true |
| 486 | for more { |
| 487 | frame, more = frames.Next() |
| 488 | _, fileName = path.Split(frame.File) |
| 489 | if fileName != "log.go" { |
| 490 | foundFrame = frame // First frame after log.go in the frame stack |
| 491 | break |
| 492 | } |
| 493 | } |
| 494 | parts := strings.Split(foundFrame.Function, ".") |
| 495 | pl := len(parts) |
| 496 | if pl >= 2 { |
| 497 | funcName = parts[pl-1] |
| 498 | if parts[pl-2][0] == '(' { |
| 499 | packageName = strings.Join(parts[0:pl-2], ".") |
| 500 | } else { |
| 501 | packageName = strings.Join(parts[0:pl-1], ".") |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if strings.HasSuffix(packageName, ".init") { |
| 506 | packageName = strings.TrimSuffix(packageName, ".init") |
| 507 | } |
| 508 | |
| 509 | if strings.HasSuffix(fileName, ".go") { |
| 510 | fileName = strings.TrimSuffix(fileName, ".go") |
| 511 | } |
| 512 | |
| 513 | return packageName, fileName, funcName, foundFrame.Line |
| 514 | } |
| 515 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 516 | // With returns a logger initialized with the key-value pairs |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 517 | func (l clogger) With(keysAndValues Fields) CLogger { |
| 518 | return clogger{log: l.log.With(serializeMap(keysAndValues)...), parent: l.parent} |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | // Debug logs a message at level Debug on the standard logger. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 522 | func (l clogger) Debug(ctx context.Context, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 523 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Debug(args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | // Debugln logs a message at level Debug on the standard logger with a line feed. Default in any case. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 527 | func (l clogger) Debugln(ctx context.Context, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 528 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Debug(args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | // Debugw logs a message at level Debug on the standard logger. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 532 | func (l clogger) Debugf(ctx context.Context, format string, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 533 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Debugf(format, args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | // Debugw logs a message with some additional context. The variadic key-value |
| 537 | // pairs are treated as they are in With. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 538 | func (l clogger) Debugw(ctx context.Context, msg string, keysAndValues Fields) { |
khenaidoo | b3ec7d5 | 2020-04-29 16:52:08 -0400 | [diff] [blame] | 539 | if l.V(DebugLevel) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 540 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Debugw(msg, serializeMap(keysAndValues)...) |
khenaidoo | b3ec7d5 | 2020-04-29 16:52:08 -0400 | [diff] [blame] | 541 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | // Info logs a message at level Info on the standard logger. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 545 | func (l clogger) Info(ctx context.Context, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 546 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Info(args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | // Infoln logs a message at level Info on the standard logger with a line feed. Default in any case. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 550 | func (l clogger) Infoln(ctx context.Context, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 551 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Info(args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 552 | //msg := fmt.Sprintln(args...) |
| 553 | //l.sourced().Info(msg[:len(msg)-1]) |
| 554 | } |
| 555 | |
| 556 | // Infof logs a message at level Info on the standard logger. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 557 | func (l clogger) Infof(ctx context.Context, format string, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 558 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Infof(format, args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | // Infow logs a message with some additional context. The variadic key-value |
| 562 | // pairs are treated as they are in With. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 563 | func (l clogger) Infow(ctx context.Context, msg string, keysAndValues Fields) { |
khenaidoo | b3ec7d5 | 2020-04-29 16:52:08 -0400 | [diff] [blame] | 564 | if l.V(InfoLevel) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 565 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Infow(msg, serializeMap(keysAndValues)...) |
khenaidoo | b3ec7d5 | 2020-04-29 16:52:08 -0400 | [diff] [blame] | 566 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | // Warn logs a message at level Warn on the standard logger. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 570 | func (l clogger) Warn(ctx context.Context, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 571 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warn(args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | // Warnln logs a message at level Warn on the standard logger with a line feed. Default in any case. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 575 | func (l clogger) Warnln(ctx context.Context, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 576 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warn(args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | // Warnf logs a message at level Warn on the standard logger. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 580 | func (l clogger) Warnf(ctx context.Context, format string, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 581 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warnf(format, args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | // Warnw logs a message with some additional context. The variadic key-value |
| 585 | // pairs are treated as they are in With. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 586 | func (l clogger) Warnw(ctx context.Context, msg string, keysAndValues Fields) { |
khenaidoo | b3ec7d5 | 2020-04-29 16:52:08 -0400 | [diff] [blame] | 587 | if l.V(WarnLevel) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 588 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warnw(msg, serializeMap(keysAndValues)...) |
khenaidoo | b3ec7d5 | 2020-04-29 16:52:08 -0400 | [diff] [blame] | 589 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | // Error logs a message at level Error on the standard logger. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 593 | func (l clogger) Error(ctx context.Context, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 594 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Error(args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | // Errorln logs a message at level Error on the standard logger with a line feed. Default in any case. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 598 | func (l clogger) Errorln(ctx context.Context, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 599 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Error(args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | // Errorf logs a message at level Error on the standard logger. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 603 | func (l clogger) Errorf(ctx context.Context, format string, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 604 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Errorf(format, args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | // Errorw logs a message with some additional context. The variadic key-value |
| 608 | // pairs are treated as they are in With. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 609 | func (l clogger) Errorw(ctx context.Context, msg string, keysAndValues Fields) { |
khenaidoo | b3ec7d5 | 2020-04-29 16:52:08 -0400 | [diff] [blame] | 610 | if l.V(ErrorLevel) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 611 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Errorw(msg, serializeMap(keysAndValues)...) |
khenaidoo | b3ec7d5 | 2020-04-29 16:52:08 -0400 | [diff] [blame] | 612 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | // Fatal logs a message at level Fatal on the standard logger. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 616 | func (l clogger) Fatal(ctx context.Context, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 617 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Fatal(args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | // Fatalln logs a message at level Fatal on the standard logger with a line feed. Default in any case. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 621 | func (l clogger) Fatalln(ctx context.Context, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 622 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Fatal(args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | // Fatalf logs a message at level Fatal on the standard logger. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 626 | func (l clogger) Fatalf(ctx context.Context, format string, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 627 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Fatalf(format, args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | // Fatalw logs a message with some additional context. The variadic key-value |
| 631 | // pairs are treated as they are in With. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 632 | func (l clogger) Fatalw(ctx context.Context, msg string, keysAndValues Fields) { |
khenaidoo | b3ec7d5 | 2020-04-29 16:52:08 -0400 | [diff] [blame] | 633 | if l.V(FatalLevel) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 634 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Fatalw(msg, serializeMap(keysAndValues)...) |
khenaidoo | b3ec7d5 | 2020-04-29 16:52:08 -0400 | [diff] [blame] | 635 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | // Warning logs a message at level Warn on the standard logger. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 639 | func (l clogger) Warning(ctx context.Context, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 640 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warn(args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | // Warningln logs a message at level Warn on the standard logger with a line feed. Default in any case. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 644 | func (l clogger) Warningln(ctx context.Context, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 645 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warn(args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | // Warningf logs a message at level Warn on the standard logger. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 649 | func (l clogger) Warningf(ctx context.Context, format string, args ...interface{}) { |
Girish Kumar | 503cce6 | 2020-08-24 16:34:39 +0000 | [diff] [blame] | 650 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warnf(format, args...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | // V reports whether verbosity level l is at least the requested verbose level. |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 654 | func (l clogger) V(level LogLevel) bool { |
Scott Baker | d7c25f4 | 2020-02-21 08:12:06 -0800 | [diff] [blame] | 655 | return l.parent.Core().Enabled(logLevelToLevel(level)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 656 | } |
| 657 | |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 658 | // GetLogLevel returns the current level of the logger |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 659 | func (l clogger) GetLogLevel() LogLevel { |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 660 | return levelToLogLevel(cfgs[l.packageName].Level.Level()) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 661 | } |