Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // Older Version of Logger interface without support of Context Injection |
| 18 | // This is Depreciated and should not be used anymore. Instead use CLogger |
| 19 | // defined in log.go file. |
| 20 | // This file will be deleted once all code files of voltha compopnents have been |
| 21 | // changed to use new CLogger interface methods supporting context injection |
| 22 | package log |
| 23 | |
| 24 | import ( |
| 25 | zp "go.uber.org/zap" |
| 26 | ) |
| 27 | |
| 28 | // Logger represents an abstract logging interface. Any logging implementation used |
| 29 | // will need to abide by this interface |
| 30 | type Logger interface { |
| 31 | Debug(...interface{}) |
| 32 | Debugln(...interface{}) |
| 33 | Debugf(string, ...interface{}) |
| 34 | Debugw(string, Fields) |
| 35 | |
| 36 | Info(...interface{}) |
| 37 | Infoln(...interface{}) |
| 38 | Infof(string, ...interface{}) |
| 39 | Infow(string, Fields) |
| 40 | |
| 41 | Warn(...interface{}) |
| 42 | Warnln(...interface{}) |
| 43 | Warnf(string, ...interface{}) |
| 44 | Warnw(string, Fields) |
| 45 | |
| 46 | Error(...interface{}) |
| 47 | Errorln(...interface{}) |
| 48 | Errorf(string, ...interface{}) |
| 49 | Errorw(string, Fields) |
| 50 | |
| 51 | Fatal(...interface{}) |
| 52 | Fatalln(...interface{}) |
| 53 | Fatalf(string, ...interface{}) |
| 54 | Fatalw(string, Fields) |
| 55 | |
| 56 | With(Fields) Logger |
| 57 | |
| 58 | // The following are added to be able to use this logger as a gRPC LoggerV2 if needed |
| 59 | // |
| 60 | Warning(...interface{}) |
| 61 | Warningln(...interface{}) |
| 62 | Warningf(string, ...interface{}) |
| 63 | |
| 64 | // V reports whether verbosity level l is at least the requested verbose level. |
| 65 | V(l LogLevel) bool |
| 66 | |
| 67 | //Returns the log level of this specific logger |
| 68 | GetLogLevel() LogLevel |
| 69 | } |
| 70 | |
| 71 | // logger has been refactored to be a thin wrapper on clogger implementation to support |
| 72 | // all existing log statements during transition to new clogger |
| 73 | type logger struct { |
| 74 | cl *clogger |
| 75 | } |
| 76 | |
| 77 | func AddPackage(outputType string, level LogLevel, defaultFields Fields, pkgNames ...string) (Logger, error) { |
Girish Kumar | e798af1 | 2020-06-26 18:06:04 +0000 | [diff] [blame] | 78 | // Get package name of caller method and pass further on; else this method is considered caller |
| 79 | pkgName, _, _, _ := getCallerInfo() |
| 80 | |
| 81 | pkgNames = append(pkgNames, pkgName) |
Girish Kumar | 46d7c3a | 2020-05-18 12:06:33 +0000 | [diff] [blame] | 82 | clg, err := RegisterPackage(outputType, level, defaultFields, pkgNames...) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | |
| 87 | return logger{cl: clg.(*clogger)}, nil |
| 88 | } |
| 89 | |
| 90 | func getPackageLevelSugaredLogger() *zp.SugaredLogger { |
| 91 | pkgName, _, _, _ := getCallerInfo() |
| 92 | if _, exist := loggers[pkgName]; exist { |
| 93 | return loggers[pkgName].log |
| 94 | } |
| 95 | return defaultLogger.log |
| 96 | } |
| 97 | |
| 98 | func getPackageLevelLogger() CLogger { |
| 99 | pkgName, _, _, _ := getCallerInfo() |
| 100 | if _, exist := loggers[pkgName]; exist { |
| 101 | return loggers[pkgName] |
| 102 | } |
| 103 | return defaultLogger |
| 104 | } |
| 105 | |
| 106 | // With returns a logger initialized with the key-value pairs |
| 107 | func (l logger) With(keysAndValues Fields) Logger { |
| 108 | return logger{cl: &clogger{log: l.cl.log.With(serializeMap(keysAndValues)...), parent: l.cl.parent}} |
| 109 | } |
| 110 | |
| 111 | // Debug logs a message at level Debug on the standard logger. |
| 112 | func (l logger) Debug(args ...interface{}) { |
| 113 | l.cl.log.Debug(args...) |
| 114 | } |
| 115 | |
| 116 | // Debugln logs a message at level Debug on the standard logger with a line feed. Default in any case. |
| 117 | func (l logger) Debugln(args ...interface{}) { |
| 118 | l.cl.log.Debug(args...) |
| 119 | } |
| 120 | |
| 121 | // Debugw logs a message at level Debug on the standard logger. |
| 122 | func (l logger) Debugf(format string, args ...interface{}) { |
| 123 | l.cl.log.Debugf(format, args...) |
| 124 | } |
| 125 | |
| 126 | // Debugw logs a message with some additional context. The variadic key-value |
| 127 | // pairs are treated as they are in With. |
| 128 | func (l logger) Debugw(msg string, keysAndValues Fields) { |
| 129 | if l.V(DebugLevel) { |
| 130 | l.cl.log.Debugw(msg, serializeMap(keysAndValues)...) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Info logs a message at level Info on the standard logger. |
| 135 | func (l logger) Info(args ...interface{}) { |
| 136 | l.cl.log.Info(args...) |
| 137 | } |
| 138 | |
| 139 | // Infoln logs a message at level Info on the standard logger with a line feed. Default in any case. |
| 140 | func (l logger) Infoln(args ...interface{}) { |
| 141 | l.cl.log.Info(args...) |
| 142 | //msg := fmt.Sprintln(args...) |
| 143 | //l.sourced().Info(msg[:len(msg)-1]) |
| 144 | } |
| 145 | |
| 146 | // Infof logs a message at level Info on the standard logger. |
| 147 | func (l logger) Infof(format string, args ...interface{}) { |
| 148 | l.cl.log.Infof(format, args...) |
| 149 | } |
| 150 | |
| 151 | // Infow logs a message with some additional context. The variadic key-value |
| 152 | // pairs are treated as they are in With. |
| 153 | func (l logger) Infow(msg string, keysAndValues Fields) { |
| 154 | if l.V(InfoLevel) { |
| 155 | l.cl.log.Infow(msg, serializeMap(keysAndValues)...) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Warn logs a message at level Warn on the standard logger. |
| 160 | func (l logger) Warn(args ...interface{}) { |
| 161 | l.cl.log.Warn(args...) |
| 162 | } |
| 163 | |
| 164 | // Warnln logs a message at level Warn on the standard logger with a line feed. Default in any case. |
| 165 | func (l logger) Warnln(args ...interface{}) { |
| 166 | l.cl.log.Warn(args...) |
| 167 | } |
| 168 | |
| 169 | // Warnf logs a message at level Warn on the standard logger. |
| 170 | func (l logger) Warnf(format string, args ...interface{}) { |
| 171 | l.cl.log.Warnf(format, args...) |
| 172 | } |
| 173 | |
| 174 | // Warnw logs a message with some additional context. The variadic key-value |
| 175 | // pairs are treated as they are in With. |
| 176 | func (l logger) Warnw(msg string, keysAndValues Fields) { |
| 177 | if l.V(WarnLevel) { |
| 178 | l.cl.log.Warnw(msg, serializeMap(keysAndValues)...) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Error logs a message at level Error on the standard logger. |
| 183 | func (l logger) Error(args ...interface{}) { |
| 184 | l.cl.log.Error(args...) |
| 185 | } |
| 186 | |
| 187 | // Errorln logs a message at level Error on the standard logger with a line feed. Default in any case. |
| 188 | func (l logger) Errorln(args ...interface{}) { |
| 189 | l.cl.log.Error(args...) |
| 190 | } |
| 191 | |
| 192 | // Errorf logs a message at level Error on the standard logger. |
| 193 | func (l logger) Errorf(format string, args ...interface{}) { |
| 194 | l.cl.log.Errorf(format, args...) |
| 195 | } |
| 196 | |
| 197 | // Errorw logs a message with some additional context. The variadic key-value |
| 198 | // pairs are treated as they are in With. |
| 199 | func (l logger) Errorw(msg string, keysAndValues Fields) { |
| 200 | if l.V(ErrorLevel) { |
| 201 | l.cl.log.Errorw(msg, serializeMap(keysAndValues)...) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Fatal logs a message at level Fatal on the standard logger. |
| 206 | func (l logger) Fatal(args ...interface{}) { |
| 207 | l.cl.log.Fatal(args...) |
| 208 | } |
| 209 | |
| 210 | // Fatalln logs a message at level Fatal on the standard logger with a line feed. Default in any case. |
| 211 | func (l logger) Fatalln(args ...interface{}) { |
| 212 | l.cl.log.Fatal(args...) |
| 213 | } |
| 214 | |
| 215 | // Fatalf logs a message at level Fatal on the standard logger. |
| 216 | func (l logger) Fatalf(format string, args ...interface{}) { |
| 217 | l.cl.log.Fatalf(format, args...) |
| 218 | } |
| 219 | |
| 220 | // Fatalw logs a message with some additional context. The variadic key-value |
| 221 | // pairs are treated as they are in With. |
| 222 | func (l logger) Fatalw(msg string, keysAndValues Fields) { |
| 223 | if l.V(FatalLevel) { |
| 224 | l.cl.log.Fatalw(msg, serializeMap(keysAndValues)...) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // Warning logs a message at level Warn on the standard logger. |
| 229 | func (l logger) Warning(args ...interface{}) { |
| 230 | l.cl.log.Warn(args...) |
| 231 | } |
| 232 | |
| 233 | // Warningln logs a message at level Warn on the standard logger with a line feed. Default in any case. |
| 234 | func (l logger) Warningln(args ...interface{}) { |
| 235 | l.cl.log.Warn(args...) |
| 236 | } |
| 237 | |
| 238 | // Warningf logs a message at level Warn on the standard logger. |
| 239 | func (l logger) Warningf(format string, args ...interface{}) { |
| 240 | l.cl.log.Warnf(format, args...) |
| 241 | } |
| 242 | |
| 243 | // V reports whether verbosity level l is at least the requested verbose level. |
| 244 | func (l logger) V(level LogLevel) bool { |
| 245 | return l.cl.parent.Core().Enabled(logLevelToLevel(level)) |
| 246 | } |
| 247 | |
| 248 | // GetLogLevel returns the current level of the logger |
| 249 | func (l logger) GetLogLevel() LogLevel { |
| 250 | return levelToLogLevel(cfgs[l.cl.packageName].Level.Level()) |
| 251 | } |
| 252 | |
| 253 | // With returns a logger initialized with the key-value pairs |
| 254 | func With(keysAndValues Fields) Logger { |
| 255 | return logger{cl: &clogger{log: getPackageLevelSugaredLogger().With(serializeMap(keysAndValues)...), parent: defaultLogger.parent}} |
| 256 | } |
| 257 | |
| 258 | // Debug logs a message at level Debug on the standard logger. |
| 259 | func Debug(args ...interface{}) { |
| 260 | getPackageLevelSugaredLogger().Debug(args...) |
| 261 | } |
| 262 | |
| 263 | // Debugln logs a message at level Debug on the standard logger. |
| 264 | func Debugln(args ...interface{}) { |
| 265 | getPackageLevelSugaredLogger().Debug(args...) |
| 266 | } |
| 267 | |
| 268 | // Debugf logs a message at level Debug on the standard logger. |
| 269 | func Debugf(format string, args ...interface{}) { |
| 270 | getPackageLevelSugaredLogger().Debugf(format, args...) |
| 271 | } |
| 272 | |
| 273 | // Debugw logs a message with some additional context. The variadic key-value |
| 274 | // pairs are treated as they are in With. |
| 275 | func Debugw(msg string, keysAndValues Fields) { |
| 276 | getPackageLevelSugaredLogger().Debugw(msg, serializeMap(keysAndValues)...) |
| 277 | } |
| 278 | |
| 279 | // Info logs a message at level Info on the standard logger. |
| 280 | func Info(args ...interface{}) { |
| 281 | getPackageLevelSugaredLogger().Info(args...) |
| 282 | } |
| 283 | |
| 284 | // Infoln logs a message at level Info on the standard logger. |
| 285 | func Infoln(args ...interface{}) { |
| 286 | getPackageLevelSugaredLogger().Info(args...) |
| 287 | } |
| 288 | |
| 289 | // Infof logs a message at level Info on the standard logger. |
| 290 | func Infof(format string, args ...interface{}) { |
| 291 | getPackageLevelSugaredLogger().Infof(format, args...) |
| 292 | } |
| 293 | |
| 294 | //Infow logs a message with some additional context. The variadic key-value |
| 295 | //pairs are treated as they are in With. |
| 296 | func Infow(msg string, keysAndValues Fields) { |
| 297 | getPackageLevelSugaredLogger().Infow(msg, serializeMap(keysAndValues)...) |
| 298 | } |
| 299 | |
| 300 | // Warn logs a message at level Warn on the standard logger. |
| 301 | func Warn(args ...interface{}) { |
| 302 | getPackageLevelSugaredLogger().Warn(args...) |
| 303 | } |
| 304 | |
| 305 | // Warnln logs a message at level Warn on the standard logger. |
| 306 | func Warnln(args ...interface{}) { |
| 307 | getPackageLevelSugaredLogger().Warn(args...) |
| 308 | } |
| 309 | |
| 310 | // Warnf logs a message at level Warn on the standard logger. |
| 311 | func Warnf(format string, args ...interface{}) { |
| 312 | getPackageLevelSugaredLogger().Warnf(format, args...) |
| 313 | } |
| 314 | |
| 315 | // Warnw logs a message with some additional context. The variadic key-value |
| 316 | // pairs are treated as they are in With. |
| 317 | func Warnw(msg string, keysAndValues Fields) { |
| 318 | getPackageLevelSugaredLogger().Warnw(msg, serializeMap(keysAndValues)...) |
| 319 | } |
| 320 | |
| 321 | // Error logs a message at level Error on the standard logger. |
| 322 | func Error(args ...interface{}) { |
| 323 | getPackageLevelSugaredLogger().Error(args...) |
| 324 | } |
| 325 | |
| 326 | // Errorln logs a message at level Error on the standard logger. |
| 327 | func Errorln(args ...interface{}) { |
| 328 | getPackageLevelSugaredLogger().Error(args...) |
| 329 | } |
| 330 | |
| 331 | // Errorf logs a message at level Error on the standard logger. |
| 332 | func Errorf(format string, args ...interface{}) { |
| 333 | getPackageLevelSugaredLogger().Errorf(format, args...) |
| 334 | } |
| 335 | |
| 336 | // Errorw logs a message with some additional context. The variadic key-value |
| 337 | // pairs are treated as they are in With. |
| 338 | func Errorw(msg string, keysAndValues Fields) { |
| 339 | getPackageLevelSugaredLogger().Errorw(msg, serializeMap(keysAndValues)...) |
| 340 | } |
| 341 | |
| 342 | // Fatal logs a message at level Fatal on the standard logger. |
| 343 | func Fatal(args ...interface{}) { |
| 344 | getPackageLevelSugaredLogger().Fatal(args...) |
| 345 | } |
| 346 | |
| 347 | // Fatalln logs a message at level Fatal on the standard logger. |
| 348 | func Fatalln(args ...interface{}) { |
| 349 | getPackageLevelSugaredLogger().Fatal(args...) |
| 350 | } |
| 351 | |
| 352 | // Fatalf logs a message at level Fatal on the standard logger. |
| 353 | func Fatalf(format string, args ...interface{}) { |
| 354 | getPackageLevelSugaredLogger().Fatalf(format, args...) |
| 355 | } |
| 356 | |
| 357 | // Fatalw logs a message with some additional context. The variadic key-value |
| 358 | // pairs are treated as they are in With. |
| 359 | func Fatalw(msg string, keysAndValues Fields) { |
| 360 | getPackageLevelSugaredLogger().Fatalw(msg, serializeMap(keysAndValues)...) |
| 361 | } |
| 362 | |
| 363 | // Warning logs a message at level Warn on the standard logger. |
| 364 | func Warning(args ...interface{}) { |
| 365 | getPackageLevelSugaredLogger().Warn(args...) |
| 366 | } |
| 367 | |
| 368 | // Warningln logs a message at level Warn on the standard logger. |
| 369 | func Warningln(args ...interface{}) { |
| 370 | getPackageLevelSugaredLogger().Warn(args...) |
| 371 | } |
| 372 | |
| 373 | // Warningf logs a message at level Warn on the standard logger. |
| 374 | func Warningf(format string, args ...interface{}) { |
| 375 | getPackageLevelSugaredLogger().Warnf(format, args...) |
| 376 | } |
| 377 | |
| 378 | // V reports whether verbosity level l is at least the requested verbose level. |
| 379 | func V(level LogLevel) bool { |
| 380 | return getPackageLevelLogger().V(level) |
| 381 | } |
| 382 | |
| 383 | //GetLogLevel returns the log level of the invoking package |
| 384 | func GetLogLevel() LogLevel { |
| 385 | return getPackageLevelLogger().GetLogLevel() |
| 386 | } |