blob: b8d498c820a71bdeb81d258c964ae1bfad6bc63e [file] [log] [blame]
Don Newton7577f072020-01-06 12:41:11 -05001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Girish Kumarcd402012020-08-18 12:17:38 +000017// 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)
Don Newton7577f072020-01-06 12:41:11 -050024//
25// Using package-level logging (recommended approach). In the examples below, log refers to this log package.
Don Newton7577f072020-01-06 12:41:11 -050026//
Girish Kumarcd402012020-08-18 12:17:38 +000027// 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.
Don Newton7577f072020-01-06 12:41:11 -050032//
Girish Kumarcd402012020-08-18 12:17:38 +000033// var logger log.CLogger
34// func init() {
35// logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"key1": "value1"})
36// }
Don Newton7577f072020-01-06 12:41:11 -050037//
Girish Kumarcd402012020-08-18 12:17:38 +000038// 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:
Don Newton7577f072020-01-06 12:41:11 -050040//
Girish Kumarcd402012020-08-18 12:17:38 +000041// logger.Infow("An example", mylog.Fields{"myStringOutput": "output", "myIntOutput": 2})
Don Newton7577f072020-01-06 12:41:11 -050042//
Girish Kumarcd402012020-08-18 12:17:38 +000043// 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
Don Newton7577f072020-01-06 12:41:11 -050050
51package log
52
53import (
Rohan Agrawalc32d9932020-06-15 11:01:47 +000054 "context"
Don Newton7577f072020-01-06 12:41:11 -050055 "errors"
56 "fmt"
57 zp "go.uber.org/zap"
58 zc "go.uber.org/zap/zapcore"
59 "path"
60 "runtime"
61 "strings"
62)
63
divyadesai81bb7ba2020-03-11 11:45:23 +000064type LogLevel int8
65
Don Newton7577f072020-01-06 12:41:11 -050066const (
67 // DebugLevel logs a message at debug level
divyadesai81bb7ba2020-03-11 11:45:23 +000068 DebugLevel = LogLevel(iota)
Don Newton7577f072020-01-06 12:41:11 -050069 // 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
Don Newton7577f072020-01-06 12:41:11 -050075 // 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
80const CONSOLE = "console"
81
82// JSON formats the log using json format, mostly used by an automated logging system consumption
83const JSON = "json"
84
Rohan Agrawalc32d9932020-06-15 11:01:47 +000085// Context Aware Logger represents an abstract logging interface. Any logging implementation used
Don Newton7577f072020-01-06 12:41:11 -050086// will need to abide by this interface
Rohan Agrawalc32d9932020-06-15 11:01:47 +000087type CLogger interface {
88 Debug(context.Context, ...interface{})
89 Debugln(context.Context, ...interface{})
90 Debugf(context.Context, string, ...interface{})
91 Debugw(context.Context, string, Fields)
Don Newton7577f072020-01-06 12:41:11 -050092
Rohan Agrawalc32d9932020-06-15 11:01:47 +000093 Info(context.Context, ...interface{})
94 Infoln(context.Context, ...interface{})
95 Infof(context.Context, string, ...interface{})
96 Infow(context.Context, string, Fields)
Don Newton7577f072020-01-06 12:41:11 -050097
Rohan Agrawalc32d9932020-06-15 11:01:47 +000098 Warn(context.Context, ...interface{})
99 Warnln(context.Context, ...interface{})
100 Warnf(context.Context, string, ...interface{})
101 Warnw(context.Context, string, Fields)
Don Newton7577f072020-01-06 12:41:11 -0500102
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000103 Error(context.Context, ...interface{})
104 Errorln(context.Context, ...interface{})
105 Errorf(context.Context, string, ...interface{})
106 Errorw(context.Context, string, Fields)
Don Newton7577f072020-01-06 12:41:11 -0500107
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000108 Fatal(context.Context, ...interface{})
109 Fatalln(context.Context, ...interface{})
110 Fatalf(context.Context, string, ...interface{})
111 Fatalw(context.Context, string, Fields)
Don Newton7577f072020-01-06 12:41:11 -0500112
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000113 With(Fields) CLogger
Don Newton7577f072020-01-06 12:41:11 -0500114
115 // The following are added to be able to use this logger as a gRPC LoggerV2 if needed
116 //
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000117 Warning(context.Context, ...interface{})
118 Warningln(context.Context, ...interface{})
119 Warningf(context.Context, string, ...interface{})
Don Newton7577f072020-01-06 12:41:11 -0500120
121 // V reports whether verbosity level l is at least the requested verbose level.
divyadesai81bb7ba2020-03-11 11:45:23 +0000122 V(l LogLevel) bool
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -0800123
124 //Returns the log level of this specific logger
divyadesai81bb7ba2020-03-11 11:45:23 +0000125 GetLogLevel() LogLevel
Don Newton7577f072020-01-06 12:41:11 -0500126}
127
128// Fields is used as key-value pairs for structured logging
129type Fields map[string]interface{}
130
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000131var defaultLogger *clogger
Don Newton7577f072020-01-06 12:41:11 -0500132var cfg zp.Config
133
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000134var loggers map[string]*clogger
Don Newton7577f072020-01-06 12:41:11 -0500135var cfgs map[string]zp.Config
136
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000137type clogger struct {
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -0800138 log *zp.SugaredLogger
139 parent *zp.Logger
140 packageName string
Don Newton7577f072020-01-06 12:41:11 -0500141}
142
divyadesai81bb7ba2020-03-11 11:45:23 +0000143func logLevelToAtomicLevel(l LogLevel) zp.AtomicLevel {
Don Newton7577f072020-01-06 12:41:11 -0500144 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)
Don Newton7577f072020-01-06 12:41:11 -0500153 case FatalLevel:
154 return zp.NewAtomicLevelAt(zc.FatalLevel)
155 }
156 return zp.NewAtomicLevelAt(zc.ErrorLevel)
157}
158
divyadesai81bb7ba2020-03-11 11:45:23 +0000159func logLevelToLevel(l LogLevel) zc.Level {
Don Newton7577f072020-01-06 12:41:11 -0500160 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
Don Newton7577f072020-01-06 12:41:11 -0500169 case FatalLevel:
170 return zc.FatalLevel
171 }
172 return zc.ErrorLevel
173}
174
divyadesai81bb7ba2020-03-11 11:45:23 +0000175func levelToLogLevel(l zc.Level) LogLevel {
Don Newton7577f072020-01-06 12:41:11 -0500176 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
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -0800185 case zc.FatalLevel:
186 return FatalLevel
187 }
188 return ErrorLevel
189}
190
divyadesai81bb7ba2020-03-11 11:45:23 +0000191func StringToLogLevel(l string) (LogLevel, error) {
192 switch strings.ToUpper(l) {
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -0800193 case "DEBUG":
divyadesai81bb7ba2020-03-11 11:45:23 +0000194 return DebugLevel, nil
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -0800195 case "INFO":
divyadesai81bb7ba2020-03-11 11:45:23 +0000196 return InfoLevel, nil
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -0800197 case "WARN":
divyadesai81bb7ba2020-03-11 11:45:23 +0000198 return WarnLevel, nil
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -0800199 case "ERROR":
divyadesai81bb7ba2020-03-11 11:45:23 +0000200 return ErrorLevel, nil
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -0800201 case "FATAL":
divyadesai81bb7ba2020-03-11 11:45:23 +0000202 return FatalLevel, nil
Don Newton7577f072020-01-06 12:41:11 -0500203 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000204 return 0, errors.New("Given LogLevel is invalid : " + l)
Don Newton7577f072020-01-06 12:41:11 -0500205}
206
divyadesai81bb7ba2020-03-11 11:45:23 +0000207func 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
223func getDefaultConfig(outputType string, level LogLevel, defaultFields Fields) zp.Config {
Don Newton7577f072020-01-06 12:41:11 -0500224 return zp.Config{
divyadesai81bb7ba2020-03-11 11:45:23 +0000225 Level: logLevelToAtomicLevel(level),
Don Newton7577f072020-01-06 12:41:11 -0500226 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",
divyadesai81bb7ba2020-03-11 11:45:23 +0000235 CallerKey: "caller",
Don Newton7577f072020-01-06 12:41:11 -0500236 StacktraceKey: "stacktrace",
237 LineEnding: zc.DefaultLineEnding,
238 EncodeLevel: zc.LowercaseLevelEncoder,
239 EncodeTime: zc.ISO8601TimeEncoder,
240 EncodeDuration: zc.SecondsDurationEncoder,
241 EncodeCaller: zc.ShortCallerEncoder,
242 },
243 }
244}
245
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000246func ConstructZapConfig(outputType string, level LogLevel, fields Fields) zp.Config {
247 return getDefaultConfig(outputType, level, fields)
248}
249
Don Newton7577f072020-01-06 12:41:11 -0500250// SetLogger needs to be invoked before the logger API can be invoked. This function
251// initialize the default logger (zap's sugaredlogger)
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000252func SetDefaultLogger(outputType string, level LogLevel, defaultFields Fields) (CLogger, error) {
Don Newton7577f072020-01-06 12:41:11 -0500253 // Build a custom config using zap
254 cfg = getDefaultConfig(outputType, level, defaultFields)
255
divyadesai81bb7ba2020-03-11 11:45:23 +0000256 l, err := cfg.Build(zp.AddCallerSkip(1))
Don Newton7577f072020-01-06 12:41:11 -0500257 if err != nil {
258 return nil, err
259 }
260
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000261 defaultLogger = &clogger{
Don Newton7577f072020-01-06 12:41:11 -0500262 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.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000278func RegisterPackage(outputType string, level LogLevel, defaultFields Fields, pkgNames ...string) (CLogger, error) {
Don Newton7577f072020-01-06 12:41:11 -0500279 if cfgs == nil {
280 cfgs = make(map[string]zp.Config)
281 }
282 if loggers == nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000283 loggers = make(map[string]*clogger)
Don Newton7577f072020-01-06 12:41:11 -0500284 }
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
divyadesai81bb7ba2020-03-11 11:45:23 +0000301 l, err := cfgs[pkgName].Build(zp.AddCallerSkip(1))
Don Newton7577f072020-01-06 12:41:11 -0500302 if err != nil {
303 return nil, err
304 }
305
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000306 loggers[pkgName] = &clogger{
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -0800307 log: l.Sugar(),
308 parent: l,
309 packageName: pkgName,
Don Newton7577f072020-01-06 12:41:11 -0500310 }
311 return loggers[pkgName], nil
312}
313
314//UpdateAllLoggers create new loggers for all registered pacakges with the defaultFields.
315func 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 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000323 l, err := cfg.Build(zp.AddCallerSkip(1))
Don Newton7577f072020-01-06 12:41:11 -0500324 if err != nil {
325 return err
326 }
327
divyadesai81bb7ba2020-03-11 11:45:23 +0000328 // Update the existing zap logger instance
329 loggers[pkgName].log = l.Sugar()
330 loggers[pkgName].parent = l
Don Newton7577f072020-01-06 12:41:11 -0500331 }
332 return nil
333}
334
335// Return a list of all packages that have individually-configured loggers
336func 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
divyadesai81bb7ba2020-03-11 11:45:23 +0000346// UpdateLogger updates the logger associated with a caller's package with supplied defaultFields
347func UpdateLogger(defaultFields Fields) error {
Don Newton7577f072020-01-06 12:41:11 -0500348 pkgName, _, _, _ := getCallerInfo()
349 if _, exist := loggers[pkgName]; !exist {
divyadesai81bb7ba2020-03-11 11:45:23 +0000350 return fmt.Errorf("package-%s-not-registered", pkgName)
Don Newton7577f072020-01-06 12:41:11 -0500351 }
352
353 // Build a new logger
354 if _, exist := cfgs[pkgName]; !exist {
divyadesai81bb7ba2020-03-11 11:45:23 +0000355 return fmt.Errorf("config-%s-not-registered", pkgName)
Don Newton7577f072020-01-06 12:41:11 -0500356 }
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 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000365 l, err := cfg.Build(zp.AddCallerSkip(1))
Don Newton7577f072020-01-06 12:41:11 -0500366 if err != nil {
divyadesai81bb7ba2020-03-11 11:45:23 +0000367 return err
Don Newton7577f072020-01-06 12:41:11 -0500368 }
369
divyadesai81bb7ba2020-03-11 11:45:23 +0000370 // Update the existing zap logger instance
371 loggers[pkgName].log = l.Sugar()
372 loggers[pkgName].parent = l
373
374 return nil
Don Newton7577f072020-01-06 12:41:11 -0500375}
376
divyadesai81bb7ba2020-03-11 11:45:23 +0000377func setLevel(cfg zp.Config, level LogLevel) {
Don Newton7577f072020-01-06 12:41:11 -0500378 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)
Don Newton7577f072020-01-06 12:41:11 -0500387 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
divyadesai81bb7ba2020-03-11 11:45:23 +0000396func SetPackageLogLevel(packageName string, level LogLevel) {
Don Newton7577f072020-01-06 12:41:11 -0500397 // 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
divyadesai81bb7ba2020-03-11 11:45:23 +0000404func SetAllLogLevel(level LogLevel) {
Don Newton7577f072020-01-06 12:41:11 -0500405 // 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.
divyadesai81bb7ba2020-03-11 11:45:23 +0000412func GetPackageLogLevel(packageName ...string) (LogLevel, error) {
Don Newton7577f072020-01-06 12:41:11 -0500413 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 {
divyadesai81bb7ba2020-03-11 11:45:23 +0000420 return levelToLogLevel(cfg.Level.Level()), nil
Don Newton7577f072020-01-06 12:41:11 -0500421 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000422 return 0, fmt.Errorf("unknown-package-%s", name)
Don Newton7577f072020-01-06 12:41:11 -0500423}
424
425//GetDefaultLogLevel gets the log level used for packages that don't have specific loggers
divyadesai81bb7ba2020-03-11 11:45:23 +0000426func GetDefaultLogLevel() LogLevel {
427 return levelToLogLevel(cfg.Level.Level())
Don Newton7577f072020-01-06 12:41:11 -0500428}
429
430//SetLogLevel sets the log level for the logger corresponding to the caller's package
divyadesai81bb7ba2020-03-11 11:45:23 +0000431func SetLogLevel(level LogLevel) error {
Don Newton7577f072020-01-06 12:41:11 -0500432 pkgName, _, _, _ := getCallerInfo()
433 if _, exist := cfgs[pkgName]; !exist {
divyadesai81bb7ba2020-03-11 11:45:23 +0000434 return fmt.Errorf("unregistered-package-%s", pkgName)
Don Newton7577f072020-01-06 12:41:11 -0500435 }
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
divyadesai81bb7ba2020-03-11 11:45:23 +0000442func SetDefaultLogLevel(level LogLevel) {
Don Newton7577f072020-01-06 12:41:11 -0500443 setLevel(cfg, level)
444}
445
446// CleanUp flushed any buffered log entries. Applications should take care to call
447// CleanUp before exiting.
448func 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
468func 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
Don Newton7577f072020-01-06 12:41:11 -0500516// With returns a logger initialized with the key-value pairs
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000517func (l clogger) With(keysAndValues Fields) CLogger {
518 return clogger{log: l.log.With(serializeMap(keysAndValues)...), parent: l.parent}
Don Newton7577f072020-01-06 12:41:11 -0500519}
520
521// Debug logs a message at level Debug on the standard logger.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000522func (l clogger) Debug(ctx context.Context, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000523 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Debug(args...)
Don Newton7577f072020-01-06 12:41:11 -0500524}
525
526// Debugln logs a message at level Debug on the standard logger with a line feed. Default in any case.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000527func (l clogger) Debugln(ctx context.Context, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000528 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Debug(args...)
Don Newton7577f072020-01-06 12:41:11 -0500529}
530
531// Debugw logs a message at level Debug on the standard logger.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000532func (l clogger) Debugf(ctx context.Context, format string, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000533 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Debugf(format, args...)
Don Newton7577f072020-01-06 12:41:11 -0500534}
535
536// Debugw logs a message with some additional context. The variadic key-value
537// pairs are treated as they are in With.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000538func (l clogger) Debugw(ctx context.Context, msg string, keysAndValues Fields) {
Neha Sharma87d43d72020-04-08 14:10:40 +0000539 if l.V(DebugLevel) {
Girish Kumarcd402012020-08-18 12:17:38 +0000540 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Debugw(msg, serializeMap(keysAndValues)...)
Neha Sharma87d43d72020-04-08 14:10:40 +0000541 }
Don Newton7577f072020-01-06 12:41:11 -0500542}
543
544// Info logs a message at level Info on the standard logger.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000545func (l clogger) Info(ctx context.Context, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000546 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Info(args...)
Don Newton7577f072020-01-06 12:41:11 -0500547}
548
549// Infoln logs a message at level Info on the standard logger with a line feed. Default in any case.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000550func (l clogger) Infoln(ctx context.Context, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000551 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Info(args...)
Don Newton7577f072020-01-06 12:41:11 -0500552 //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.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000557func (l clogger) Infof(ctx context.Context, format string, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000558 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Infof(format, args...)
Don Newton7577f072020-01-06 12:41:11 -0500559}
560
561// Infow logs a message with some additional context. The variadic key-value
562// pairs are treated as they are in With.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000563func (l clogger) Infow(ctx context.Context, msg string, keysAndValues Fields) {
Neha Sharma87d43d72020-04-08 14:10:40 +0000564 if l.V(InfoLevel) {
Girish Kumarcd402012020-08-18 12:17:38 +0000565 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Infow(msg, serializeMap(keysAndValues)...)
Neha Sharma87d43d72020-04-08 14:10:40 +0000566 }
Don Newton7577f072020-01-06 12:41:11 -0500567}
568
569// Warn logs a message at level Warn on the standard logger.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000570func (l clogger) Warn(ctx context.Context, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000571 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warn(args...)
Don Newton7577f072020-01-06 12:41:11 -0500572}
573
574// Warnln logs a message at level Warn on the standard logger with a line feed. Default in any case.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000575func (l clogger) Warnln(ctx context.Context, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000576 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warn(args...)
Don Newton7577f072020-01-06 12:41:11 -0500577}
578
579// Warnf logs a message at level Warn on the standard logger.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000580func (l clogger) Warnf(ctx context.Context, format string, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000581 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warnf(format, args...)
Don Newton7577f072020-01-06 12:41:11 -0500582}
583
584// Warnw logs a message with some additional context. The variadic key-value
585// pairs are treated as they are in With.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000586func (l clogger) Warnw(ctx context.Context, msg string, keysAndValues Fields) {
Neha Sharma87d43d72020-04-08 14:10:40 +0000587 if l.V(WarnLevel) {
Girish Kumarcd402012020-08-18 12:17:38 +0000588 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warnw(msg, serializeMap(keysAndValues)...)
Neha Sharma87d43d72020-04-08 14:10:40 +0000589 }
Don Newton7577f072020-01-06 12:41:11 -0500590}
591
592// Error logs a message at level Error on the standard logger.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000593func (l clogger) Error(ctx context.Context, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000594 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Error(args...)
Don Newton7577f072020-01-06 12:41:11 -0500595}
596
597// Errorln logs a message at level Error on the standard logger with a line feed. Default in any case.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000598func (l clogger) Errorln(ctx context.Context, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000599 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Error(args...)
Don Newton7577f072020-01-06 12:41:11 -0500600}
601
602// Errorf logs a message at level Error on the standard logger.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000603func (l clogger) Errorf(ctx context.Context, format string, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000604 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Errorf(format, args...)
Don Newton7577f072020-01-06 12:41:11 -0500605}
606
607// Errorw logs a message with some additional context. The variadic key-value
608// pairs are treated as they are in With.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000609func (l clogger) Errorw(ctx context.Context, msg string, keysAndValues Fields) {
Neha Sharma87d43d72020-04-08 14:10:40 +0000610 if l.V(ErrorLevel) {
Girish Kumarcd402012020-08-18 12:17:38 +0000611 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Errorw(msg, serializeMap(keysAndValues)...)
Neha Sharma87d43d72020-04-08 14:10:40 +0000612 }
Don Newton7577f072020-01-06 12:41:11 -0500613}
614
615// Fatal logs a message at level Fatal on the standard logger.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000616func (l clogger) Fatal(ctx context.Context, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000617 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Fatal(args...)
Don Newton7577f072020-01-06 12:41:11 -0500618}
619
620// Fatalln logs a message at level Fatal on the standard logger with a line feed. Default in any case.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000621func (l clogger) Fatalln(ctx context.Context, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000622 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Fatal(args...)
Don Newton7577f072020-01-06 12:41:11 -0500623}
624
625// Fatalf logs a message at level Fatal on the standard logger.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000626func (l clogger) Fatalf(ctx context.Context, format string, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000627 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Fatalf(format, args...)
Don Newton7577f072020-01-06 12:41:11 -0500628}
629
630// Fatalw logs a message with some additional context. The variadic key-value
631// pairs are treated as they are in With.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000632func (l clogger) Fatalw(ctx context.Context, msg string, keysAndValues Fields) {
Neha Sharma87d43d72020-04-08 14:10:40 +0000633 if l.V(FatalLevel) {
Girish Kumarcd402012020-08-18 12:17:38 +0000634 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Fatalw(msg, serializeMap(keysAndValues)...)
Neha Sharma87d43d72020-04-08 14:10:40 +0000635 }
Don Newton7577f072020-01-06 12:41:11 -0500636}
637
638// Warning logs a message at level Warn on the standard logger.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000639func (l clogger) Warning(ctx context.Context, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000640 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warn(args...)
Don Newton7577f072020-01-06 12:41:11 -0500641}
642
643// Warningln logs a message at level Warn on the standard logger with a line feed. Default in any case.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000644func (l clogger) Warningln(ctx context.Context, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000645 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warn(args...)
Don Newton7577f072020-01-06 12:41:11 -0500646}
647
648// Warningf logs a message at level Warn on the standard logger.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000649func (l clogger) Warningf(ctx context.Context, format string, args ...interface{}) {
Girish Kumarcd402012020-08-18 12:17:38 +0000650 l.log.With(GetGlobalLFM().ExtractContextAttributes(ctx)...).Warnf(format, args...)
Don Newton7577f072020-01-06 12:41:11 -0500651}
652
653// V reports whether verbosity level l is at least the requested verbose level.
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000654func (l clogger) V(level LogLevel) bool {
divyadesai81bb7ba2020-03-11 11:45:23 +0000655 return l.parent.Core().Enabled(logLevelToLevel(level))
Don Newton7577f072020-01-06 12:41:11 -0500656}
657
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -0800658// GetLogLevel returns the current level of the logger
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000659func (l clogger) GetLogLevel() LogLevel {
divyadesai81bb7ba2020-03-11 11:45:23 +0000660 return levelToLogLevel(cfgs[l.packageName].Level.Level())
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -0800661}