blob: 8cb1174af21cf60f87f5931ec403cbb6e3a7b971 [file] [log] [blame]
Tinoj Joseph1d108322022-07-13 10:07:39 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14 */
15
16package log
17
18import (
19 "context"
20
21 "github.com/opencord/voltha-lib-go/v7/pkg/log"
22)
23
24const skipLevel = 2
25
vinokuma926cb3e2023-03-29 11:41:06 +053026// Fields - struct to update log params
Tinoj Joseph1d108322022-07-13 10:07:39 +053027type Fields log.Fields
28
vinokuma926cb3e2023-03-29 11:41:06 +053029// CLogger - CLogger wrapper
Tinoj Joseph1d108322022-07-13 10:07:39 +053030type CLogger struct {
31 clogger log.CLogger
32}
33
vinokuma926cb3e2023-03-29 11:41:06 +053034type LevelLog int8
Tinoj Joseph1d108322022-07-13 10:07:39 +053035
36// constants defining the Log Level
37const (
38 // DebugLevel logs a message at debug level
39 DebugLevel = iota
40 // InfoLevel logs a message at info level
41 InfoLevel
42 // WarnLevel logs a message at warning level
43 WarnLevel
44 // ErrorLevel logs a message at error level
45 ErrorLevel
46 // FatalLevel logs a message, then calls os.Exit(1).
47 FatalLevel
48)
49
50// AddPackageWithDefaultParam registers a package to the log map with default params
51func AddPackageWithDefaultParam() (CLogger, error) {
52 var cLogger CLogger
53 _, err := log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{})
54 if err == nil {
55 cLogger.clogger, _ = log.UpdateCallerSkipLevel(skipLevel)
56 }
57 return cLogger, err
58}
59
60// AddPackage registers a package to the log map
61func AddPackage(level int) (*CLogger, error) {
62 var cLogger *CLogger
63 logger, err := log.RegisterPackage(log.JSON, log.LogLevel(level), log.Fields{})
64 if err == nil {
65 cLogger = &CLogger{
66 clogger: logger,
67 }
68 }
69 return cLogger, err
70}
71
vinokuma926cb3e2023-03-29 11:41:06 +053072// StringToLogLevel - converts the log level from string to defined uint8
73func StringToLogLevel(l string) (LevelLog, error) {
Tinoj Joseph1d108322022-07-13 10:07:39 +053074 ll, err := log.StringToLogLevel(l)
75 if err != nil {
76 return 0, err
77 }
vinokuma926cb3e2023-03-29 11:41:06 +053078 return LevelLog(ll), nil
Tinoj Joseph1d108322022-07-13 10:07:39 +053079}
80
81// With initializes logger with the key-value pairs
82func (cl CLogger) With(ctx context.Context, keysAndValues Fields, msg string) {
83 cl.clogger.With(log.Fields(keysAndValues)).Fatal(ctx, msg)
84}
85
86// SetAllLogLevel sets the log level of all registered packages to level
87func SetAllLogLevel(level int) {
88 log.SetAllLogLevel(log.LogLevel(level))
89}
90
91// SetDefaultLogLevel sets the log level used for packages that don't have specific loggers
92func SetDefaultLogLevel(level int) {
93 log.SetDefaultLogLevel(log.LogLevel(level))
94}
95
96// UpdateAllLoggers create new loggers for all registered pacakges with the defaultFields.
97func UpdateAllLoggers(defaultFields Fields) error {
98 _ = log.UpdateAllLoggers(log.Fields(defaultFields))
99 return log.UpdateAllCallerSkipLevel(skipLevel)
100}
101
102// SetDefaultLogger needs to be invoked before the logger API can be invoked. This function
103// initialize the default logger (zap's sugaredlogger)
104func SetDefaultLogger(ctx context.Context, level int, defaultFields Fields) error {
105 _, err := log.SetDefaultLogger(log.JSON, log.LogLevel(level), log.Fields(defaultFields))
106 return err
107}
108
109// CleanUp flushed any buffered log entries. Applications should take care to call
110// CleanUp before exiting.
111func CleanUp() error {
112 return log.CleanUp()
113}
114
115// Fatal logs a message at level Fatal on the standard logger.
116func (cl CLogger) Fatal(ctx context.Context, args string) {
117 cl.clogger.Fatal(ctx, args)
118}
119
120// Fatalw logs a message with some additional context. The variadic key-value
121// pairs are treated as they are in With.
122func (cl CLogger) Fatalw(ctx context.Context, msg string, keysAndValues Fields) {
123 cl.clogger.Fatalw(ctx, msg, log.Fields(keysAndValues))
Tinoj Joseph1d108322022-07-13 10:07:39 +0530124}
125
126// Error logs a message at level Error on the standard logger.
127func (cl CLogger) Error(ctx context.Context, args string) {
128 cl.clogger.Error(ctx, args)
129}
130
131// Errorw logs a message with some additional context. The variadic key-value
132// pairs are treated as they are in With.
133func (cl CLogger) Errorw(ctx context.Context, msg string, keysAndValues Fields) {
134 cl.clogger.Errorw(ctx, msg, log.Fields(keysAndValues))
135}
136
137// Warn logs a message at level Warn on the standard logger.
138func (cl CLogger) Warn(ctx context.Context, args string) {
139 cl.clogger.Warn(ctx, args)
140}
141
142// Warnw logs a message with some additional context. The variadic key-value
143// pairs are treated as they are in With.
144func (cl CLogger) Warnw(ctx context.Context, msg string, keysAndValues Fields) {
145 cl.clogger.Warnw(ctx, msg, log.Fields(keysAndValues))
146}
147
148// Info logs a message at level Info on the standard logger.
149func (cl CLogger) Info(ctx context.Context, args string) {
150 cl.clogger.Info(ctx, args)
151}
152
153// Infow logs a message with some additional context. The variadic key-value
154// pairs are treated as they are in With.
155func (cl CLogger) Infow(ctx context.Context, msg string, keysAndValues Fields) {
156 cl.clogger.Infow(ctx, msg, log.Fields(keysAndValues))
157}
158
159// Debug logs a message at level Debug on the standard logger.
160func (cl CLogger) Debug(ctx context.Context, args string) {
161 cl.clogger.Debug(ctx, args)
162}
163
164// Debugw logs a message with some additional context. The variadic key-value
165// pairs are treated as they are in With.
166func (cl CLogger) Debugw(ctx context.Context, msg string, keysAndValues Fields) {
167 cl.clogger.Debugw(ctx, msg, log.Fields(keysAndValues))
168}