blob: b47b5621d882db0a737be026678290925905b0f0 [file] [log] [blame]
Girish Kumar46d7c3a2020-05-18 12:06:33 +00001/*
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) {
Girish Kumare798af12020-06-26 18:06:04 +000078 // 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 Kumar46d7c3a2020-05-18 12:06:33 +000082 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
90func getPackageLevelSugaredLogger() *zp.SugaredLogger {
91 pkgName, _, _, _ := getCallerInfo()
92 if _, exist := loggers[pkgName]; exist {
93 return loggers[pkgName].log
94 }
95 return defaultLogger.log
96}
97
98func 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
107func (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.
112func (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.
117func (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.
122func (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.
128func (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.
135func (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.
140func (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.
147func (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.
153func (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.
160func (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.
165func (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.
170func (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.
176func (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.
183func (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.
188func (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.
193func (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.
199func (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.
206func (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.
211func (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.
216func (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.
222func (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.
229func (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.
234func (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.
239func (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.
244func (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
249func (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
254func 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.
259func Debug(args ...interface{}) {
260 getPackageLevelSugaredLogger().Debug(args...)
261}
262
263// Debugln logs a message at level Debug on the standard logger.
264func Debugln(args ...interface{}) {
265 getPackageLevelSugaredLogger().Debug(args...)
266}
267
268// Debugf logs a message at level Debug on the standard logger.
269func 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.
275func 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.
280func Info(args ...interface{}) {
281 getPackageLevelSugaredLogger().Info(args...)
282}
283
284// Infoln logs a message at level Info on the standard logger.
285func Infoln(args ...interface{}) {
286 getPackageLevelSugaredLogger().Info(args...)
287}
288
289// Infof logs a message at level Info on the standard logger.
290func 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.
296func 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.
301func Warn(args ...interface{}) {
302 getPackageLevelSugaredLogger().Warn(args...)
303}
304
305// Warnln logs a message at level Warn on the standard logger.
306func Warnln(args ...interface{}) {
307 getPackageLevelSugaredLogger().Warn(args...)
308}
309
310// Warnf logs a message at level Warn on the standard logger.
311func 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.
317func 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.
322func Error(args ...interface{}) {
323 getPackageLevelSugaredLogger().Error(args...)
324}
325
326// Errorln logs a message at level Error on the standard logger.
327func Errorln(args ...interface{}) {
328 getPackageLevelSugaredLogger().Error(args...)
329}
330
331// Errorf logs a message at level Error on the standard logger.
332func 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.
338func 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.
343func Fatal(args ...interface{}) {
344 getPackageLevelSugaredLogger().Fatal(args...)
345}
346
347// Fatalln logs a message at level Fatal on the standard logger.
348func Fatalln(args ...interface{}) {
349 getPackageLevelSugaredLogger().Fatal(args...)
350}
351
352// Fatalf logs a message at level Fatal on the standard logger.
353func 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.
359func 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.
364func Warning(args ...interface{}) {
365 getPackageLevelSugaredLogger().Warn(args...)
366}
367
368// Warningln logs a message at level Warn on the standard logger.
369func Warningln(args ...interface{}) {
370 getPackageLevelSugaredLogger().Warn(args...)
371}
372
373// Warningf logs a message at level Warn on the standard logger.
374func 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.
379func V(level LogLevel) bool {
380 return getPackageLevelLogger().V(level)
381}
382
383//GetLogLevel returns the log level of the invoking package
384func GetLogLevel() LogLevel {
385 return getPackageLevelLogger().GetLogLevel()
386}