Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [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 | |
| 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) |
| 24 | // |
| 25 | // Using package-level logging (recommended approach). In the examples below, log refers to this log package. |
| 26 | // |
| 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. |
| 32 | // |
| 33 | // var logger log.CLogger |
| 34 | // func init() { |
| 35 | // logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"key1": "value1"}) |
| 36 | // } |
| 37 | // |
| 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: |
| 40 | // |
| 41 | // logger.Infow("An example", mylog.Fields{"myStringOutput": "output", "myIntOutput": 2}) |
| 42 | // |
| 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 |
| 50 | |
| 51 | package log |
| 52 | |
| 53 | import ( |
| 54 | "context" |
| 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 | |
| 64 | type LogLevel int8 |
| 65 | |
| 66 | const ( |
| 67 | // DebugLevel logs a message at debug level |
| 68 | DebugLevel = LogLevel(iota) |
| 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 |
| 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 | |
| 85 | // Context Aware Logger represents an abstract logging interface. Any logging implementation used |
| 86 | // will need to abide by this interface |
| 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) |
| 92 | |
| 93 | Info(context.Context, ...interface{}) |
| 94 | Infoln(context.Context, ...interface{}) |
| 95 | Infof(context.Context, string, ...interface{}) |
| 96 | Infow(context.Context, string, Fields) |
| 97 | |
| 98 | Warn(context.Context, ...interface{}) |
| 99 | Warnln(context.Context, ...interface{}) |
| 100 | Warnf(context.Context, string, ...interface{}) |
| 101 | Warnw(context.Context, string, Fields) |
| 102 | |
| 103 | Error(context.Context, ...interface{}) |
| 104 | Errorln(context.Context, ...interface{}) |
| 105 | Errorf(context.Context, string, ...interface{}) |
| 106 | Errorw(context.Context, string, Fields) |
| 107 | |
| 108 | Fatal(context.Context, ...interface{}) |
| 109 | Fatalln(context.Context, ...interface{}) |
| 110 | Fatalf(context.Context, string, ...interface{}) |
| 111 | Fatalw(context.Context, string, Fields) |
| 112 | |
| 113 | With(Fields) CLogger |
| 114 | |
| 115 | // The following are added to be able to use this logger as a gRPC LoggerV2 if needed |
| 116 | // |
| 117 | Warning(context.Context, ...interface{}) |
| 118 | Warningln(context.Context, ...interface{}) |
| 119 | Warningf(context.Context, string, ...interface{}) |
| 120 | |
| 121 | // V reports whether verbosity level l is at least the requested verbose level. |
| 122 | V(l LogLevel) bool |
| 123 | |
| 124 | //Returns the log level of this specific logger |
| 125 | GetLogLevel() LogLevel |
| 126 | } |
| 127 | |
| 128 | // Fields is used as key-value pairs for structured logging |
| 129 | type Fields map[string]interface{} |
| 130 | |
| 131 | var defaultLogger *clogger |
| 132 | var cfg zp.Config |
| 133 | |
| 134 | var loggers map[string]*clogger |
| 135 | var cfgs map[string]zp.Config |
| 136 | |
| 137 | type clogger struct { |
| 138 | log *zp.SugaredLogger |
| 139 | parent *zp.Logger |
| 140 | packageName string |
| 141 | } |
| 142 | |
| 143 | func logLevelToAtomicLevel(l LogLevel) zp.AtomicLevel { |
| 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) |
| 153 | case FatalLevel: |
| 154 | return zp.NewAtomicLevelAt(zc.FatalLevel) |
| 155 | } |
| 156 | return zp.NewAtomicLevelAt(zc.ErrorLevel) |
| 157 | } |
| 158 | |
| 159 | func logLevelToLevel(l LogLevel) zc.Level { |
| 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 |
| 169 | case FatalLevel: |
| 170 | return zc.FatalLevel |
| 171 | } |
| 172 | return zc.ErrorLevel |
| 173 | } |
| 174 | |
| 175 | func levelToLogLevel(l zc.Level) LogLevel { |
| 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 |
| 185 | case zc.FatalLevel: |
| 186 | return FatalLevel |
| 187 | } |
| 188 | return ErrorLevel |
| 189 | } |
| 190 | |
| 191 | func StringToLogLevel(l string) (LogLevel, error) { |
| 192 | switch strings.ToUpper(l) { |
| 193 | case "DEBUG": |
| 194 | return DebugLevel, nil |
| 195 | case "INFO": |
| 196 | return InfoLevel, nil |
| 197 | case "WARN": |
| 198 | return WarnLevel, nil |
| 199 | case "ERROR": |
| 200 | return ErrorLevel, nil |
| 201 | case "FATAL": |
| 202 | return FatalLevel, nil |
| 203 | } |
| 204 | return 0, errors.New("Given LogLevel is invalid : " + l) |
| 205 | } |
| 206 | |
| 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 | |
| 223 | func getDefaultConfig(outputType string, level LogLevel, defaultFields Fields) zp.Config { |
| 224 | return zp.Config{ |
| 225 | Level: logLevelToAtomicLevel(level), |
| 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", |
| 235 | CallerKey: "caller", |
| 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 | |
| 246 | func ConstructZapConfig(outputType string, level LogLevel, fields Fields) zp.Config { |
| 247 | return getDefaultConfig(outputType, level, fields) |
| 248 | } |
| 249 | |
| 250 | // SetLogger needs to be invoked before the logger API can be invoked. This function |
| 251 | // initialize the default logger (zap's sugaredlogger) |
| 252 | func SetDefaultLogger(outputType string, level LogLevel, defaultFields Fields) (CLogger, error) { |
| 253 | // Build a custom config using zap |
| 254 | cfg = getDefaultConfig(outputType, level, defaultFields) |
| 255 | |
| 256 | l, err := cfg.Build(zp.AddCallerSkip(1)) |
| 257 | if err != nil { |
| 258 | return nil, err |
| 259 | } |
| 260 | |
| 261 | defaultLogger = &clogger{ |
| 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. |
| 278 | func RegisterPackage(outputType string, level LogLevel, defaultFields Fields, pkgNames ...string) (CLogger, error) { |
| 279 | if cfgs == nil { |
| 280 | cfgs = make(map[string]zp.Config) |
| 281 | } |
| 282 | if loggers == nil { |
| 283 | loggers = make(map[string]*clogger) |
| 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 | |
| 301 | l, err := cfgs[pkgName].Build(zp.AddCallerSkip(1)) |
| 302 | if err != nil { |
| 303 | return nil, err |
| 304 | } |
| 305 | |
| 306 | loggers[pkgName] = &clogger{ |
| 307 | log: l.Sugar(), |
| 308 | parent: l, |
| 309 | packageName: pkgName, |
| 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 | } |
| 323 | l, err := cfg.Build(zp.AddCallerSkip(1)) |
| 324 | if err != nil { |
| 325 | return err |
| 326 | } |
| 327 | |
| 328 | // Update the existing zap logger instance |
| 329 | loggers[pkgName].log = l.Sugar() |
| 330 | loggers[pkgName].parent = l |
| 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 | |
| 346 | // UpdateLogger updates the logger associated with a caller's package with supplied defaultFields |
| 347 | func UpdateLogger(defaultFields Fields) error { |
| 348 | pkgName, _, _, _ := getCallerInfo() |
| 349 | if _, exist := loggers[pkgName]; !exist { |
| 350 | return fmt.Errorf("package-%s-not-registered", pkgName) |
| 351 | } |
| 352 | |
| 353 | // Build a new logger |
| 354 | if _, exist := cfgs[pkgName]; !exist { |
| 355 | return fmt.Errorf("config-%s-not-registered", pkgName) |
| 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 | } |
| 365 | l, err := cfg.Build(zp.AddCallerSkip(1)) |
| 366 | if err != nil { |
| 367 | return err |
| 368 | } |
| 369 | |
| 370 | // Update the existing zap logger instance |
| 371 | loggers[pkgName].log = l.Sugar() |
| 372 | loggers[pkgName].parent = l |
| 373 | |
| 374 | return nil |
| 375 | } |
| 376 | |
| 377 | func setLevel(cfg zp.Config, level LogLevel) { |
| 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) |
| 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 |
| 396 | func SetPackageLogLevel(packageName string, level LogLevel) { |
| 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 |
| 404 | func SetAllLogLevel(level LogLevel) { |
| 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. |
| 412 | func GetPackageLogLevel(packageName ...string) (LogLevel, error) { |
| 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 { |
| 420 | return levelToLogLevel(cfg.Level.Level()), nil |
| 421 | } |
| 422 | return 0, fmt.Errorf("unknown-package-%s", name) |
| 423 | } |
| 424 | |
| 425 | //GetDefaultLogLevel gets the log level used for packages that don't have specific loggers |
| 426 | func GetDefaultLogLevel() LogLevel { |
| 427 | return levelToLogLevel(cfg.Level.Level()) |
| 428 | } |
| 429 | |
| 430 | //SetLogLevel sets the log level for the logger corresponding to the caller's package |
| 431 | func SetLogLevel(level LogLevel) error { |
| 432 | pkgName, _, _, _ := getCallerInfo() |
| 433 | if _, exist := cfgs[pkgName]; !exist { |
| 434 | return fmt.Errorf("unregistered-package-%s", pkgName) |
| 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 |
| 442 | func SetDefaultLogLevel(level LogLevel) { |
| 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 | |
| 516 | // With returns a logger initialized with the key-value pairs |
| 517 | func (l clogger) With(keysAndValues Fields) CLogger { |
| 518 | return clogger{log: l.log.With(serializeMap(keysAndValues)...), parent: l.parent} |
| 519 | } |
| 520 | |
| 521 | // Debug logs a message at level Debug on the standard logger. |
| 522 | func (l clogger) Debug(ctx context.Context, args ...interface{}) { |
| 523 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Debug(args...) |
| 524 | } |
| 525 | |
| 526 | // Debugln logs a message at level Debug on the standard logger with a line feed. Default in any case. |
| 527 | func (l clogger) Debugln(ctx context.Context, args ...interface{}) { |
| 528 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Debug(args...) |
| 529 | } |
| 530 | |
| 531 | // Debugw logs a message at level Debug on the standard logger. |
| 532 | func (l clogger) Debugf(ctx context.Context, format string, args ...interface{}) { |
| 533 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Debugf(format, args...) |
| 534 | } |
| 535 | |
| 536 | // Debugw logs a message with some additional context. The variadic key-value |
| 537 | // pairs are treated as they are in With. |
| 538 | func (l clogger) Debugw(ctx context.Context, msg string, keysAndValues Fields) { |
| 539 | if l.V(DebugLevel) { |
| 540 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Debugw(msg, serializeMap(keysAndValues)...) |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | // Info logs a message at level Info on the standard logger. |
| 545 | func (l clogger) Info(ctx context.Context, args ...interface{}) { |
| 546 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Info(args...) |
| 547 | } |
| 548 | |
| 549 | // Infoln logs a message at level Info on the standard logger with a line feed. Default in any case. |
| 550 | func (l clogger) Infoln(ctx context.Context, args ...interface{}) { |
| 551 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Info(args...) |
| 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. |
| 557 | func (l clogger) Infof(ctx context.Context, format string, args ...interface{}) { |
| 558 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Infof(format, args...) |
| 559 | } |
| 560 | |
| 561 | // Infow logs a message with some additional context. The variadic key-value |
| 562 | // pairs are treated as they are in With. |
| 563 | func (l clogger) Infow(ctx context.Context, msg string, keysAndValues Fields) { |
| 564 | if l.V(InfoLevel) { |
| 565 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Infow(msg, serializeMap(keysAndValues)...) |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | // Warn logs a message at level Warn on the standard logger. |
| 570 | func (l clogger) Warn(ctx context.Context, args ...interface{}) { |
| 571 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warn(args...) |
| 572 | } |
| 573 | |
| 574 | // Warnln logs a message at level Warn on the standard logger with a line feed. Default in any case. |
| 575 | func (l clogger) Warnln(ctx context.Context, args ...interface{}) { |
| 576 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warn(args...) |
| 577 | } |
| 578 | |
| 579 | // Warnf logs a message at level Warn on the standard logger. |
| 580 | func (l clogger) Warnf(ctx context.Context, format string, args ...interface{}) { |
| 581 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warnf(format, args...) |
| 582 | } |
| 583 | |
| 584 | // Warnw logs a message with some additional context. The variadic key-value |
| 585 | // pairs are treated as they are in With. |
| 586 | func (l clogger) Warnw(ctx context.Context, msg string, keysAndValues Fields) { |
| 587 | if l.V(WarnLevel) { |
| 588 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warnw(msg, serializeMap(keysAndValues)...) |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | // Error logs a message at level Error on the standard logger. |
| 593 | func (l clogger) Error(ctx context.Context, args ...interface{}) { |
| 594 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Error(args...) |
| 595 | } |
| 596 | |
| 597 | // Errorln logs a message at level Error on the standard logger with a line feed. Default in any case. |
| 598 | func (l clogger) Errorln(ctx context.Context, args ...interface{}) { |
| 599 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Error(args...) |
| 600 | } |
| 601 | |
| 602 | // Errorf logs a message at level Error on the standard logger. |
| 603 | func (l clogger) Errorf(ctx context.Context, format string, args ...interface{}) { |
| 604 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Errorf(format, args...) |
| 605 | } |
| 606 | |
| 607 | // Errorw logs a message with some additional context. The variadic key-value |
| 608 | // pairs are treated as they are in With. |
| 609 | func (l clogger) Errorw(ctx context.Context, msg string, keysAndValues Fields) { |
| 610 | if l.V(ErrorLevel) { |
| 611 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Errorw(msg, serializeMap(keysAndValues)...) |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // Fatal logs a message at level Fatal on the standard logger. |
| 616 | func (l clogger) Fatal(ctx context.Context, args ...interface{}) { |
| 617 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Fatal(args...) |
| 618 | } |
| 619 | |
| 620 | // Fatalln logs a message at level Fatal on the standard logger with a line feed. Default in any case. |
| 621 | func (l clogger) Fatalln(ctx context.Context, args ...interface{}) { |
| 622 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Fatal(args...) |
| 623 | } |
| 624 | |
| 625 | // Fatalf logs a message at level Fatal on the standard logger. |
| 626 | func (l clogger) Fatalf(ctx context.Context, format string, args ...interface{}) { |
| 627 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Fatalf(format, args...) |
| 628 | } |
| 629 | |
| 630 | // Fatalw logs a message with some additional context. The variadic key-value |
| 631 | // pairs are treated as they are in With. |
| 632 | func (l clogger) Fatalw(ctx context.Context, msg string, keysAndValues Fields) { |
| 633 | if l.V(FatalLevel) { |
| 634 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Fatalw(msg, serializeMap(keysAndValues)...) |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | // Warning logs a message at level Warn on the standard logger. |
| 639 | func (l clogger) Warning(ctx context.Context, args ...interface{}) { |
| 640 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warn(args...) |
| 641 | } |
| 642 | |
| 643 | // Warningln logs a message at level Warn on the standard logger with a line feed. Default in any case. |
| 644 | func (l clogger) Warningln(ctx context.Context, args ...interface{}) { |
| 645 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warn(args...) |
| 646 | } |
| 647 | |
| 648 | // Warningf logs a message at level Warn on the standard logger. |
| 649 | func (l clogger) Warningf(ctx context.Context, format string, args ...interface{}) { |
| 650 | l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warnf(format, args...) |
| 651 | } |
| 652 | |
| 653 | // V reports whether verbosity level l is at least the requested verbose level. |
| 654 | func (l clogger) V(level LogLevel) bool { |
| 655 | return l.parent.Core().Enabled(logLevelToLevel(level)) |
| 656 | } |
| 657 | |
| 658 | // GetLogLevel returns the current level of the logger |
| 659 | func (l clogger) GetLogLevel() LogLevel { |
| 660 | return levelToLogLevel(cfgs[l.packageName].Level.Level()) |
| 661 | } |