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