khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [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 | */ |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 16 | |
| 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 |
| 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) |
| 24 | // |
| 25 | // Using package-level logging (recommended approach). In the examples below, log refers to this log package. |
| 26 | // 1. In the appropriate package add the following in the init section of the package. The log level can be changed |
| 27 | // and any number of default fields can be added as well. The log level specifies the lowest log level that will be |
| 28 | // in the output while the fields will be automatically added to all log printouts. |
| 29 | // |
| 30 | // log.AddPackage(mylog.JSON, log.WarnLevel, log.Fields{"anyFieldName": "any value"}) |
| 31 | // |
| 32 | //2. In the calling package, just invoke any of the publicly available functions of the logger. Here is an example |
| 33 | // to write an Info log with additional fields: |
| 34 | // |
| 35 | //log.Infow("An example", mylog.Fields{"myStringOutput": "output", "myIntOutput": 2}) |
| 36 | // |
| 37 | //3. To dynamically change the log level, you can use 1)SetLogLevel from inside your package or 2) SetPackageLogLevel |
| 38 | // from anywhere or 3) SetAllLogLevel from anywhere. |
| 39 | // |
| 40 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 41 | package log |
| 42 | |
| 43 | import ( |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 44 | "errors" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 45 | "fmt" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 46 | zp "go.uber.org/zap" |
| 47 | zc "go.uber.org/zap/zapcore" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 48 | "path" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 49 | "runtime" |
| 50 | "strings" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 51 | ) |
| 52 | |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 53 | type LogLevel int8 |
| 54 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 55 | const ( |
| 56 | // DebugLevel logs a message at debug level |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 57 | DebugLevel = LogLevel(iota) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 58 | // InfoLevel logs a message at info level |
| 59 | InfoLevel |
| 60 | // WarnLevel logs a message at warning level |
| 61 | WarnLevel |
| 62 | // ErrorLevel logs a message at error level |
| 63 | ErrorLevel |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 64 | // FatalLevel logs a message, then calls os.Exit(1). |
| 65 | FatalLevel |
| 66 | ) |
| 67 | |
| 68 | // CONSOLE formats the log for the console, mostly used during development |
| 69 | const CONSOLE = "console" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 70 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 71 | // JSON formats the log using json format, mostly used by an automated logging system consumption |
| 72 | const JSON = "json" |
| 73 | |
| 74 | // Logger represents an abstract logging interface. Any logging implementation used |
| 75 | // will need to abide by this interface |
| 76 | type Logger interface { |
| 77 | Debug(...interface{}) |
| 78 | Debugln(...interface{}) |
| 79 | Debugf(string, ...interface{}) |
| 80 | Debugw(string, Fields) |
| 81 | |
| 82 | Info(...interface{}) |
| 83 | Infoln(...interface{}) |
| 84 | Infof(string, ...interface{}) |
| 85 | Infow(string, Fields) |
| 86 | |
| 87 | Warn(...interface{}) |
| 88 | Warnln(...interface{}) |
| 89 | Warnf(string, ...interface{}) |
| 90 | Warnw(string, Fields) |
| 91 | |
| 92 | Error(...interface{}) |
| 93 | Errorln(...interface{}) |
| 94 | Errorf(string, ...interface{}) |
| 95 | Errorw(string, Fields) |
| 96 | |
| 97 | Fatal(...interface{}) |
| 98 | Fatalln(...interface{}) |
| 99 | Fatalf(string, ...interface{}) |
| 100 | Fatalw(string, Fields) |
| 101 | |
| 102 | With(Fields) Logger |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 103 | |
| 104 | // The following are added to be able to use this logger as a gRPC LoggerV2 if needed |
| 105 | // |
| 106 | Warning(...interface{}) |
| 107 | Warningln(...interface{}) |
| 108 | Warningf(string, ...interface{}) |
| 109 | |
| 110 | // V reports whether verbosity level l is at least the requested verbose level. |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 111 | V(l LogLevel) bool |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 112 | |
| 113 | //Returns the log level of this specific logger |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 114 | GetLogLevel() LogLevel |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 115 | } |
| 116 | |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 117 | // Fields is used as key-value pairs for structured logging |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 118 | type Fields map[string]interface{} |
| 119 | |
| 120 | var defaultLogger *logger |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 121 | var cfg zp.Config |
| 122 | |
| 123 | var loggers map[string]*logger |
| 124 | var cfgs map[string]zp.Config |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 125 | |
| 126 | type logger struct { |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 127 | log *zp.SugaredLogger |
| 128 | parent *zp.Logger |
| 129 | packageName string |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 132 | func logLevelToAtomicLevel(l LogLevel) zp.AtomicLevel { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 133 | switch l { |
| 134 | case DebugLevel: |
| 135 | return zp.NewAtomicLevelAt(zc.DebugLevel) |
| 136 | case InfoLevel: |
| 137 | return zp.NewAtomicLevelAt(zc.InfoLevel) |
| 138 | case WarnLevel: |
| 139 | return zp.NewAtomicLevelAt(zc.WarnLevel) |
| 140 | case ErrorLevel: |
| 141 | return zp.NewAtomicLevelAt(zc.ErrorLevel) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 142 | case FatalLevel: |
| 143 | return zp.NewAtomicLevelAt(zc.FatalLevel) |
| 144 | } |
| 145 | return zp.NewAtomicLevelAt(zc.ErrorLevel) |
| 146 | } |
| 147 | |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 148 | func logLevelToLevel(l LogLevel) zc.Level { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 149 | switch l { |
| 150 | case DebugLevel: |
| 151 | return zc.DebugLevel |
| 152 | case InfoLevel: |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 153 | return zc.InfoLevel |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 154 | case WarnLevel: |
| 155 | return zc.WarnLevel |
| 156 | case ErrorLevel: |
| 157 | return zc.ErrorLevel |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 158 | case FatalLevel: |
| 159 | return zc.FatalLevel |
| 160 | } |
| 161 | return zc.ErrorLevel |
| 162 | } |
| 163 | |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 164 | func levelToLogLevel(l zc.Level) LogLevel { |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 165 | switch l { |
| 166 | case zc.DebugLevel: |
| 167 | return DebugLevel |
| 168 | case zc.InfoLevel: |
| 169 | return InfoLevel |
| 170 | case zc.WarnLevel: |
| 171 | return WarnLevel |
| 172 | case zc.ErrorLevel: |
| 173 | return ErrorLevel |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 174 | case zc.FatalLevel: |
| 175 | return FatalLevel |
| 176 | } |
| 177 | return ErrorLevel |
| 178 | } |
| 179 | |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 180 | func StringToLogLevel(l string) (LogLevel, error) { |
| 181 | switch strings.ToUpper(l) { |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 182 | case "DEBUG": |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 183 | return DebugLevel, nil |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 184 | case "INFO": |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 185 | return InfoLevel, nil |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 186 | case "WARN": |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 187 | return WarnLevel, nil |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 188 | case "ERROR": |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 189 | return ErrorLevel, nil |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 190 | case "FATAL": |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 191 | return FatalLevel, nil |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 192 | } |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 193 | return 0, errors.New("Given LogLevel is invalid : " + l) |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Scott Baker | 0e78ba2 | 2020-02-24 17:58:47 -0800 | [diff] [blame^] | 196 | func LogLevelToString(l LogLevel) (string, error) { |
| 197 | switch l { |
| 198 | case DebugLevel: |
| 199 | return "DEBUG", nil |
| 200 | case InfoLevel: |
| 201 | return "INFO", nil |
| 202 | case WarnLevel: |
| 203 | return "WARN", nil |
| 204 | case ErrorLevel: |
| 205 | return "ERROR", nil |
| 206 | case FatalLevel: |
| 207 | return "FATAL", nil |
| 208 | } |
| 209 | return "", errors.New("Given LogLevel is invalid " + string(l)) |
| 210 | } |
| 211 | |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 212 | func getDefaultConfig(outputType string, level LogLevel, defaultFields Fields) zp.Config { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 213 | return zp.Config{ |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 214 | Level: logLevelToAtomicLevel(level), |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 215 | Encoding: outputType, |
| 216 | Development: true, |
| 217 | OutputPaths: []string{"stdout"}, |
| 218 | ErrorOutputPaths: []string{"stderr"}, |
| 219 | InitialFields: defaultFields, |
| 220 | EncoderConfig: zc.EncoderConfig{ |
| 221 | LevelKey: "level", |
| 222 | MessageKey: "msg", |
| 223 | TimeKey: "ts", |
| 224 | StacktraceKey: "stacktrace", |
| 225 | LineEnding: zc.DefaultLineEnding, |
| 226 | EncodeLevel: zc.LowercaseLevelEncoder, |
| 227 | EncodeTime: zc.ISO8601TimeEncoder, |
| 228 | EncodeDuration: zc.SecondsDurationEncoder, |
| 229 | EncodeCaller: zc.ShortCallerEncoder, |
| 230 | }, |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // SetLogger needs to be invoked before the logger API can be invoked. This function |
| 235 | // initialize the default logger (zap's sugaredlogger) |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 236 | func SetDefaultLogger(outputType string, level LogLevel, defaultFields Fields) (Logger, error) { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 237 | // Build a custom config using zap |
khenaidoo | d4d922e | 2018-08-03 22:35:16 -0400 | [diff] [blame] | 238 | cfg = getDefaultConfig(outputType, level, defaultFields) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 239 | |
| 240 | l, err := cfg.Build() |
| 241 | if err != nil { |
| 242 | return nil, err |
| 243 | } |
| 244 | |
| 245 | defaultLogger = &logger{ |
| 246 | log: l.Sugar(), |
| 247 | parent: l, |
| 248 | } |
| 249 | |
| 250 | return defaultLogger, nil |
| 251 | } |
| 252 | |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 253 | // AddPackage registers a package to the log map. Each package gets its own logger which allows |
| 254 | // its config (loglevel) to be changed dynamically without interacting with the other packages. |
| 255 | // outputType is JSON, level is the lowest level log to output with this logger and defaultFields is a map of |
| 256 | // key-value pairs to always add to the output. |
| 257 | // Note: AddPackage also returns a reference to the actual logger. If a calling package uses this reference directly |
| 258 | //instead of using the publicly available functions in this log package then a number of functionalities will not |
| 259 | // be available to it, notably log tracing with filename.functionname.linenumber annotation. |
| 260 | // |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 261 | // pkgNames parameter should be used for testing only as this function detects the caller's package. |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 262 | func AddPackage(outputType string, level LogLevel, defaultFields Fields, pkgNames ...string) (Logger, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 263 | if cfgs == nil { |
| 264 | cfgs = make(map[string]zp.Config) |
| 265 | } |
| 266 | if loggers == nil { |
| 267 | loggers = make(map[string]*logger) |
| 268 | } |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 269 | |
| 270 | var pkgName string |
| 271 | for _, name := range pkgNames { |
| 272 | pkgName = name |
| 273 | break |
| 274 | } |
| 275 | if pkgName == "" { |
| 276 | pkgName, _, _, _ = getCallerInfo() |
| 277 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 278 | |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 279 | if _, exist := loggers[pkgName]; exist { |
| 280 | return loggers[pkgName], nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 281 | } |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 282 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 283 | cfgs[pkgName] = getDefaultConfig(outputType, level, defaultFields) |
| 284 | |
| 285 | l, err := cfgs[pkgName].Build() |
| 286 | if err != nil { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 287 | return nil, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | loggers[pkgName] = &logger{ |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 291 | log: l.Sugar(), |
| 292 | parent: l, |
| 293 | packageName: pkgName, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 294 | } |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 295 | return loggers[pkgName], nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 296 | } |
| 297 | |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 298 | //UpdateAllLoggers create new loggers for all registered pacakges with the defaultFields. |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 299 | func UpdateAllLoggers(defaultFields Fields) error { |
| 300 | for pkgName, cfg := range cfgs { |
| 301 | for k, v := range defaultFields { |
| 302 | if cfg.InitialFields == nil { |
| 303 | cfg.InitialFields = make(map[string]interface{}) |
| 304 | } |
| 305 | cfg.InitialFields[k] = v |
| 306 | } |
| 307 | l, err := cfg.Build() |
| 308 | if err != nil { |
| 309 | return err |
| 310 | } |
| 311 | |
| 312 | loggers[pkgName] = &logger{ |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 313 | log: l.Sugar(), |
| 314 | parent: l, |
| 315 | packageName: pkgName, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | return nil |
| 319 | } |
| 320 | |
Scott Baker | 5f40147 | 2019-08-22 08:32:26 -0700 | [diff] [blame] | 321 | // Return a list of all packages that have individually-configured loggers |
| 322 | func GetPackageNames() []string { |
| 323 | i := 0 |
| 324 | keys := make([]string, len(loggers)) |
| 325 | for k := range loggers { |
| 326 | keys[i] = k |
| 327 | i++ |
| 328 | } |
| 329 | return keys |
| 330 | } |
| 331 | |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 332 | // UpdateLogger deletes the logger associated with a caller's package and creates a new logger with the |
| 333 | // defaultFields. If a calling package is holding on to a Logger reference obtained from AddPackage invocation, then |
| 334 | // that package needs to invoke UpdateLogger if it needs to make changes to the default fields and obtain a new logger |
| 335 | // reference |
| 336 | func UpdateLogger(defaultFields Fields) (Logger, error) { |
| 337 | pkgName, _, _, _ := getCallerInfo() |
| 338 | if _, exist := loggers[pkgName]; !exist { |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 339 | return nil, fmt.Errorf("package-%s-not-registered", pkgName) |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 340 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 341 | |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 342 | // Build a new logger |
| 343 | if _, exist := cfgs[pkgName]; !exist { |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 344 | return nil, fmt.Errorf("config-%s-not-registered", pkgName) |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | cfg := cfgs[pkgName] |
| 348 | for k, v := range defaultFields { |
| 349 | if cfg.InitialFields == nil { |
| 350 | cfg.InitialFields = make(map[string]interface{}) |
| 351 | } |
| 352 | cfg.InitialFields[k] = v |
| 353 | } |
| 354 | l, err := cfg.Build() |
| 355 | if err != nil { |
| 356 | return nil, err |
| 357 | } |
| 358 | |
| 359 | // Set the logger |
| 360 | loggers[pkgName] = &logger{ |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 361 | log: l.Sugar(), |
| 362 | parent: l, |
| 363 | packageName: pkgName, |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 364 | } |
| 365 | return loggers[pkgName], nil |
| 366 | } |
| 367 | |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 368 | func setLevel(cfg zp.Config, level LogLevel) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 369 | switch level { |
| 370 | case DebugLevel: |
| 371 | cfg.Level.SetLevel(zc.DebugLevel) |
| 372 | case InfoLevel: |
| 373 | cfg.Level.SetLevel(zc.InfoLevel) |
| 374 | case WarnLevel: |
| 375 | cfg.Level.SetLevel(zc.WarnLevel) |
| 376 | case ErrorLevel: |
| 377 | cfg.Level.SetLevel(zc.ErrorLevel) |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 378 | case FatalLevel: |
| 379 | cfg.Level.SetLevel(zc.FatalLevel) |
| 380 | default: |
| 381 | cfg.Level.SetLevel(zc.ErrorLevel) |
| 382 | } |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | //SetPackageLogLevel dynamically sets the log level of a given package to level. This is typically invoked at an |
| 386 | // application level during debugging |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 387 | func SetPackageLogLevel(packageName string, level LogLevel) { |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 388 | // Get proper config |
| 389 | if cfg, ok := cfgs[packageName]; ok { |
| 390 | setLevel(cfg, level) |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | //SetAllLogLevel sets the log level of all registered packages to level |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 395 | func SetAllLogLevel(level LogLevel) { |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 396 | // Get proper config |
| 397 | for _, cfg := range cfgs { |
| 398 | setLevel(cfg, level) |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | //GetPackageLogLevel returns the current log level of a package. |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 403 | func GetPackageLogLevel(packageName ...string) (LogLevel, error) { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 404 | var name string |
| 405 | if len(packageName) == 1 { |
| 406 | name = packageName[0] |
| 407 | } else { |
| 408 | name, _, _, _ = getCallerInfo() |
| 409 | } |
| 410 | if cfg, ok := cfgs[name]; ok { |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 411 | return levelToLogLevel(cfg.Level.Level()), nil |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 412 | } |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 413 | return 0, fmt.Errorf("unknown-package-%s", name) |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 414 | } |
| 415 | |
Scott Baker | 5f40147 | 2019-08-22 08:32:26 -0700 | [diff] [blame] | 416 | //GetDefaultLogLevel gets the log level used for packages that don't have specific loggers |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 417 | func GetDefaultLogLevel() LogLevel { |
| 418 | return levelToLogLevel(cfg.Level.Level()) |
Scott Baker | 5f40147 | 2019-08-22 08:32:26 -0700 | [diff] [blame] | 419 | } |
| 420 | |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 421 | //SetLogLevel sets the log level for the logger corresponding to the caller's package |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 422 | func SetLogLevel(level LogLevel) error { |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 423 | pkgName, _, _, _ := getCallerInfo() |
| 424 | if _, exist := cfgs[pkgName]; !exist { |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 425 | return fmt.Errorf("unregistered-package-%s", pkgName) |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 426 | } |
| 427 | cfg := cfgs[pkgName] |
| 428 | setLevel(cfg, level) |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 429 | return nil |
| 430 | } |
| 431 | |
Scott Baker | 5f40147 | 2019-08-22 08:32:26 -0700 | [diff] [blame] | 432 | //SetDefaultLogLevel sets the log level used for packages that don't have specific loggers |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 433 | func SetDefaultLogLevel(level LogLevel) { |
Scott Baker | 5f40147 | 2019-08-22 08:32:26 -0700 | [diff] [blame] | 434 | setLevel(cfg, level) |
| 435 | } |
| 436 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 437 | // CleanUp flushed any buffered log entries. Applications should take care to call |
| 438 | // CleanUp before exiting. |
| 439 | func CleanUp() error { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 440 | for _, logger := range loggers { |
| 441 | if logger != nil { |
| 442 | if logger.parent != nil { |
| 443 | if err := logger.parent.Sync(); err != nil { |
| 444 | return err |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | } |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 449 | if defaultLogger != nil { |
| 450 | if defaultLogger.parent != nil { |
| 451 | if err := defaultLogger.parent.Sync(); err != nil { |
| 452 | return err |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | return nil |
| 457 | } |
| 458 | |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 459 | func getCallerInfo() (string, string, string, int) { |
khenaidoo | 2c6f167 | 2018-09-20 23:14:41 -0400 | [diff] [blame] | 460 | // Since the caller of a log function is one stack frame before (in terms of stack higher level) the log.go |
| 461 | // filename, then first look for the last log.go filename and then grab the caller info one level higher. |
| 462 | maxLevel := 3 |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 463 | skiplevel := 3 // Level with the most empirical success to see the last log.go stack frame. |
khenaidoo | 2c6f167 | 2018-09-20 23:14:41 -0400 | [diff] [blame] | 464 | pc := make([]uintptr, maxLevel) |
| 465 | n := runtime.Callers(skiplevel, pc) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 466 | packageName := "" |
khenaidoo | 2c6f167 | 2018-09-20 23:14:41 -0400 | [diff] [blame] | 467 | funcName := "" |
| 468 | fileName := "" |
| 469 | var line int |
| 470 | if n == 0 { |
| 471 | return packageName, fileName, funcName, line |
| 472 | } |
| 473 | frames := runtime.CallersFrames(pc[:n]) |
| 474 | var frame runtime.Frame |
| 475 | var foundFrame runtime.Frame |
| 476 | more := true |
| 477 | for more { |
| 478 | frame, more = frames.Next() |
| 479 | _, fileName = path.Split(frame.File) |
| 480 | if fileName != "log.go" { |
| 481 | foundFrame = frame // First frame after log.go in the frame stack |
| 482 | break |
| 483 | } |
| 484 | } |
| 485 | parts := strings.Split(foundFrame.Function, ".") |
| 486 | pl := len(parts) |
| 487 | if pl >= 2 { |
| 488 | funcName = parts[pl-1] |
| 489 | if parts[pl-2][0] == '(' { |
| 490 | packageName = strings.Join(parts[0:pl-2], ".") |
| 491 | } else { |
| 492 | packageName = strings.Join(parts[0:pl-1], ".") |
| 493 | } |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 494 | } |
| 495 | |
khenaidoo | 2c6f167 | 2018-09-20 23:14:41 -0400 | [diff] [blame] | 496 | if strings.HasSuffix(packageName, ".init") { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 497 | packageName = strings.TrimSuffix(packageName, ".init") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | if strings.HasSuffix(fileName, ".go") { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 501 | fileName = strings.TrimSuffix(fileName, ".go") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 502 | } |
khenaidoo | 2c6f167 | 2018-09-20 23:14:41 -0400 | [diff] [blame] | 503 | |
| 504 | return packageName, fileName, funcName, foundFrame.Line |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 505 | } |
| 506 | |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 507 | func getPackageLevelSugaredLogger() *zp.SugaredLogger { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 508 | pkgName, fileName, funcName, line := getCallerInfo() |
| 509 | if _, exist := loggers[pkgName]; exist { |
| 510 | return loggers[pkgName].log.With("caller", fmt.Sprintf("%s.%s:%d", fileName, funcName, line)) |
| 511 | } |
| 512 | return defaultLogger.log.With("caller", fmt.Sprintf("%s.%s:%d", fileName, funcName, line)) |
| 513 | } |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 514 | |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 515 | func getPackageLevelLogger() Logger { |
| 516 | pkgName, _, _, _ := getCallerInfo() |
| 517 | if _, exist := loggers[pkgName]; exist { |
| 518 | return loggers[pkgName] |
| 519 | } |
| 520 | return defaultLogger |
| 521 | } |
| 522 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 523 | func serializeMap(fields Fields) []interface{} { |
| 524 | data := make([]interface{}, len(fields)*2) |
| 525 | i := 0 |
| 526 | for k, v := range fields { |
| 527 | data[i] = k |
| 528 | data[i+1] = v |
| 529 | i = i + 2 |
| 530 | } |
| 531 | return data |
| 532 | } |
| 533 | |
| 534 | // With returns a logger initialized with the key-value pairs |
| 535 | func (l logger) With(keysAndValues Fields) Logger { |
| 536 | return logger{log: l.log.With(serializeMap(keysAndValues)...), parent: l.parent} |
| 537 | } |
| 538 | |
| 539 | // Debug logs a message at level Debug on the standard logger. |
| 540 | func (l logger) Debug(args ...interface{}) { |
| 541 | l.log.Debug(args...) |
| 542 | } |
| 543 | |
| 544 | // Debugln logs a message at level Debug on the standard logger with a line feed. Default in any case. |
| 545 | func (l logger) Debugln(args ...interface{}) { |
| 546 | l.log.Debug(args...) |
| 547 | } |
| 548 | |
| 549 | // Debugw logs a message at level Debug on the standard logger. |
| 550 | func (l logger) Debugf(format string, args ...interface{}) { |
| 551 | l.log.Debugf(format, args...) |
| 552 | } |
| 553 | |
| 554 | // Debugw logs a message with some additional context. The variadic key-value |
| 555 | // pairs are treated as they are in With. |
| 556 | func (l logger) Debugw(msg string, keysAndValues Fields) { |
| 557 | l.log.Debugw(msg, serializeMap(keysAndValues)...) |
| 558 | } |
| 559 | |
| 560 | // Info logs a message at level Info on the standard logger. |
| 561 | func (l logger) Info(args ...interface{}) { |
| 562 | l.log.Info(args...) |
| 563 | } |
| 564 | |
| 565 | // Infoln logs a message at level Info on the standard logger with a line feed. Default in any case. |
| 566 | func (l logger) Infoln(args ...interface{}) { |
| 567 | l.log.Info(args...) |
| 568 | //msg := fmt.Sprintln(args...) |
| 569 | //l.sourced().Info(msg[:len(msg)-1]) |
| 570 | } |
| 571 | |
| 572 | // Infof logs a message at level Info on the standard logger. |
| 573 | func (l logger) Infof(format string, args ...interface{}) { |
| 574 | l.log.Infof(format, args...) |
| 575 | } |
| 576 | |
| 577 | // Infow logs a message with some additional context. The variadic key-value |
| 578 | // pairs are treated as they are in With. |
| 579 | func (l logger) Infow(msg string, keysAndValues Fields) { |
| 580 | l.log.Infow(msg, serializeMap(keysAndValues)...) |
| 581 | } |
| 582 | |
| 583 | // Warn logs a message at level Warn on the standard logger. |
| 584 | func (l logger) Warn(args ...interface{}) { |
| 585 | l.log.Warn(args...) |
| 586 | } |
| 587 | |
| 588 | // Warnln logs a message at level Warn on the standard logger with a line feed. Default in any case. |
| 589 | func (l logger) Warnln(args ...interface{}) { |
| 590 | l.log.Warn(args...) |
| 591 | } |
| 592 | |
| 593 | // Warnf logs a message at level Warn on the standard logger. |
| 594 | func (l logger) Warnf(format string, args ...interface{}) { |
| 595 | l.log.Warnf(format, args...) |
| 596 | } |
| 597 | |
| 598 | // Warnw logs a message with some additional context. The variadic key-value |
| 599 | // pairs are treated as they are in With. |
| 600 | func (l logger) Warnw(msg string, keysAndValues Fields) { |
| 601 | l.log.Warnw(msg, serializeMap(keysAndValues)...) |
| 602 | } |
| 603 | |
| 604 | // Error logs a message at level Error on the standard logger. |
| 605 | func (l logger) Error(args ...interface{}) { |
| 606 | l.log.Error(args...) |
| 607 | } |
| 608 | |
| 609 | // Errorln logs a message at level Error on the standard logger with a line feed. Default in any case. |
| 610 | func (l logger) Errorln(args ...interface{}) { |
| 611 | l.log.Error(args...) |
| 612 | } |
| 613 | |
| 614 | // Errorf logs a message at level Error on the standard logger. |
| 615 | func (l logger) Errorf(format string, args ...interface{}) { |
| 616 | l.log.Errorf(format, args...) |
| 617 | } |
| 618 | |
| 619 | // Errorw logs a message with some additional context. The variadic key-value |
| 620 | // pairs are treated as they are in With. |
| 621 | func (l logger) Errorw(msg string, keysAndValues Fields) { |
| 622 | l.log.Errorw(msg, serializeMap(keysAndValues)...) |
| 623 | } |
| 624 | |
| 625 | // Fatal logs a message at level Fatal on the standard logger. |
| 626 | func (l logger) Fatal(args ...interface{}) { |
| 627 | l.log.Fatal(args...) |
| 628 | } |
| 629 | |
| 630 | // Fatalln logs a message at level Fatal on the standard logger with a line feed. Default in any case. |
| 631 | func (l logger) Fatalln(args ...interface{}) { |
| 632 | l.log.Fatal(args...) |
| 633 | } |
| 634 | |
| 635 | // Fatalf logs a message at level Fatal on the standard logger. |
| 636 | func (l logger) Fatalf(format string, args ...interface{}) { |
| 637 | l.log.Fatalf(format, args...) |
| 638 | } |
| 639 | |
| 640 | // Fatalw logs a message with some additional context. The variadic key-value |
| 641 | // pairs are treated as they are in With. |
| 642 | func (l logger) Fatalw(msg string, keysAndValues Fields) { |
| 643 | l.log.Fatalw(msg, serializeMap(keysAndValues)...) |
| 644 | } |
| 645 | |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 646 | // Warning logs a message at level Warn on the standard logger. |
| 647 | func (l logger) Warning(args ...interface{}) { |
| 648 | l.log.Warn(args...) |
| 649 | } |
| 650 | |
| 651 | // Warningln logs a message at level Warn on the standard logger with a line feed. Default in any case. |
| 652 | func (l logger) Warningln(args ...interface{}) { |
| 653 | l.log.Warn(args...) |
| 654 | } |
| 655 | |
| 656 | // Warningf logs a message at level Warn on the standard logger. |
| 657 | func (l logger) Warningf(format string, args ...interface{}) { |
| 658 | l.log.Warnf(format, args...) |
| 659 | } |
| 660 | |
| 661 | // V reports whether verbosity level l is at least the requested verbose level. |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 662 | func (l logger) V(level LogLevel) bool { |
| 663 | return l.parent.Core().Enabled(logLevelToLevel(level)) |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 664 | } |
| 665 | |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 666 | // GetLogLevel returns the current level of the logger |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 667 | func (l logger) GetLogLevel() LogLevel { |
| 668 | return levelToLogLevel(cfgs[l.packageName].Level.Level()) |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 669 | } |
| 670 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 671 | // With returns a logger initialized with the key-value pairs |
| 672 | func With(keysAndValues Fields) Logger { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 673 | return logger{log: getPackageLevelSugaredLogger().With(serializeMap(keysAndValues)...), parent: defaultLogger.parent} |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | // Debug logs a message at level Debug on the standard logger. |
| 677 | func Debug(args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 678 | getPackageLevelSugaredLogger().Debug(args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | // Debugln logs a message at level Debug on the standard logger. |
| 682 | func Debugln(args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 683 | getPackageLevelSugaredLogger().Debug(args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | // Debugf logs a message at level Debug on the standard logger. |
| 687 | func Debugf(format string, args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 688 | getPackageLevelSugaredLogger().Debugf(format, args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | // Debugw logs a message with some additional context. The variadic key-value |
| 692 | // pairs are treated as they are in With. |
| 693 | func Debugw(msg string, keysAndValues Fields) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 694 | getPackageLevelSugaredLogger().Debugw(msg, serializeMap(keysAndValues)...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | // Info logs a message at level Info on the standard logger. |
| 698 | func Info(args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 699 | getPackageLevelSugaredLogger().Info(args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | // Infoln logs a message at level Info on the standard logger. |
| 703 | func Infoln(args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 704 | getPackageLevelSugaredLogger().Info(args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | // Infof logs a message at level Info on the standard logger. |
| 708 | func Infof(format string, args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 709 | getPackageLevelSugaredLogger().Infof(format, args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | //Infow logs a message with some additional context. The variadic key-value |
| 713 | //pairs are treated as they are in With. |
| 714 | func Infow(msg string, keysAndValues Fields) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 715 | getPackageLevelSugaredLogger().Infow(msg, serializeMap(keysAndValues)...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | // Warn logs a message at level Warn on the standard logger. |
| 719 | func Warn(args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 720 | getPackageLevelSugaredLogger().Warn(args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | // Warnln logs a message at level Warn on the standard logger. |
| 724 | func Warnln(args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 725 | getPackageLevelSugaredLogger().Warn(args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | // Warnf logs a message at level Warn on the standard logger. |
| 729 | func Warnf(format string, args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 730 | getPackageLevelSugaredLogger().Warnf(format, args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | // Warnw logs a message with some additional context. The variadic key-value |
| 734 | // pairs are treated as they are in With. |
| 735 | func Warnw(msg string, keysAndValues Fields) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 736 | getPackageLevelSugaredLogger().Warnw(msg, serializeMap(keysAndValues)...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | // Error logs a message at level Error on the standard logger. |
| 740 | func Error(args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 741 | getPackageLevelSugaredLogger().Error(args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | // Errorln logs a message at level Error on the standard logger. |
| 745 | func Errorln(args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 746 | getPackageLevelSugaredLogger().Error(args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | // Errorf logs a message at level Error on the standard logger. |
| 750 | func Errorf(format string, args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 751 | getPackageLevelSugaredLogger().Errorf(format, args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | // Errorw logs a message with some additional context. The variadic key-value |
| 755 | // pairs are treated as they are in With. |
| 756 | func Errorw(msg string, keysAndValues Fields) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 757 | getPackageLevelSugaredLogger().Errorw(msg, serializeMap(keysAndValues)...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | // Fatal logs a message at level Fatal on the standard logger. |
| 761 | func Fatal(args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 762 | getPackageLevelSugaredLogger().Fatal(args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | // Fatalln logs a message at level Fatal on the standard logger. |
| 766 | func Fatalln(args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 767 | getPackageLevelSugaredLogger().Fatal(args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | // Fatalf logs a message at level Fatal on the standard logger. |
| 771 | func Fatalf(format string, args ...interface{}) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 772 | getPackageLevelSugaredLogger().Fatalf(format, args...) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | // Fatalw logs a message with some additional context. The variadic key-value |
| 776 | // pairs are treated as they are in With. |
| 777 | func Fatalw(msg string, keysAndValues Fields) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 778 | getPackageLevelSugaredLogger().Fatalw(msg, serializeMap(keysAndValues)...) |
| 779 | } |
| 780 | |
| 781 | // Warning logs a message at level Warn on the standard logger. |
| 782 | func Warning(args ...interface{}) { |
| 783 | getPackageLevelSugaredLogger().Warn(args...) |
| 784 | } |
| 785 | |
| 786 | // Warningln logs a message at level Warn on the standard logger. |
| 787 | func Warningln(args ...interface{}) { |
| 788 | getPackageLevelSugaredLogger().Warn(args...) |
| 789 | } |
| 790 | |
| 791 | // Warningf logs a message at level Warn on the standard logger. |
| 792 | func Warningf(format string, args ...interface{}) { |
| 793 | getPackageLevelSugaredLogger().Warnf(format, args...) |
| 794 | } |
| 795 | |
| 796 | // V reports whether verbosity level l is at least the requested verbose level. |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 797 | func V(level LogLevel) bool { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 798 | return getPackageLevelLogger().V(level) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 799 | } |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 800 | |
| 801 | //GetLogLevel returns the log level of the invoking package |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 802 | func GetLogLevel() LogLevel { |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 803 | return getPackageLevelLogger().GetLogLevel() |
| 804 | } |