Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | package log |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | |
| 21 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 22 | ) |
| 23 | |
| 24 | const skipLevel = 2 |
| 25 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 26 | // Fields - struct to update log params |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 27 | type Fields log.Fields |
| 28 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 29 | // CLogger - CLogger wrapper |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 30 | type CLogger struct { |
| 31 | clogger log.CLogger |
| 32 | } |
| 33 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 34 | type LevelLog int8 |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 35 | |
| 36 | // constants defining the Log Level |
| 37 | const ( |
| 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 |
| 51 | func 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 |
| 61 | func 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 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 72 | // StringToLogLevel - converts the log level from string to defined uint8 |
| 73 | func StringToLogLevel(l string) (LevelLog, error) { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 74 | ll, err := log.StringToLogLevel(l) |
| 75 | if err != nil { |
| 76 | return 0, err |
| 77 | } |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 78 | return LevelLog(ll), nil |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // With initializes logger with the key-value pairs |
| 82 | func (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 |
| 87 | func 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 |
| 92 | func SetDefaultLogLevel(level int) { |
| 93 | log.SetDefaultLogLevel(log.LogLevel(level)) |
| 94 | } |
| 95 | |
| 96 | // UpdateAllLoggers create new loggers for all registered pacakges with the defaultFields. |
| 97 | func 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) |
| 104 | func 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. |
| 111 | func CleanUp() error { |
| 112 | return log.CleanUp() |
| 113 | } |
| 114 | |
| 115 | // Fatal logs a message at level Fatal on the standard logger. |
| 116 | func (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. |
| 122 | func (cl CLogger) Fatalw(ctx context.Context, msg string, keysAndValues Fields) { |
| 123 | cl.clogger.Fatalw(ctx, msg, log.Fields(keysAndValues)) |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Error logs a message at level Error on the standard logger. |
| 127 | func (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. |
| 133 | func (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. |
| 138 | func (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. |
| 144 | func (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. |
| 149 | func (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. |
| 155 | func (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. |
| 160 | func (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. |
| 166 | func (cl CLogger) Debugw(ctx context.Context, msg string, keysAndValues Fields) { |
| 167 | cl.clogger.Debugw(ctx, msg, log.Fields(keysAndValues)) |
| 168 | } |