blob: 408158aeea5f699b162b30fbd3498c1ed123ce7a [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 */
khenaidoo7da372d2018-09-21 16:03:09 -040016
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
khenaidoocfee5f42018-07-19 22:47:38 -040041package log
42
43import (
khenaidoo7da372d2018-09-21 16:03:09 -040044 "errors"
khenaidoo5c11af72018-07-20 17:21:05 -040045 "fmt"
khenaidoocfee5f42018-07-19 22:47:38 -040046 zp "go.uber.org/zap"
47 zc "go.uber.org/zap/zapcore"
khenaidoob9203542018-09-17 22:56:37 -040048 "path"
khenaidoocfee5f42018-07-19 22:47:38 -040049 "runtime"
50 "strings"
khenaidoocfee5f42018-07-19 22:47:38 -040051)
52
53const (
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
69const CONSOLE = "console"
khenaidoo5c11af72018-07-20 17:21:05 -040070
khenaidoocfee5f42018-07-19 22:47:38 -040071// JSON formats the log using json format, mostly used by an automated logging system consumption
72const JSON = "json"
73
74// Logger represents an abstract logging interface. Any logging implementation used
75// will need to abide by this interface
76type 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
khenaidoo7da372d2018-09-21 16:03:09 -0400103
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
khenaidoocfee5f42018-07-19 22:47:38 -0400112}
113
khenaidoo7da372d2018-09-21 16:03:09 -0400114// Fields is used as key-value pairs for structured logging
khenaidoocfee5f42018-07-19 22:47:38 -0400115type Fields map[string]interface{}
116
117var defaultLogger *logger
khenaidoob9203542018-09-17 22:56:37 -0400118var cfg zp.Config
119
120var loggers map[string]*logger
121var cfgs map[string]zp.Config
khenaidoocfee5f42018-07-19 22:47:38 -0400122
123type logger struct {
124 log *zp.SugaredLogger
125 parent *zp.Logger
126}
127
khenaidoo6f2fbe32019-01-18 16:16:50 -0500128func intToAtomicLevel(l int) zp.AtomicLevel {
khenaidoocfee5f42018-07-19 22:47:38 -0400129 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
khenaidoo6f2fbe32019-01-18 16:16:50 -0500146func intToLevel(l int) zc.Level {
khenaidoo7da372d2018-09-21 16:03:09 -0400147 switch l {
148 case DebugLevel:
149 return zc.DebugLevel
150 case InfoLevel:
khenaidoo6f2fbe32019-01-18 16:16:50 -0500151 return zc.InfoLevel
khenaidoo7da372d2018-09-21 16:03:09 -0400152 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
khenaidoo6f2fbe32019-01-18 16:16:50 -0500164func 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
182
khenaidoocfee5f42018-07-19 22:47:38 -0400183func getDefaultConfig(outputType string, level int, defaultFields Fields) zp.Config {
184 return zp.Config{
khenaidoo6f2fbe32019-01-18 16:16:50 -0500185 Level: intToAtomicLevel(level),
khenaidoocfee5f42018-07-19 22:47:38 -0400186 Encoding: outputType,
187 Development: true,
188 OutputPaths: []string{"stdout"},
189 ErrorOutputPaths: []string{"stderr"},
190 InitialFields: defaultFields,
191 EncoderConfig: zc.EncoderConfig{
192 LevelKey: "level",
193 MessageKey: "msg",
194 TimeKey: "ts",
195 StacktraceKey: "stacktrace",
196 LineEnding: zc.DefaultLineEnding,
197 EncodeLevel: zc.LowercaseLevelEncoder,
198 EncodeTime: zc.ISO8601TimeEncoder,
199 EncodeDuration: zc.SecondsDurationEncoder,
200 EncodeCaller: zc.ShortCallerEncoder,
201 },
202 }
203}
204
205// SetLogger needs to be invoked before the logger API can be invoked. This function
206// initialize the default logger (zap's sugaredlogger)
khenaidoob9203542018-09-17 22:56:37 -0400207func SetDefaultLogger(outputType string, level int, defaultFields Fields) (Logger, error) {
khenaidoocfee5f42018-07-19 22:47:38 -0400208 // Build a custom config using zap
khenaidood4d922e2018-08-03 22:35:16 -0400209 cfg = getDefaultConfig(outputType, level, defaultFields)
khenaidoocfee5f42018-07-19 22:47:38 -0400210
211 l, err := cfg.Build()
212 if err != nil {
213 return nil, err
214 }
215
216 defaultLogger = &logger{
217 log: l.Sugar(),
218 parent: l,
219 }
220
221 return defaultLogger, nil
222}
223
khenaidoo7da372d2018-09-21 16:03:09 -0400224// AddPackage registers a package to the log map. Each package gets its own logger which allows
225// its config (loglevel) to be changed dynamically without interacting with the other packages.
226// outputType is JSON, level is the lowest level log to output with this logger and defaultFields is a map of
227// key-value pairs to always add to the output.
228// Note: AddPackage also returns a reference to the actual logger. If a calling package uses this reference directly
229//instead of using the publicly available functions in this log package then a number of functionalities will not
230// be available to it, notably log tracing with filename.functionname.linenumber annotation.
231//
khenaidoo6f2fbe32019-01-18 16:16:50 -0500232// pkgNames parameter should be used for testing only as this function detects the caller's package.
233func AddPackage(outputType string, level int, defaultFields Fields, pkgNames ...string) (Logger, error) {
khenaidoob9203542018-09-17 22:56:37 -0400234 if cfgs == nil {
235 cfgs = make(map[string]zp.Config)
236 }
237 if loggers == nil {
238 loggers = make(map[string]*logger)
239 }
khenaidoo6f2fbe32019-01-18 16:16:50 -0500240
241 var pkgName string
242 for _, name := range pkgNames {
243 pkgName = name
244 break
245 }
246 if pkgName == "" {
247 pkgName, _, _, _ = getCallerInfo()
248 }
khenaidoob9203542018-09-17 22:56:37 -0400249
khenaidoo7da372d2018-09-21 16:03:09 -0400250 if _, exist := loggers[pkgName]; exist {
251 return loggers[pkgName], nil
khenaidoob9203542018-09-17 22:56:37 -0400252 }
khenaidoo7da372d2018-09-21 16:03:09 -0400253
khenaidoob9203542018-09-17 22:56:37 -0400254 cfgs[pkgName] = getDefaultConfig(outputType, level, defaultFields)
255
256 l, err := cfgs[pkgName].Build()
257 if err != nil {
khenaidoo7da372d2018-09-21 16:03:09 -0400258 return nil, err
khenaidoob9203542018-09-17 22:56:37 -0400259 }
260
261 loggers[pkgName] = &logger{
262 log: l.Sugar(),
263 parent: l,
264 }
khenaidoo7da372d2018-09-21 16:03:09 -0400265 return loggers[pkgName], nil
khenaidoob9203542018-09-17 22:56:37 -0400266}
267
khenaidoo7da372d2018-09-21 16:03:09 -0400268//UpdateAllLoggers create new loggers for all registered pacakges with the defaultFields.
khenaidoob9203542018-09-17 22:56:37 -0400269func UpdateAllLoggers(defaultFields Fields) error {
270 for pkgName, cfg := range cfgs {
271 for k, v := range defaultFields {
272 if cfg.InitialFields == nil {
273 cfg.InitialFields = make(map[string]interface{})
274 }
275 cfg.InitialFields[k] = v
276 }
277 l, err := cfg.Build()
278 if err != nil {
279 return err
280 }
281
282 loggers[pkgName] = &logger{
283 log: l.Sugar(),
284 parent: l,
285 }
286 }
287 return nil
288}
289
khenaidoo7da372d2018-09-21 16:03:09 -0400290// UpdateLogger deletes the logger associated with a caller's package and creates a new logger with the
291// defaultFields. If a calling package is holding on to a Logger reference obtained from AddPackage invocation, then
292// that package needs to invoke UpdateLogger if it needs to make changes to the default fields and obtain a new logger
293// reference
294func UpdateLogger(defaultFields Fields) (Logger, error) {
295 pkgName, _, _, _ := getCallerInfo()
296 if _, exist := loggers[pkgName]; !exist {
297 return nil, errors.New(fmt.Sprintf("package-%s-not-registered", pkgName))
298 }
khenaidoob9203542018-09-17 22:56:37 -0400299
khenaidoo7da372d2018-09-21 16:03:09 -0400300 // Build a new logger
301 if _, exist := cfgs[pkgName]; !exist {
302 return nil, errors.New(fmt.Sprintf("config-%s-not-registered", pkgName))
303 }
304
305 cfg := cfgs[pkgName]
306 for k, v := range defaultFields {
307 if cfg.InitialFields == nil {
308 cfg.InitialFields = make(map[string]interface{})
309 }
310 cfg.InitialFields[k] = v
311 }
312 l, err := cfg.Build()
313 if err != nil {
314 return nil, err
315 }
316
317 // Set the logger
318 loggers[pkgName] = &logger{
319 log: l.Sugar(),
320 parent: l,
321 }
322 return loggers[pkgName], nil
323}
324
khenaidoo6f2fbe32019-01-18 16:16:50 -0500325func setLevel(cfg zp.Config, level int) {
khenaidoo7da372d2018-09-21 16:03:09 -0400326 switch level {
327 case DebugLevel:
328 cfg.Level.SetLevel(zc.DebugLevel)
329 case InfoLevel:
330 cfg.Level.SetLevel(zc.InfoLevel)
331 case WarnLevel:
332 cfg.Level.SetLevel(zc.WarnLevel)
333 case ErrorLevel:
334 cfg.Level.SetLevel(zc.ErrorLevel)
335 case PanicLevel:
336 cfg.Level.SetLevel(zc.PanicLevel)
337 case FatalLevel:
338 cfg.Level.SetLevel(zc.FatalLevel)
339 default:
340 cfg.Level.SetLevel(zc.ErrorLevel)
341 }
khenaidoo6f2fbe32019-01-18 16:16:50 -0500342}
343
344//SetPackageLogLevel dynamically sets the log level of a given package to level. This is typically invoked at an
345// application level during debugging
346func SetPackageLogLevel(packageName string, level int) {
347 // Get proper config
348 if cfg, ok := cfgs[packageName]; ok {
349 setLevel(cfg, level)
350 }
351}
352
353//SetAllLogLevel sets the log level of all registered packages to level
354func SetAllLogLevel(level int) {
355 // Get proper config
356 for _, cfg := range cfgs {
357 setLevel(cfg, level)
358 }
359}
360
361//GetPackageLogLevel returns the current log level of a package.
362func GetPackageLogLevel(packageName string) (int, error) {
363 if cfg, ok := cfgs[packageName]; ok {
364 return levelToInt(cfg.Level.Level()), nil
365 }
366 return 0, errors.New(fmt.Sprintf("unknown-package-%s", packageName))
367}
368
369//SetLogLevel sets the log level for the logger corresponding to the caller's package
370func SetLogLevel(level int) error {
371 pkgName, _, _, _ := getCallerInfo()
372 if _, exist := cfgs[pkgName]; !exist {
373 return errors.New(fmt.Sprintf("unregistered-package-%s", pkgName))
374 }
375 cfg := cfgs[pkgName]
376 setLevel(cfg, level)
khenaidoo7da372d2018-09-21 16:03:09 -0400377 return nil
378}
379
khenaidoocfee5f42018-07-19 22:47:38 -0400380// CleanUp flushed any buffered log entries. Applications should take care to call
381// CleanUp before exiting.
382func CleanUp() error {
khenaidoob9203542018-09-17 22:56:37 -0400383 for _, logger := range loggers {
384 if logger != nil {
385 if logger.parent != nil {
386 if err := logger.parent.Sync(); err != nil {
387 return err
388 }
389 }
390 }
391 }
khenaidoocfee5f42018-07-19 22:47:38 -0400392 if defaultLogger != nil {
393 if defaultLogger.parent != nil {
394 if err := defaultLogger.parent.Sync(); err != nil {
395 return err
396 }
397 }
398 }
399 return nil
400}
401
khenaidoo7da372d2018-09-21 16:03:09 -0400402func getCallerInfo() (string, string, string, int) {
khenaidoo2c6f1672018-09-20 23:14:41 -0400403 // Since the caller of a log function is one stack frame before (in terms of stack higher level) the log.go
404 // filename, then first look for the last log.go filename and then grab the caller info one level higher.
405 maxLevel := 3
khenaidoo7da372d2018-09-21 16:03:09 -0400406 skiplevel := 3 // Level with the most empirical success to see the last log.go stack frame.
khenaidoo2c6f1672018-09-20 23:14:41 -0400407 pc := make([]uintptr, maxLevel)
408 n := runtime.Callers(skiplevel, pc)
khenaidoob9203542018-09-17 22:56:37 -0400409 packageName := ""
khenaidoo2c6f1672018-09-20 23:14:41 -0400410 funcName := ""
411 fileName := ""
412 var line int
413 if n == 0 {
414 return packageName, fileName, funcName, line
415 }
416 frames := runtime.CallersFrames(pc[:n])
417 var frame runtime.Frame
418 var foundFrame runtime.Frame
419 more := true
420 for more {
421 frame, more = frames.Next()
422 _, fileName = path.Split(frame.File)
423 if fileName != "log.go" {
424 foundFrame = frame // First frame after log.go in the frame stack
425 break
426 }
427 }
428 parts := strings.Split(foundFrame.Function, ".")
429 pl := len(parts)
430 if pl >= 2 {
431 funcName = parts[pl-1]
432 if parts[pl-2][0] == '(' {
433 packageName = strings.Join(parts[0:pl-2], ".")
434 } else {
435 packageName = strings.Join(parts[0:pl-1], ".")
436 }
khenaidoocfee5f42018-07-19 22:47:38 -0400437 }
438
khenaidoo2c6f1672018-09-20 23:14:41 -0400439 if strings.HasSuffix(packageName, ".init") {
khenaidoo7da372d2018-09-21 16:03:09 -0400440 packageName = strings.TrimSuffix(packageName, ".init")
khenaidoob9203542018-09-17 22:56:37 -0400441 }
442
443 if strings.HasSuffix(fileName, ".go") {
khenaidoo7da372d2018-09-21 16:03:09 -0400444 fileName = strings.TrimSuffix(fileName, ".go")
khenaidoob9203542018-09-17 22:56:37 -0400445 }
khenaidoo2c6f1672018-09-20 23:14:41 -0400446
447 return packageName, fileName, funcName, foundFrame.Line
khenaidoob9203542018-09-17 22:56:37 -0400448}
449
khenaidoo7da372d2018-09-21 16:03:09 -0400450func getPackageLevelSugaredLogger() *zp.SugaredLogger {
khenaidoob9203542018-09-17 22:56:37 -0400451 pkgName, fileName, funcName, line := getCallerInfo()
452 if _, exist := loggers[pkgName]; exist {
453 return loggers[pkgName].log.With("caller", fmt.Sprintf("%s.%s:%d", fileName, funcName, line))
454 }
455 return defaultLogger.log.With("caller", fmt.Sprintf("%s.%s:%d", fileName, funcName, line))
456}
khenaidoocfee5f42018-07-19 22:47:38 -0400457
khenaidoo7da372d2018-09-21 16:03:09 -0400458func getPackageLevelLogger() Logger {
459 pkgName, _, _, _ := getCallerInfo()
460 if _, exist := loggers[pkgName]; exist {
461 return loggers[pkgName]
462 }
463 return defaultLogger
464}
465
khenaidoocfee5f42018-07-19 22:47:38 -0400466func serializeMap(fields Fields) []interface{} {
467 data := make([]interface{}, len(fields)*2)
468 i := 0
469 for k, v := range fields {
470 data[i] = k
471 data[i+1] = v
472 i = i + 2
473 }
474 return data
475}
476
477// With returns a logger initialized with the key-value pairs
478func (l logger) With(keysAndValues Fields) Logger {
479 return logger{log: l.log.With(serializeMap(keysAndValues)...), parent: l.parent}
480}
481
482// Debug logs a message at level Debug on the standard logger.
483func (l logger) Debug(args ...interface{}) {
484 l.log.Debug(args...)
485}
486
487// Debugln logs a message at level Debug on the standard logger with a line feed. Default in any case.
488func (l logger) Debugln(args ...interface{}) {
489 l.log.Debug(args...)
490}
491
492// Debugw logs a message at level Debug on the standard logger.
493func (l logger) Debugf(format string, args ...interface{}) {
494 l.log.Debugf(format, args...)
495}
496
497// Debugw logs a message with some additional context. The variadic key-value
498// pairs are treated as they are in With.
499func (l logger) Debugw(msg string, keysAndValues Fields) {
500 l.log.Debugw(msg, serializeMap(keysAndValues)...)
501}
502
503// Info logs a message at level Info on the standard logger.
504func (l logger) Info(args ...interface{}) {
505 l.log.Info(args...)
506}
507
508// Infoln logs a message at level Info on the standard logger with a line feed. Default in any case.
509func (l logger) Infoln(args ...interface{}) {
510 l.log.Info(args...)
511 //msg := fmt.Sprintln(args...)
512 //l.sourced().Info(msg[:len(msg)-1])
513}
514
515// Infof logs a message at level Info on the standard logger.
516func (l logger) Infof(format string, args ...interface{}) {
517 l.log.Infof(format, args...)
518}
519
520// Infow logs a message with some additional context. The variadic key-value
521// pairs are treated as they are in With.
522func (l logger) Infow(msg string, keysAndValues Fields) {
523 l.log.Infow(msg, serializeMap(keysAndValues)...)
524}
525
526// Warn logs a message at level Warn on the standard logger.
527func (l logger) Warn(args ...interface{}) {
528 l.log.Warn(args...)
529}
530
531// Warnln logs a message at level Warn on the standard logger with a line feed. Default in any case.
532func (l logger) Warnln(args ...interface{}) {
533 l.log.Warn(args...)
534}
535
536// Warnf logs a message at level Warn on the standard logger.
537func (l logger) Warnf(format string, args ...interface{}) {
538 l.log.Warnf(format, args...)
539}
540
541// Warnw logs a message with some additional context. The variadic key-value
542// pairs are treated as they are in With.
543func (l logger) Warnw(msg string, keysAndValues Fields) {
544 l.log.Warnw(msg, serializeMap(keysAndValues)...)
545}
546
547// Error logs a message at level Error on the standard logger.
548func (l logger) Error(args ...interface{}) {
549 l.log.Error(args...)
550}
551
552// Errorln logs a message at level Error on the standard logger with a line feed. Default in any case.
553func (l logger) Errorln(args ...interface{}) {
554 l.log.Error(args...)
555}
556
557// Errorf logs a message at level Error on the standard logger.
558func (l logger) Errorf(format string, args ...interface{}) {
559 l.log.Errorf(format, args...)
560}
561
562// Errorw logs a message with some additional context. The variadic key-value
563// pairs are treated as they are in With.
564func (l logger) Errorw(msg string, keysAndValues Fields) {
565 l.log.Errorw(msg, serializeMap(keysAndValues)...)
566}
567
568// Fatal logs a message at level Fatal on the standard logger.
569func (l logger) Fatal(args ...interface{}) {
570 l.log.Fatal(args...)
571}
572
573// Fatalln logs a message at level Fatal on the standard logger with a line feed. Default in any case.
574func (l logger) Fatalln(args ...interface{}) {
575 l.log.Fatal(args...)
576}
577
578// Fatalf logs a message at level Fatal on the standard logger.
579func (l logger) Fatalf(format string, args ...interface{}) {
580 l.log.Fatalf(format, args...)
581}
582
583// Fatalw logs a message with some additional context. The variadic key-value
584// pairs are treated as they are in With.
585func (l logger) Fatalw(msg string, keysAndValues Fields) {
586 l.log.Fatalw(msg, serializeMap(keysAndValues)...)
587}
588
khenaidoo7da372d2018-09-21 16:03:09 -0400589// Warning logs a message at level Warn on the standard logger.
590func (l logger) Warning(args ...interface{}) {
591 l.log.Warn(args...)
592}
593
594// Warningln logs a message at level Warn on the standard logger with a line feed. Default in any case.
595func (l logger) Warningln(args ...interface{}) {
596 l.log.Warn(args...)
597}
598
599// Warningf logs a message at level Warn on the standard logger.
600func (l logger) Warningf(format string, args ...interface{}) {
601 l.log.Warnf(format, args...)
602}
603
604// V reports whether verbosity level l is at least the requested verbose level.
605func (l logger) V(level int) bool {
khenaidoo6f2fbe32019-01-18 16:16:50 -0500606 return l.parent.Core().Enabled(intToLevel(level))
khenaidoo7da372d2018-09-21 16:03:09 -0400607}
608
khenaidoocfee5f42018-07-19 22:47:38 -0400609// With returns a logger initialized with the key-value pairs
610func With(keysAndValues Fields) Logger {
khenaidoo7da372d2018-09-21 16:03:09 -0400611 return logger{log: getPackageLevelSugaredLogger().With(serializeMap(keysAndValues)...), parent: defaultLogger.parent}
khenaidoocfee5f42018-07-19 22:47:38 -0400612}
613
614// Debug logs a message at level Debug on the standard logger.
615func Debug(args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400616 getPackageLevelSugaredLogger().Debug(args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400617}
618
619// Debugln logs a message at level Debug on the standard logger.
620func Debugln(args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400621 getPackageLevelSugaredLogger().Debug(args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400622}
623
624// Debugf logs a message at level Debug on the standard logger.
625func Debugf(format string, args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400626 getPackageLevelSugaredLogger().Debugf(format, args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400627}
628
629// Debugw logs a message with some additional context. The variadic key-value
630// pairs are treated as they are in With.
631func Debugw(msg string, keysAndValues Fields) {
khenaidoo7da372d2018-09-21 16:03:09 -0400632 getPackageLevelSugaredLogger().Debugw(msg, serializeMap(keysAndValues)...)
khenaidoocfee5f42018-07-19 22:47:38 -0400633}
634
635// Info logs a message at level Info on the standard logger.
636func Info(args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400637 getPackageLevelSugaredLogger().Info(args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400638}
639
640// Infoln logs a message at level Info on the standard logger.
641func Infoln(args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400642 getPackageLevelSugaredLogger().Info(args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400643}
644
645// Infof logs a message at level Info on the standard logger.
646func Infof(format string, args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400647 getPackageLevelSugaredLogger().Infof(format, args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400648}
649
650//Infow logs a message with some additional context. The variadic key-value
651//pairs are treated as they are in With.
652func Infow(msg string, keysAndValues Fields) {
khenaidoo7da372d2018-09-21 16:03:09 -0400653 getPackageLevelSugaredLogger().Infow(msg, serializeMap(keysAndValues)...)
khenaidoocfee5f42018-07-19 22:47:38 -0400654}
655
656// Warn logs a message at level Warn on the standard logger.
657func Warn(args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400658 getPackageLevelSugaredLogger().Warn(args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400659}
660
661// Warnln logs a message at level Warn on the standard logger.
662func Warnln(args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400663 getPackageLevelSugaredLogger().Warn(args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400664}
665
666// Warnf logs a message at level Warn on the standard logger.
667func Warnf(format string, args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400668 getPackageLevelSugaredLogger().Warnf(format, args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400669}
670
671// Warnw logs a message with some additional context. The variadic key-value
672// pairs are treated as they are in With.
673func Warnw(msg string, keysAndValues Fields) {
khenaidoo7da372d2018-09-21 16:03:09 -0400674 getPackageLevelSugaredLogger().Warnw(msg, serializeMap(keysAndValues)...)
khenaidoocfee5f42018-07-19 22:47:38 -0400675}
676
677// Error logs a message at level Error on the standard logger.
678func Error(args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400679 getPackageLevelSugaredLogger().Error(args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400680}
681
682// Errorln logs a message at level Error on the standard logger.
683func Errorln(args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400684 getPackageLevelSugaredLogger().Error(args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400685}
686
687// Errorf logs a message at level Error on the standard logger.
688func Errorf(format string, args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400689 getPackageLevelSugaredLogger().Errorf(format, args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400690}
691
692// Errorw logs a message with some additional context. The variadic key-value
693// pairs are treated as they are in With.
694func Errorw(msg string, keysAndValues Fields) {
khenaidoo7da372d2018-09-21 16:03:09 -0400695 getPackageLevelSugaredLogger().Errorw(msg, serializeMap(keysAndValues)...)
khenaidoocfee5f42018-07-19 22:47:38 -0400696}
697
698// Fatal logs a message at level Fatal on the standard logger.
699func Fatal(args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400700 getPackageLevelSugaredLogger().Fatal(args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400701}
702
703// Fatalln logs a message at level Fatal on the standard logger.
704func Fatalln(args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400705 getPackageLevelSugaredLogger().Fatal(args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400706}
707
708// Fatalf logs a message at level Fatal on the standard logger.
709func Fatalf(format string, args ...interface{}) {
khenaidoo7da372d2018-09-21 16:03:09 -0400710 getPackageLevelSugaredLogger().Fatalf(format, args...)
khenaidoocfee5f42018-07-19 22:47:38 -0400711}
712
713// Fatalw logs a message with some additional context. The variadic key-value
714// pairs are treated as they are in With.
715func Fatalw(msg string, keysAndValues Fields) {
khenaidoo7da372d2018-09-21 16:03:09 -0400716 getPackageLevelSugaredLogger().Fatalw(msg, serializeMap(keysAndValues)...)
717}
718
719// Warning logs a message at level Warn on the standard logger.
720func Warning(args ...interface{}) {
721 getPackageLevelSugaredLogger().Warn(args...)
722}
723
724// Warningln logs a message at level Warn on the standard logger.
725func Warningln(args ...interface{}) {
726 getPackageLevelSugaredLogger().Warn(args...)
727}
728
729// Warningf logs a message at level Warn on the standard logger.
730func Warningf(format string, args ...interface{}) {
731 getPackageLevelSugaredLogger().Warnf(format, args...)
732}
733
734// V reports whether verbosity level l is at least the requested verbose level.
735func V(level int) bool {
736 return getPackageLevelLogger().V(level)
khenaidoocfee5f42018-07-19 22:47:38 -0400737}