dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/clients/logger/logger.go (about) 1 /******************************************************************************* 2 * Copyright 2019 Dell Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. 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 distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 *******************************************************************************/ 14 15 /* 16 Package logger provides a client for integration with the support-logging service. The client can also be configured 17 to write logs to a local file rather than sending them to a service. 18 */ 19 package logger 20 21 // Logging client for the Go implementation of edgexfoundry 22 23 import ( 24 "fmt" 25 stdLog "log" 26 "os" 27 28 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors" 29 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models" 30 31 "github.com/go-kit/log" 32 ) 33 34 // LoggingClient defines the interface for logging operations. 35 type LoggingClient interface { 36 // SetLogLevel sets minimum severity log level. If a logging method is called with a lower level of severity than 37 // what is set, it will result in no output. 38 SetLogLevel(logLevel string) errors.EdgeX 39 // LogLevel returns the current log level setting 40 LogLevel() string 41 // Debug logs a message at the DEBUG severity level 42 Debug(msg string, args ...interface{}) 43 // Error logs a message at the ERROR severity level 44 Error(msg string, args ...interface{}) 45 // Info logs a message at the INFO severity level 46 Info(msg string, args ...interface{}) 47 // Trace logs a message at the TRACE severity level 48 Trace(msg string, args ...interface{}) 49 // Warn logs a message at the WARN severity level 50 Warn(msg string, args ...interface{}) 51 // Debugf logs a formatted message at the DEBUG severity level 52 Debugf(msg string, args ...interface{}) 53 // Errorf logs a formatted message at the ERROR severity level 54 Errorf(msg string, args ...interface{}) 55 // Infof logs a formatted message at the INFO severity level 56 Infof(msg string, args ...interface{}) 57 // Tracef logs a formatted message at the TRACE severity level 58 Tracef(msg string, args ...interface{}) 59 // Warnf logs a formatted message at the WARN severity level 60 Warnf(msg string, args ...interface{}) 61 } 62 63 type edgeXLogger struct { 64 owningServiceName string 65 logLevel *string 66 rootLogger log.Logger 67 levelLoggers map[string]log.Logger 68 } 69 70 // NewClient creates an instance of LoggingClient 71 func NewClient(owningServiceName string, logLevel string) LoggingClient { 72 if !isValidLogLevel(logLevel) { 73 logLevel = models.InfoLog 74 } 75 76 // Set up logging client 77 lc := edgeXLogger{ 78 owningServiceName: owningServiceName, 79 logLevel: &logLevel, 80 } 81 82 lc.rootLogger = log.NewLogfmtLogger(os.Stdout) 83 lc.rootLogger = log.WithPrefix( 84 lc.rootLogger, 85 "ts", 86 log.DefaultTimestampUTC, 87 "app", 88 owningServiceName, 89 "source", 90 log.Caller(5)) 91 92 // Set up the loggers 93 lc.levelLoggers = map[string]log.Logger{} 94 95 for _, logLevel := range logLevels() { 96 lc.levelLoggers[logLevel] = log.WithPrefix(lc.rootLogger, "level", logLevel) 97 } 98 99 return lc 100 } 101 102 // LogLevels returns an array of the possible log levels in order from most to least verbose. 103 func logLevels() []string { 104 return []string{ 105 models.TraceLog, 106 models.DebugLog, 107 models.InfoLog, 108 models.WarnLog, 109 models.ErrorLog} 110 } 111 112 func isValidLogLevel(l string) bool { 113 for _, name := range logLevels() { 114 if name == l { 115 return true 116 } 117 } 118 return false 119 } 120 121 func (lc edgeXLogger) log(logLevel string, formatted bool, msg string, args ...interface{}) { 122 // Check minimum log level 123 for _, name := range logLevels() { 124 if name == *lc.logLevel { 125 break 126 } 127 if name == logLevel { 128 return 129 } 130 } 131 132 if args == nil { 133 args = []interface{}{"msg", msg} 134 } else if formatted { 135 args = []interface{}{"msg", fmt.Sprintf(msg, args...)} 136 } else { 137 if len(args)%2 == 1 { 138 // add an empty string to keep k/v pairs correct 139 args = append(args, "") 140 } 141 if len(msg) > 0 { 142 args = append(args, "msg", msg) 143 } 144 } 145 146 err := lc.levelLoggers[logLevel].Log(args...) 147 if err != nil { 148 stdLog.Fatal(err.Error()) 149 return 150 } 151 152 } 153 154 func (lc edgeXLogger) SetLogLevel(logLevel string) errors.EdgeX { 155 if isValidLogLevel(logLevel) { 156 *lc.logLevel = logLevel 157 158 return nil 159 } 160 161 return errors.NewCommonEdgeX(errors.KindContractInvalid, fmt.Sprintf("invalid log level `%s`", logLevel), nil) 162 } 163 164 func (lc edgeXLogger) LogLevel() string { 165 if lc.logLevel == nil { 166 return "" 167 } 168 return *lc.logLevel 169 } 170 171 func (lc edgeXLogger) Info(msg string, args ...interface{}) { 172 lc.log(models.InfoLog, false, msg, args...) 173 } 174 175 func (lc edgeXLogger) Trace(msg string, args ...interface{}) { 176 lc.log(models.TraceLog, false, msg, args...) 177 } 178 179 func (lc edgeXLogger) Debug(msg string, args ...interface{}) { 180 lc.log(models.DebugLog, false, msg, args...) 181 } 182 183 func (lc edgeXLogger) Warn(msg string, args ...interface{}) { 184 lc.log(models.WarnLog, false, msg, args...) 185 } 186 187 func (lc edgeXLogger) Error(msg string, args ...interface{}) { 188 lc.log(models.ErrorLog, false, msg, args...) 189 } 190 191 func (lc edgeXLogger) Infof(msg string, args ...interface{}) { 192 lc.log(models.InfoLog, true, msg, args...) 193 } 194 195 func (lc edgeXLogger) Tracef(msg string, args ...interface{}) { 196 lc.log(models.TraceLog, true, msg, args...) 197 } 198 199 func (lc edgeXLogger) Debugf(msg string, args ...interface{}) { 200 lc.log(models.DebugLog, true, msg, args...) 201 } 202 203 func (lc edgeXLogger) Warnf(msg string, args ...interface{}) { 204 lc.log(models.WarnLog, true, msg, args...) 205 } 206 207 func (lc edgeXLogger) Errorf(msg string, args ...interface{}) { 208 lc.log(models.ErrorLog, true, msg, args...) 209 }