github.com/Axway/agent-sdk@v1.1.101/pkg/util/log/logger.go (about) 1 package log 2 3 import ( 4 "fmt" 5 6 "github.com/elastic/beats/v7/libbeat/logp" 7 "github.com/sirupsen/logrus" 8 ) 9 10 // FieldLogger Wraps the StdLogger, and provides logrus methods for logging with fields. 11 // Intended to mimic the logrus.FieldLogger interface, but with our own interface and implementation. 12 type FieldLogger interface { 13 StdLogger 14 WithField(key string, value interface{}) FieldLogger 15 WithFields(fields logrus.Fields) FieldLogger 16 WithError(err error) FieldLogger 17 WithComponent(componentName string) FieldLogger 18 WithPackage(packageName string) FieldLogger 19 } 20 21 // StdLogger interface for logging methods found in the go standard library logger, and logrus methods. 22 type StdLogger interface { 23 Redactor 24 logrus.StdLogger 25 26 Debug(args ...interface{}) 27 Debugf(format string, args ...interface{}) 28 Debugln(args ...interface{}) 29 30 Error(args ...interface{}) 31 Errorf(format string, args ...interface{}) 32 Errorln(args ...interface{}) 33 34 Info(args ...interface{}) 35 Infof(format string, args ...interface{}) 36 Infoln(args ...interface{}) 37 38 Warn(args ...interface{}) 39 Warnf(format string, args ...interface{}) 40 Warnln(args ...interface{}) 41 42 Trace(args ...interface{}) 43 Tracef(format string, args ...interface{}) 44 Traceln(args ...interface{}) 45 } 46 47 // Redactor interface for redacting log messages 48 type Redactor interface { 49 DebugRedacted(fields []string, args ...interface{}) 50 ErrorRedacted(fields []string, args ...interface{}) 51 InfoRedacted(fields []string, args ...interface{}) 52 TraceRedacted(fields []string, args ...interface{}) 53 } 54 55 // NewFieldLogger returns a FieldLogger for standard logging, and logp logging. 56 func NewFieldLogger() FieldLogger { 57 entry := logrus.NewEntry(log) 58 return &logger{ 59 entry: entry, 60 noLogP: false, 61 } 62 } 63 64 // NewFieldLogger returns a FieldLogger for standard logging, and logp logging. 65 func NewMetricFieldLogger() FieldLogger { 66 entry := logrus.NewEntry(metric) 67 return &logger{ 68 entry: entry, 69 noLogP: true, 70 } 71 } 72 73 // NewFieldLogger returns a FieldLogger for standard logging, and logp logging. 74 func NewUsageFieldLogger() FieldLogger { 75 entry := logrus.NewEntry(usage) 76 return &logger{ 77 entry: entry, 78 noLogP: true, 79 } 80 } 81 82 // NewFieldLoggerEntry returns a FieldLogger for standard logging, and logp logging. 83 func NewFieldLoggerEntry(l *logrus.Logger, isLogP bool) FieldLogger { 84 entry := logrus.NewEntry(l) 85 return &logger{ 86 entry: entry, 87 noLogP: isLogP, 88 } 89 } 90 91 type logger struct { 92 entry *logrus.Entry 93 noLogP bool 94 } 95 96 // WithComponent adds a field to the log message 97 func (l *logger) WithComponent(value string) FieldLogger { 98 return &logger{entry: l.entry.WithField("component", value), noLogP: l.noLogP} 99 } 100 101 // WithPackage adds a field to the log message 102 func (l *logger) WithPackage(value string) FieldLogger { 103 return &logger{entry: l.entry.WithField("package", value), noLogP: l.noLogP} 104 } 105 106 // WithField adds a field to the log message 107 func (l *logger) WithField(key string, value interface{}) FieldLogger { 108 return &logger{entry: l.entry.WithField(key, value), noLogP: l.noLogP} 109 } 110 111 // WithFields adds multiple fields to the log message 112 func (l *logger) WithFields(fields logrus.Fields) FieldLogger { 113 return &logger{entry: l.entry.WithFields(fields), noLogP: l.noLogP} 114 } 115 116 // WithError adds an error field to the message 117 func (l *logger) WithError(err error) FieldLogger { 118 return &logger{entry: l.entry.WithError(err), noLogP: l.noLogP} 119 } 120 121 // Debugf prints a formatted debug message 122 func (l *logger) Debugf(format string, args ...interface{}) { 123 if l.isLogP() { 124 lgp := l.logpWithEntries() 125 lgp.Named(debugSelector).Debugf(format, args...) 126 return 127 } 128 l.entry.Debugf(format, args...) 129 } 130 131 // Infof prints a formatted info message 132 func (l *logger) Infof(format string, args ...interface{}) { 133 if l.isLogP() { 134 lgp := l.logpWithEntries() 135 lgp.Infof(format, args...) 136 return 137 } 138 l.entry.Infof(format, args...) 139 } 140 141 // Printf formats a message 142 func (l *logger) Printf(format string, args ...interface{}) { 143 if l.isLogP() { 144 lgp := l.logpWithEntries() 145 lgp.Infof(format, args...) 146 return 147 } 148 l.entry.Printf(format, args...) 149 } 150 151 // Warnf prints a formatted warning message 152 func (l *logger) Warnf(format string, args ...interface{}) { 153 if l.isLogP() { 154 lgp := l.logpWithEntries() 155 lgp.Warnf(format, args...) 156 return 157 } 158 l.entry.Warnf(format, args...) 159 } 160 161 // Tracef prints a formatted trace message 162 func (l *logger) Tracef(format string, args ...interface{}) { 163 if l.isLogP() && GetLevel() == logrus.TraceLevel { 164 lgp := l.logpWithEntries() 165 lgp.Named(traceSelector).Debugf(format, args...) 166 return 167 } 168 l.entry.Tracef(format, args...) 169 } 170 171 // Errorf prints a formatted error message 172 func (l *logger) Errorf(format string, args ...interface{}) { 173 if l.isLogP() { 174 lgp := l.logpWithEntries() 175 lgp.Errorw(format, args...) 176 return 177 } 178 l.entry.Errorf(format, args...) 179 } 180 181 // Fatalf prints a formatted fatal message 182 func (l *logger) Fatalf(format string, args ...interface{}) { 183 if l.isLogP() { 184 lgp := l.logpWithEntries() 185 lgp.Fatalw(format, args...) 186 return 187 } 188 l.entry.Fatalf(format, args...) 189 } 190 191 // Panicf prints a formatted panic message 192 func (l *logger) Panicf(format string, args ...interface{}) { 193 if l.isLogP() { 194 lgp := l.logpWithEntries() 195 lgp.Panicw(format, args...) 196 return 197 } 198 l.entry.Panicf(format, args...) 199 } 200 201 // Debug prints a debug message 202 func (l *logger) Debug(args ...interface{}) { 203 if l.isLogP() { 204 lgp := l.logpWithEntries() 205 lgp.Named(debugSelector).Debug(args...) 206 return 207 } 208 l.entry.Debug(args...) 209 } 210 211 // Info prints an info message 212 func (l *logger) Info(args ...interface{}) { 213 if l.isLogP() { 214 lgp := l.logpWithEntries() 215 lgp.Info(args...) 216 return 217 } 218 l.entry.Info(args...) 219 } 220 221 // Print prints a message 222 func (l *logger) Print(args ...interface{}) { 223 if l.isLogP() { 224 lgp := l.logpWithEntries() 225 lgp.Info(args...) 226 return 227 } 228 l.entry.Print(args...) 229 } 230 231 // Trace prints a trace message 232 func (l *logger) Trace(args ...interface{}) { 233 if l.isLogP() && GetLevel() == logrus.TraceLevel { 234 lgp := l.logpWithEntries() 235 lgp.Named(traceSelector).Debug(args...) 236 return 237 } 238 l.entry.Trace(args...) 239 } 240 241 // Warn prints a warning message 242 func (l *logger) Warn(args ...interface{}) { 243 if l.isLogP() { 244 lgp := l.logpWithEntries() 245 lgp.Warn(args...) 246 return 247 } 248 l.entry.Warn(args...) 249 } 250 251 // Error prints an error message 252 func (l *logger) Error(args ...interface{}) { 253 if l.isLogP() { 254 lgp := l.logpWithEntries() 255 lgp.Error(args...) 256 return 257 } 258 l.entry.Error(args...) 259 } 260 261 // Fatal prints a fatal error message 262 func (l *logger) Fatal(args ...interface{}) { 263 if l.isLogP() { 264 lgp := l.logpWithEntries() 265 lgp.Fatal(args...) 266 return 267 } 268 l.entry.Fatal(args...) 269 } 270 271 // Panic prints a panic message 272 func (l *logger) Panic(args ...interface{}) { 273 if l.isLogP() { 274 lgp := l.logpWithEntries() 275 lgp.Panic(args...) 276 return 277 } 278 l.entry.Panic(args...) 279 } 280 281 // Debugln prints a debug line 282 func (l *logger) Debugln(args ...interface{}) { 283 if l.isLogP() { 284 lgp := l.logpWithEntries() 285 lgp.Named(debugSelector).Debug(args...) 286 return 287 } 288 l.entry.Debugln(args...) 289 } 290 291 // Infoln prints an info line 292 func (l *logger) Infoln(args ...interface{}) { 293 if l.isLogP() { 294 lgp := l.logpWithEntries() 295 lgp.Info(args...) 296 return 297 } 298 l.entry.Infoln(args...) 299 } 300 301 // Println prints a line 302 func (l *logger) Println(args ...interface{}) { 303 if l.isLogP() { 304 lgp := l.logpWithEntries() 305 lgp.Info(args...) 306 return 307 } 308 l.entry.Println(args...) 309 } 310 311 // Traceln prints a trace line 312 func (l *logger) Traceln(args ...interface{}) { 313 if l.isLogP() && GetLevel() == logrus.TraceLevel { 314 lgp := l.logpWithEntries() 315 lgp.Named(traceSelector).Debug(args...) 316 return 317 } 318 l.entry.Traceln(args...) 319 } 320 321 // Warnln prints a warn line 322 func (l *logger) Warnln(args ...interface{}) { 323 if l.isLogP() { 324 lgp := l.logpWithEntries() 325 lgp.Warn(args...) 326 return 327 } 328 l.entry.Warnln(args...) 329 } 330 331 // Errorln prints an error line 332 func (l *logger) Errorln(args ...interface{}) { 333 if l.isLogP() { 334 lgp := l.logpWithEntries() 335 lgp.Error(args...) 336 return 337 } 338 l.entry.Errorln(args...) 339 } 340 341 // Fatalln prints a fatal line 342 func (l *logger) Fatalln(args ...interface{}) { 343 if l.isLogP() { 344 lgp := l.logpWithEntries() 345 lgp.Fatal(args...) 346 return 347 } 348 l.entry.Fatalln(args...) 349 } 350 351 // Panicln prints a panic line 352 func (l *logger) Panicln(args ...interface{}) { 353 if l.isLogP() { 354 lgp := l.logpWithEntries() 355 lgp.Panic(args...) 356 return 357 } 358 l.entry.Panicln(args...) 359 } 360 361 // TraceRedacted redacts a trace message 362 func (l *logger) TraceRedacted(fields []string, args ...interface{}) { 363 l.Trace(ObscureArguments(fields, args...)) 364 } 365 366 // ErrorRedacted redacts an error message 367 func (l *logger) ErrorRedacted(fields []string, args ...interface{}) { 368 l.Error(ObscureArguments(fields, args...)) 369 } 370 371 // InfoRedacted redacts an info message 372 func (l *logger) InfoRedacted(fields []string, args ...interface{}) { 373 l.Info(ObscureArguments(fields, args...)) 374 } 375 376 // DebugRedacted redacts a debug message 377 func (l *logger) DebugRedacted(fields []string, args ...interface{}) { 378 l.Debug(ObscureArguments(fields, args...)) 379 } 380 381 func (l *logger) isLogP() bool { 382 if l.noLogP { 383 return false 384 } 385 return isLogP 386 } 387 388 func (l *logger) logpWithEntries() *logp.Logger { 389 var entries []interface{} 390 for k, val := range l.entry.Data { 391 entries = append(entries, logp.String(k, fmt.Sprintf("%v", val))) 392 } 393 lgp := logp.L() 394 for _, entry := range entries { 395 lgp = lgp.With(entry) 396 } 397 return lgp 398 }