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 | |
| 26 | //Fields - struct to update log params |
| 27 | type Fields log.Fields |
| 28 | |
| 29 | //CLogger - CLogger wrapper |
| 30 | type CLogger struct { |
| 31 | clogger log.CLogger |
| 32 | } |
| 33 | |
| 34 | type LogLevel int8 |
| 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 | |
| 72 | //StringToLogLevel - converts the log level from string to defined uint8 |
| 73 | func StringToLogLevel(l string) (LogLevel, error) { |
| 74 | ll, err := log.StringToLogLevel(l) |
| 75 | if err != nil { |
| 76 | return 0, err |
| 77 | } |
| 78 | return LogLevel(ll), nil |
| 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)) |
| 124 | |
| 125 | } |
| 126 | |
| 127 | // Error logs a message at level Error on the standard logger. |
| 128 | func (cl CLogger) Error(ctx context.Context, args string) { |
| 129 | cl.clogger.Error(ctx, args) |
| 130 | } |
| 131 | |
| 132 | // Errorw logs a message with some additional context. The variadic key-value |
| 133 | // pairs are treated as they are in With. |
| 134 | func (cl CLogger) Errorw(ctx context.Context, msg string, keysAndValues Fields) { |
| 135 | cl.clogger.Errorw(ctx, msg, log.Fields(keysAndValues)) |
| 136 | } |
| 137 | |
| 138 | // Warn logs a message at level Warn on the standard logger. |
| 139 | func (cl CLogger) Warn(ctx context.Context, args string) { |
| 140 | cl.clogger.Warn(ctx, args) |
| 141 | } |
| 142 | |
| 143 | // Warnw logs a message with some additional context. The variadic key-value |
| 144 | // pairs are treated as they are in With. |
| 145 | func (cl CLogger) Warnw(ctx context.Context, msg string, keysAndValues Fields) { |
| 146 | cl.clogger.Warnw(ctx, msg, log.Fields(keysAndValues)) |
| 147 | } |
| 148 | |
| 149 | // Info logs a message at level Info on the standard logger. |
| 150 | func (cl CLogger) Info(ctx context.Context, args string) { |
| 151 | cl.clogger.Info(ctx, args) |
| 152 | } |
| 153 | |
| 154 | // Infow logs a message with some additional context. The variadic key-value |
| 155 | // pairs are treated as they are in With. |
| 156 | func (cl CLogger) Infow(ctx context.Context, msg string, keysAndValues Fields) { |
| 157 | cl.clogger.Infow(ctx, msg, log.Fields(keysAndValues)) |
| 158 | } |
| 159 | |
| 160 | // Debug logs a message at level Debug on the standard logger. |
| 161 | func (cl CLogger) Debug(ctx context.Context, args string) { |
| 162 | cl.clogger.Debug(ctx, args) |
| 163 | } |
| 164 | |
| 165 | // Debugw logs a message with some additional context. The variadic key-value |
| 166 | // pairs are treated as they are in With. |
| 167 | func (cl CLogger) Debugw(ctx context.Context, msg string, keysAndValues Fields) { |
| 168 | cl.clogger.Debugw(ctx, msg, log.Fields(keysAndValues)) |
| 169 | } |