blob: aab1048698b115f2fcd6882b438c0d7e68763032 [file] [log] [blame]
Girish Gowdra631ef3d2020-06-15 10:45:52 -07001/*
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
22package log
23
24import (
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
30type 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
73type logger struct {
74 cl *clogger
75}
76
77func AddPackage(outputType string, level LogLevel, defaultFields Fields, pkgNames ...string) (Logger, error) {
78 clg, err := RegisterPackage(outputType, level, defaultFields, pkgNames...)
79 if err != nil {
80 return nil, err
81 }
82
83 return logger{cl: clg.(*clogger)}, nil
84}
85
86func getPackageLevelSugaredLogger() *zp.SugaredLogger {
87 pkgName, _, _, _ := getCallerInfo()
88 if _, exist := loggers[pkgName]; exist {
89 return loggers[pkgName].log
90 }
91 return defaultLogger.log
92}
93
94func getPackageLevelLogger() CLogger {
95 pkgName, _, _, _ := getCallerInfo()
96 if _, exist := loggers[pkgName]; exist {
97 return loggers[pkgName]
98 }
99 return defaultLogger
100}
101
102// With returns a logger initialized with the key-value pairs
103func (l logger) With(keysAndValues Fields) Logger {
104 return logger{cl: &clogger{log: l.cl.log.With(serializeMap(keysAndValues)...), parent: l.cl.parent}}
105}
106
107// Debug logs a message at level Debug on the standard logger.
108func (l logger) Debug(args ...interface{}) {
109 l.cl.log.Debug(args...)
110}
111
112// Debugln logs a message at level Debug on the standard logger with a line feed. Default in any case.
113func (l logger) Debugln(args ...interface{}) {
114 l.cl.log.Debug(args...)
115}
116
117// Debugw logs a message at level Debug on the standard logger.
118func (l logger) Debugf(format string, args ...interface{}) {
119 l.cl.log.Debugf(format, args...)
120}
121
122// Debugw logs a message with some additional context. The variadic key-value
123// pairs are treated as they are in With.
124func (l logger) Debugw(msg string, keysAndValues Fields) {
125 if l.V(DebugLevel) {
126 l.cl.log.Debugw(msg, serializeMap(keysAndValues)...)
127 }
128}
129
130// Info logs a message at level Info on the standard logger.
131func (l logger) Info(args ...interface{}) {
132 l.cl.log.Info(args...)
133}
134
135// Infoln logs a message at level Info on the standard logger with a line feed. Default in any case.
136func (l logger) Infoln(args ...interface{}) {
137 l.cl.log.Info(args...)
138 //msg := fmt.Sprintln(args...)
139 //l.sourced().Info(msg[:len(msg)-1])
140}
141
142// Infof logs a message at level Info on the standard logger.
143func (l logger) Infof(format string, args ...interface{}) {
144 l.cl.log.Infof(format, args...)
145}
146
147// Infow logs a message with some additional context. The variadic key-value
148// pairs are treated as they are in With.
149func (l logger) Infow(msg string, keysAndValues Fields) {
150 if l.V(InfoLevel) {
151 l.cl.log.Infow(msg, serializeMap(keysAndValues)...)
152 }
153}
154
155// Warn logs a message at level Warn on the standard logger.
156func (l logger) Warn(args ...interface{}) {
157 l.cl.log.Warn(args...)
158}
159
160// Warnln logs a message at level Warn on the standard logger with a line feed. Default in any case.
161func (l logger) Warnln(args ...interface{}) {
162 l.cl.log.Warn(args...)
163}
164
165// Warnf logs a message at level Warn on the standard logger.
166func (l logger) Warnf(format string, args ...interface{}) {
167 l.cl.log.Warnf(format, args...)
168}
169
170// Warnw logs a message with some additional context. The variadic key-value
171// pairs are treated as they are in With.
172func (l logger) Warnw(msg string, keysAndValues Fields) {
173 if l.V(WarnLevel) {
174 l.cl.log.Warnw(msg, serializeMap(keysAndValues)...)
175 }
176}
177
178// Error logs a message at level Error on the standard logger.
179func (l logger) Error(args ...interface{}) {
180 l.cl.log.Error(args...)
181}
182
183// Errorln logs a message at level Error on the standard logger with a line feed. Default in any case.
184func (l logger) Errorln(args ...interface{}) {
185 l.cl.log.Error(args...)
186}
187
188// Errorf logs a message at level Error on the standard logger.
189func (l logger) Errorf(format string, args ...interface{}) {
190 l.cl.log.Errorf(format, args...)
191}
192
193// Errorw logs a message with some additional context. The variadic key-value
194// pairs are treated as they are in With.
195func (l logger) Errorw(msg string, keysAndValues Fields) {
196 if l.V(ErrorLevel) {
197 l.cl.log.Errorw(msg, serializeMap(keysAndValues)...)
198 }
199}
200
201// Fatal logs a message at level Fatal on the standard logger.
202func (l logger) Fatal(args ...interface{}) {
203 l.cl.log.Fatal(args...)
204}
205
206// Fatalln logs a message at level Fatal on the standard logger with a line feed. Default in any case.
207func (l logger) Fatalln(args ...interface{}) {
208 l.cl.log.Fatal(args...)
209}
210
211// Fatalf logs a message at level Fatal on the standard logger.
212func (l logger) Fatalf(format string, args ...interface{}) {
213 l.cl.log.Fatalf(format, args...)
214}
215
216// Fatalw logs a message with some additional context. The variadic key-value
217// pairs are treated as they are in With.
218func (l logger) Fatalw(msg string, keysAndValues Fields) {
219 if l.V(FatalLevel) {
220 l.cl.log.Fatalw(msg, serializeMap(keysAndValues)...)
221 }
222}
223
224// Warning logs a message at level Warn on the standard logger.
225func (l logger) Warning(args ...interface{}) {
226 l.cl.log.Warn(args...)
227}
228
229// Warningln logs a message at level Warn on the standard logger with a line feed. Default in any case.
230func (l logger) Warningln(args ...interface{}) {
231 l.cl.log.Warn(args...)
232}
233
234// Warningf logs a message at level Warn on the standard logger.
235func (l logger) Warningf(format string, args ...interface{}) {
236 l.cl.log.Warnf(format, args...)
237}
238
239// V reports whether verbosity level l is at least the requested verbose level.
240func (l logger) V(level LogLevel) bool {
241 return l.cl.parent.Core().Enabled(logLevelToLevel(level))
242}
243
244// GetLogLevel returns the current level of the logger
245func (l logger) GetLogLevel() LogLevel {
246 return levelToLogLevel(cfgs[l.cl.packageName].Level.Level())
247}
248
249// With returns a logger initialized with the key-value pairs
250func With(keysAndValues Fields) Logger {
251 return logger{cl: &clogger{log: getPackageLevelSugaredLogger().With(serializeMap(keysAndValues)...), parent: defaultLogger.parent}}
252}
253
254// Debug logs a message at level Debug on the standard logger.
255func Debug(args ...interface{}) {
256 getPackageLevelSugaredLogger().Debug(args...)
257}
258
259// Debugln logs a message at level Debug on the standard logger.
260func Debugln(args ...interface{}) {
261 getPackageLevelSugaredLogger().Debug(args...)
262}
263
264// Debugf logs a message at level Debug on the standard logger.
265func Debugf(format string, args ...interface{}) {
266 getPackageLevelSugaredLogger().Debugf(format, args...)
267}
268
269// Debugw logs a message with some additional context. The variadic key-value
270// pairs are treated as they are in With.
271func Debugw(msg string, keysAndValues Fields) {
272 getPackageLevelSugaredLogger().Debugw(msg, serializeMap(keysAndValues)...)
273}
274
275// Info logs a message at level Info on the standard logger.
276func Info(args ...interface{}) {
277 getPackageLevelSugaredLogger().Info(args...)
278}
279
280// Infoln logs a message at level Info on the standard logger.
281func Infoln(args ...interface{}) {
282 getPackageLevelSugaredLogger().Info(args...)
283}
284
285// Infof logs a message at level Info on the standard logger.
286func Infof(format string, args ...interface{}) {
287 getPackageLevelSugaredLogger().Infof(format, args...)
288}
289
290//Infow logs a message with some additional context. The variadic key-value
291//pairs are treated as they are in With.
292func Infow(msg string, keysAndValues Fields) {
293 getPackageLevelSugaredLogger().Infow(msg, serializeMap(keysAndValues)...)
294}
295
296// Warn logs a message at level Warn on the standard logger.
297func Warn(args ...interface{}) {
298 getPackageLevelSugaredLogger().Warn(args...)
299}
300
301// Warnln logs a message at level Warn on the standard logger.
302func Warnln(args ...interface{}) {
303 getPackageLevelSugaredLogger().Warn(args...)
304}
305
306// Warnf logs a message at level Warn on the standard logger.
307func Warnf(format string, args ...interface{}) {
308 getPackageLevelSugaredLogger().Warnf(format, args...)
309}
310
311// Warnw logs a message with some additional context. The variadic key-value
312// pairs are treated as they are in With.
313func Warnw(msg string, keysAndValues Fields) {
314 getPackageLevelSugaredLogger().Warnw(msg, serializeMap(keysAndValues)...)
315}
316
317// Error logs a message at level Error on the standard logger.
318func Error(args ...interface{}) {
319 getPackageLevelSugaredLogger().Error(args...)
320}
321
322// Errorln logs a message at level Error on the standard logger.
323func Errorln(args ...interface{}) {
324 getPackageLevelSugaredLogger().Error(args...)
325}
326
327// Errorf logs a message at level Error on the standard logger.
328func Errorf(format string, args ...interface{}) {
329 getPackageLevelSugaredLogger().Errorf(format, args...)
330}
331
332// Errorw logs a message with some additional context. The variadic key-value
333// pairs are treated as they are in With.
334func Errorw(msg string, keysAndValues Fields) {
335 getPackageLevelSugaredLogger().Errorw(msg, serializeMap(keysAndValues)...)
336}
337
338// Fatal logs a message at level Fatal on the standard logger.
339func Fatal(args ...interface{}) {
340 getPackageLevelSugaredLogger().Fatal(args...)
341}
342
343// Fatalln logs a message at level Fatal on the standard logger.
344func Fatalln(args ...interface{}) {
345 getPackageLevelSugaredLogger().Fatal(args...)
346}
347
348// Fatalf logs a message at level Fatal on the standard logger.
349func Fatalf(format string, args ...interface{}) {
350 getPackageLevelSugaredLogger().Fatalf(format, args...)
351}
352
353// Fatalw logs a message with some additional context. The variadic key-value
354// pairs are treated as they are in With.
355func Fatalw(msg string, keysAndValues Fields) {
356 getPackageLevelSugaredLogger().Fatalw(msg, serializeMap(keysAndValues)...)
357}
358
359// Warning logs a message at level Warn on the standard logger.
360func Warning(args ...interface{}) {
361 getPackageLevelSugaredLogger().Warn(args...)
362}
363
364// Warningln logs a message at level Warn on the standard logger.
365func Warningln(args ...interface{}) {
366 getPackageLevelSugaredLogger().Warn(args...)
367}
368
369// Warningf logs a message at level Warn on the standard logger.
370func Warningf(format string, args ...interface{}) {
371 getPackageLevelSugaredLogger().Warnf(format, args...)
372}
373
374// V reports whether verbosity level l is at least the requested verbose level.
375func V(level LogLevel) bool {
376 return getPackageLevelLogger().V(level)
377}
378
379//GetLogLevel returns the log level of the invoking package
380func GetLogLevel() LogLevel {
381 return getPackageLevelLogger().GetLogLevel()
382}