|
| 1 | +// Copyright 2022, OpenSergo Authors |
| 2 | +// |
| 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 | +package logging |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/pkg/errors" |
| 19 | + "reflect" |
| 20 | +) |
| 21 | + |
| 22 | +// Logger the common interface for logging. |
| 23 | +type Logger interface { |
| 24 | + |
| 25 | + // Print logs message no format as what the msg presents. |
| 26 | + Print(msg string) |
| 27 | + |
| 28 | + // DebugEnabled judge is the DebugLevel enabled |
| 29 | + DebugEnabled() bool |
| 30 | + // Debug logs a non-error message with the given key/value pairs as context. |
| 31 | + // |
| 32 | + // The msg argument should be used to add some constant description to |
| 33 | + // the log line. The key/value pairs can then be used to add additional |
| 34 | + // variable information. The key/value pairs should alternate string |
| 35 | + // keys and arbitrary values. |
| 36 | + Debug(msg string, keysAndValues ...interface{}) |
| 37 | + |
| 38 | + // InfoEnabled judge is the InfoLevel enabled |
| 39 | + InfoEnabled() bool |
| 40 | + // Info logs a non-error message with the given key/value pairs as context. |
| 41 | + // |
| 42 | + // The msg argument should be used to add some constant description to |
| 43 | + // the log line. The key/value pairs can then be used to add additional |
| 44 | + // variable information. The key/value pairs should alternate string |
| 45 | + // keys and arbitrary values. |
| 46 | + Info(msg string, keysAndValues ...interface{}) |
| 47 | + |
| 48 | + // WarnEnabled judge is the WarnLevel enabled |
| 49 | + WarnEnabled() bool |
| 50 | + // Warn logs a non-error message with the given key/value pairs as context. |
| 51 | + // |
| 52 | + // The msg argument should be used to add some constant description to |
| 53 | + // the log line. The key/value pairs can then be used to add additional |
| 54 | + // variable information. The key/value pairs should alternate string |
| 55 | + // keys and arbitrary values. |
| 56 | + Warn(msg string, keysAndValues ...interface{}) |
| 57 | + |
| 58 | + // ErrorEnabled judge is the ErrorLevel enabled |
| 59 | + ErrorEnabled() bool |
| 60 | + // Error logs an error message with error and the given key/value pairs as context. |
| 61 | + // |
| 62 | + // The msg argument should be used to add some constant description to |
| 63 | + // the log line. The key/value pairs can then be used to add additional |
| 64 | + // variable information. The key/value pairs should alternate string |
| 65 | + // keys and arbitrary values. |
| 66 | + Error(err error, msg string, keysAndValues ...interface{}) |
| 67 | +} |
| 68 | + |
| 69 | +var ( |
| 70 | + loggerSlice = make([]Logger, 0) |
| 71 | + |
| 72 | + consoleLogger Logger |
| 73 | +) |
| 74 | + |
| 75 | +// Print logs message no format as what the msg presents. |
| 76 | +func Print(msg string) { |
| 77 | + doLog("Print", nil, msg) |
| 78 | +} |
| 79 | + |
| 80 | +// Debug logs a non-error message with the given key/value pairs as context. |
| 81 | +// |
| 82 | +// The msg argument should be used to add some constant description to |
| 83 | +// the log line. The key/value pairs can then be used to add additional |
| 84 | +// variable information. The key/value pairs should alternate string |
| 85 | +// keys and arbitrary values. |
| 86 | +func Debug(msg string, keysAndValues ...interface{}) { |
| 87 | + doLog("Debug", nil, msg, keysAndValues...) |
| 88 | +} |
| 89 | + |
| 90 | +// DebugWithCallerDepth logs a non-error message with the given key/value pairs as context. |
| 91 | +// |
| 92 | +// logCallerDepth: to calculate the caller:line |
| 93 | +// |
| 94 | +// The msg argument should be used to add some constant description to |
| 95 | +// the log line. The key/value pairs can then be used to add additional |
| 96 | +// variable information. The key/value pairs should alternate string |
| 97 | +// keys and arbitrary values. |
| 98 | +func DebugWithCallerDepth(logger Logger, logFormat LogFormat, logCallerDepth int, msg string, keysAndValues ...interface{}) { |
| 99 | + if !logger.DebugEnabled() { |
| 100 | + return |
| 101 | + } |
| 102 | + logger.Print(AssembleMsg(logFormat, logCallerDepth, "DEBUG", msg, nil, false, keysAndValues...)) |
| 103 | +} |
| 104 | + |
| 105 | +// Info logs a non-error message with the given key/value pairs as context. |
| 106 | +// |
| 107 | +// The msg argument should be used to add some constant description to |
| 108 | +// the log line. The key/value pairs can then be used to add additional |
| 109 | +// variable information. The key/value pairs should alternate string |
| 110 | +// keys and arbitrary values. |
| 111 | +func Info(msg string, keysAndValues ...interface{}) { |
| 112 | + doLog("Info", nil, msg, keysAndValues...) |
| 113 | +} |
| 114 | + |
| 115 | +// InfoWithCallerDepth logs a non-error message with the given key/value pairs as context. |
| 116 | +// |
| 117 | +// logCallerDepth: to calculate the caller:line |
| 118 | +// |
| 119 | +// The msg argument should be used to add some constant description to |
| 120 | +// the log line. The key/value pairs can then be used to add additional |
| 121 | +// variable information. The key/value pairs should alternate string |
| 122 | +// keys and arbitrary values. |
| 123 | +func InfoWithCallerDepth(logger Logger, logFormat LogFormat, logCallerDepth int, msg string, keysAndValues ...interface{}) { |
| 124 | + if !logger.InfoEnabled() { |
| 125 | + return |
| 126 | + } |
| 127 | + logger.Print(AssembleMsg(logFormat, logCallerDepth, "INFO", msg, nil, false, keysAndValues...)) |
| 128 | +} |
| 129 | + |
| 130 | +// Warn logs a non-error message with the given key/value pairs as context. |
| 131 | +// |
| 132 | +// The msg argument should be used to add some constant description to |
| 133 | +// the log line. The key/value pairs can then be used to add additional |
| 134 | +// variable information. The key/value pairs should alternate string |
| 135 | +// keys and arbitrary values. |
| 136 | +func Warn(msg string, keysAndValues ...interface{}) { |
| 137 | + doLog("Warn", nil, msg, keysAndValues...) |
| 138 | +} |
| 139 | + |
| 140 | +// WarnWithCallerDepth logs a non-error message with the given key/value pairs as context. |
| 141 | +// |
| 142 | +// logCallerDepth: to calculate the caller:line |
| 143 | +// |
| 144 | +// The msg argument should be used to add some constant description to |
| 145 | +// the log line. The key/value pairs can then be used to add additional |
| 146 | +// variable information. The key/value pairs should alternate string |
| 147 | +// keys and arbitrary values. |
| 148 | +func WarnWithCallerDepth(logger Logger, logFormat LogFormat, logCallerDepth int, msg string, keysAndValues ...interface{}) { |
| 149 | + if !logger.WarnEnabled() { |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + logger.Print(AssembleMsg(logFormat, logCallerDepth, "WARN", msg, nil, false, keysAndValues...)) |
| 154 | +} |
| 155 | + |
| 156 | +// Error logs an error message with error and the given key/value pairs as context. |
| 157 | +// |
| 158 | +// The msg argument should be used to add some constant description to |
| 159 | +// the log line. The key/value pairs can then be used to add additional |
| 160 | +// variable information. The key/value pairs should alternate string |
| 161 | +// keys and arbitrary values. |
| 162 | +func Error(err error, msg string, keysAndValues ...interface{}) { |
| 163 | + doLog("Error", err, msg, keysAndValues...) |
| 164 | +} |
| 165 | + |
| 166 | +// ErrorWithCallerDepth logs an error message with error and the given key/value pairs as context. |
| 167 | +// |
| 168 | +// logCallerDepth: to calculate the caller:line |
| 169 | +// |
| 170 | +// The msg argument should be used to add some constant description to |
| 171 | +// the log line. The key/value pairs can then be used to add additional |
| 172 | +// variable information. The key/value pairs should alternate string |
| 173 | +// keys and arbitrary values. |
| 174 | +func ErrorWithCallerDepth(logger Logger, logFormat LogFormat, logCallerDepth int, err error, errorWithStack bool, msg string, keysAndValues ...interface{}) { |
| 175 | + if !logger.ErrorEnabled() { |
| 176 | + return |
| 177 | + } |
| 178 | + logger.Print(AssembleMsg(logFormat, logCallerDepth, "ERROR", msg, err, errorWithStack, keysAndValues...)) |
| 179 | +} |
| 180 | + |
| 181 | +// AppendLoggerSlice add the Logger into loggerSlice |
| 182 | +func AppendLoggerSlice(loggerAppend Logger) { |
| 183 | + loggerSlice = append(loggerSlice, loggerAppend) |
| 184 | +} |
| 185 | + |
| 186 | +// ClearLoggerSlice clear the Logger into loggerSlice |
| 187 | +func ClearLoggerSlice() { |
| 188 | + loggerSlice = make([]Logger, 0) |
| 189 | +} |
| 190 | + |
| 191 | +// SetConsoleLogger set the consoleLogger to print int stdout |
| 192 | +func SetConsoleLogger(logger Logger) { |
| 193 | + consoleLogger = logger |
| 194 | +} |
| 195 | + |
| 196 | +// doLog do log |
| 197 | +// funcNameFromInterface funcName in Logger |
| 198 | +// err |
| 199 | +// msg |
| 200 | +// keysAndValues |
| 201 | +func doLog(funcNameFromInterface string, err error, msg string, keysAndValues ...interface{}) { |
| 202 | + if consoleLogger == nil && len(loggerSlice) == 0 { |
| 203 | + NewDefaultConsoleLogger(InfoLevel) |
| 204 | + } |
| 205 | + |
| 206 | + if consoleLogger != nil { |
| 207 | + invokeLogger(consoleLogger, funcNameFromInterface, err, msg, keysAndValues...) |
| 208 | + } |
| 209 | + |
| 210 | + if len(loggerSlice) > 0 { |
| 211 | + for _, logger := range loggerSlice { |
| 212 | + invokeLogger(logger, funcNameFromInterface, err, msg, keysAndValues...) |
| 213 | + } |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +// invokeLogger do log actually by invoke function of Logger |
| 218 | +// logger Logger to print |
| 219 | +// funcNameFromInterface funcName in Logger |
| 220 | +// err |
| 221 | +// msg |
| 222 | +// keysAndValues |
| 223 | +func invokeLogger(logger Logger, funcNameFromInterface string, err error, msg string, keysAndValues ...interface{}) { |
| 224 | + method, ok := reflect.TypeOf(logger).MethodByName(funcNameFromInterface) |
| 225 | + if !ok { |
| 226 | + assembleMsg := AssembleMsg(SeparateFormat, 4, "WARN", "no function named '"+funcNameFromInterface+"' was found in interface 'opensergo-go/pkg/logging/Logger'", nil, false) |
| 227 | + logger.Print(assembleMsg) |
| 228 | + return |
| 229 | + } |
| 230 | + |
| 231 | + keysAndValuesLen := len(keysAndValues) |
| 232 | + params := make([]reflect.Value, 0) |
| 233 | + params = append(params, reflect.ValueOf(logger)) |
| 234 | + if "Error" == funcNameFromInterface { |
| 235 | + if err == nil { |
| 236 | + err = errors.New("") |
| 237 | + } |
| 238 | + params = append(params, reflect.ValueOf(err)) |
| 239 | + } |
| 240 | + params = append(params, reflect.ValueOf(msg)) |
| 241 | + |
| 242 | + if keysAndValuesLen != 0 { |
| 243 | + if keysAndValuesLen == 1 && keysAndValues[0] == nil { |
| 244 | + |
| 245 | + } else { |
| 246 | + for _, keyOrValue := range keysAndValues { |
| 247 | + params = append(params, reflect.ValueOf(keyOrValue)) |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + method.Func.Call(params) |
| 252 | +} |
0 commit comments