github.com/swaros/contxt/module/runner@v0.0.0-20240305083542-3dbd4436ac40/logruslogger.go (about) 1 // MIT License 2 // 3 // Copyright (c) 2020 Thomas Ziegler <thomas.zglr@googlemail.com>. All rights reserved. 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the Software), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 // AINC-NOTE-0815 24 25 package runner 26 27 import ( 28 "fmt" 29 "io" 30 31 "github.com/sirupsen/logrus" 32 "github.com/swaros/contxt/module/mimiclog" 33 ) 34 35 // mapping the logrus levels to mimiclog levels 36 type Fields map[string]interface{} 37 38 func (f Fields) ToLogrusFields() logrus.Fields { 39 return logrus.Fields(f) 40 } 41 42 // the LogrusLogger is a wrapper around the mimiclog.Logger interface 43 // and the logrus.Logger implementation 44 // it is mainly taken from the logrus_test.go file in the mimiclog module 45 46 type logrusLogger struct { 47 *logrus.Logger 48 } 49 50 // then we will create the logrus implementation 51 52 func NewLogrusLogger() *logrusLogger { 53 54 return &logrusLogger{ 55 Logger: logrus.New(), 56 } 57 } 58 59 // we need to implement the mimiclog.Logger interface 60 61 func (l *logrusLogger) makeNice(level string, args ...interface{}) { 62 63 msg := "" 64 firstIsString := false 65 if len(args) == 0 { 66 return 67 } 68 switch val := args[0].(type) { 69 case string: 70 firstIsString = true 71 msg = val 72 case error: 73 msg = val.Error() 74 default: 75 msg = fmt.Sprintf("%v", val) 76 } 77 78 if firstIsString { 79 if len(args) > 1 { 80 l.logWithLevel(level, msg, args[1:]...) 81 } else { 82 l.logWithLevel(level, msg) 83 } 84 } else { 85 l.logWithLevel(level, msg, args...) 86 } 87 } 88 89 func (l *logrusLogger) logWithLevel(level string, msg string, args ...interface{}) { 90 91 entry := logrus.NewEntry(l.Logger) 92 if len(args) > 0 { 93 switch argtype := args[0].(type) { 94 case map[string]interface{}: 95 entry = l.Logger.WithFields(argtype) 96 case Fields: 97 entry = l.Logger.WithFields(argtype.ToLogrusFields()) 98 case mimiclog.Fields: 99 entry = l.Logger.WithFields(logrus.Fields(argtype)) 100 default: 101 entry = l.Logger.WithField("data", args) 102 } 103 } 104 105 switch level { 106 case mimiclog.LevelTrace: 107 entry.Trace(msg) 108 case mimiclog.LevelDebug: 109 entry.Debug(msg) 110 case mimiclog.LevelInfo: 111 entry.Info(msg) 112 case mimiclog.LevelWarn: 113 entry.Warn(msg) 114 case mimiclog.LevelError: 115 entry.Error(msg) 116 case mimiclog.LevelCritical: 117 entry.Fatal(msg) 118 default: 119 entry.Trace(msg) 120 } 121 122 } 123 func (l *logrusLogger) Trace(args ...interface{}) { 124 l.makeNice(mimiclog.LevelTrace, args...) 125 } 126 127 func (l *logrusLogger) Debug(args ...interface{}) { 128 l.makeNice(mimiclog.LevelDebug, args...) 129 } 130 131 func (l *logrusLogger) Error(args ...interface{}) { 132 l.makeNice(mimiclog.LevelError, args...) 133 } 134 135 func (l *logrusLogger) Warn(args ...interface{}) { 136 l.makeNice(mimiclog.LevelWarn, args...) 137 } 138 139 func (l *logrusLogger) Info(args ...interface{}) { 140 l.makeNice(mimiclog.LevelInfo, args...) 141 } 142 143 func (l *logrusLogger) Critical(args ...interface{}) { 144 l.makeNice(mimiclog.LevelCritical, args...) 145 } 146 147 func (l *logrusLogger) IsLevelEnabled(level string) bool { 148 switch level { 149 case mimiclog.LevelTrace: 150 return l.IsTraceEnabled() 151 case mimiclog.LevelDebug: 152 return l.IsDebugEnabled() 153 case mimiclog.LevelInfo: 154 return l.IsInfoEnabled() 155 case mimiclog.LevelWarn: 156 return l.IsWarnEnabled() 157 case mimiclog.LevelError: 158 return l.IsErrorEnabled() 159 case mimiclog.LevelCritical: 160 return l.IsCriticalEnabled() 161 default: 162 return false 163 } 164 } 165 166 func (l *logrusLogger) IsTraceEnabled() bool { 167 return l.Logger.IsLevelEnabled(logrus.TraceLevel) 168 } 169 170 func (l *logrusLogger) IsDebugEnabled() bool { 171 return l.Logger.IsLevelEnabled(logrus.DebugLevel) 172 } 173 174 func (l *logrusLogger) IsInfoEnabled() bool { 175 return l.Logger.IsLevelEnabled(logrus.InfoLevel) 176 } 177 178 func (l *logrusLogger) IsWarnEnabled() bool { 179 return l.Logger.IsLevelEnabled(logrus.WarnLevel) 180 } 181 182 func (l *logrusLogger) IsErrorEnabled() bool { 183 return l.Logger.IsLevelEnabled(logrus.ErrorLevel) 184 } 185 186 func (l *logrusLogger) IsCriticalEnabled() bool { 187 return l.Logger.IsLevelEnabled(logrus.FatalLevel) 188 } 189 190 func (l *logrusLogger) SetLevelByString(level string) { 191 switch level { 192 case mimiclog.LevelTrace: 193 l.Logger.SetLevel(logrus.TraceLevel) 194 case mimiclog.LevelDebug: 195 l.Logger.SetLevel(logrus.DebugLevel) 196 case mimiclog.LevelInfo: 197 l.Logger.SetLevel(logrus.InfoLevel) 198 case mimiclog.LevelWarn: 199 l.Logger.SetLevel(logrus.WarnLevel) 200 case mimiclog.LevelError: 201 l.Logger.SetLevel(logrus.ErrorLevel) 202 case mimiclog.LevelCritical: 203 l.Logger.SetLevel(logrus.FatalLevel) 204 default: 205 l.Logger.SetLevel(logrus.InfoLevel) 206 } 207 } 208 209 func (l *logrusLogger) SetLevel(level interface{}) { 210 switch lvl := level.(type) { 211 case logrus.Level: 212 l.Logger.SetLevel(lvl) 213 case string: 214 l.SetLevelByString(lvl) 215 default: 216 l.Logger.SetLevel(logrus.InfoLevel) 217 } 218 } 219 220 func (l *logrusLogger) GetLevel() string { 221 return l.Logger.GetLevel().String() 222 } 223 func (l *logrusLogger) SetOutput(output io.Writer) { 224 l.Logger.SetOutput(output) 225 } 226 227 func (l *logrusLogger) GetLogger() interface{} { 228 return l.Logger 229 } 230 231 type LogrusMimic struct { 232 } 233 234 func (l *LogrusMimic) Init(args ...interface{}) (mimiclog.Logger, error) { 235 return NewLogrusLogger(), nil 236 } 237 238 func (l *LogrusMimic) Name() string { 239 return "logrus" 240 }