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