github.com/stripe/stripe-go/v76@v76.25.0/log.go (about) 1 package stripe 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 ) 8 9 // 10 // Public constants 11 // 12 13 const ( 14 // LevelNull sets a logger to show no messages at all. 15 LevelNull Level = 0 16 17 // LevelError sets a logger to show error messages only. 18 LevelError Level = 1 19 20 // LevelWarn sets a logger to show warning messages or anything more 21 // severe. 22 LevelWarn Level = 2 23 24 // LevelInfo sets a logger to show informational messages or anything more 25 // severe. 26 LevelInfo Level = 3 27 28 // LevelDebug sets a logger to show informational messages or anything more 29 // severe. 30 LevelDebug Level = 4 31 ) 32 33 // 34 // Public variables 35 // 36 37 // DefaultLeveledLogger is the default logger that the library will use to log 38 // errors, warnings, and informational messages. 39 // 40 // LeveledLoggerInterface is implemented by LeveledLogger, and one can be 41 // initialized at the desired level of logging. LeveledLoggerInterface also 42 // provides out-of-the-box compatibility with a Logrus Logger, but may require 43 // a thin shim for use with other logging libraries that use less standard 44 // conventions like Zap. 45 // 46 // This Logger will be inherited by any backends created by default, but will 47 // be overridden if a backend is created with GetBackendWithConfig with a 48 // custom LeveledLogger set. 49 var DefaultLeveledLogger LeveledLoggerInterface = &LeveledLogger{ 50 Level: LevelError, 51 } 52 53 // 54 // Public types 55 // 56 57 // Level represents a logging level. 58 type Level uint32 59 60 // LeveledLogger is a leveled logger implementation. 61 // 62 // It prints warnings and errors to `os.Stderr` and other messages to 63 // `os.Stdout`. 64 type LeveledLogger struct { 65 // Level is the minimum logging level that will be emitted by this logger. 66 // 67 // For example, a Level set to LevelWarn will emit warnings and errors, but 68 // not informational or debug messages. 69 // 70 // Always set this with a constant like LevelWarn because the individual 71 // values are not guaranteed to be stable. 72 Level Level 73 74 // Internal testing use only. 75 stderrOverride io.Writer 76 stdoutOverride io.Writer 77 } 78 79 // Debugf logs a debug message using Printf conventions. 80 func (l *LeveledLogger) Debugf(format string, v ...interface{}) { 81 if l.Level >= LevelDebug { 82 fmt.Fprintf(l.stdout(), "[DEBUG] "+format+"\n", v...) 83 } 84 } 85 86 // Errorf logs a warning message using Printf conventions. 87 func (l *LeveledLogger) Errorf(format string, v ...interface{}) { 88 // Infof logs a debug message using Printf conventions. 89 if l.Level >= LevelError { 90 fmt.Fprintf(l.stderr(), "[ERROR] "+format+"\n", v...) 91 } 92 } 93 94 // Infof logs an informational message using Printf conventions. 95 func (l *LeveledLogger) Infof(format string, v ...interface{}) { 96 if l.Level >= LevelInfo { 97 fmt.Fprintf(l.stdout(), "[INFO] "+format+"\n", v...) 98 } 99 } 100 101 // Warnf logs a warning message using Printf conventions. 102 func (l *LeveledLogger) Warnf(format string, v ...interface{}) { 103 if l.Level >= LevelWarn { 104 fmt.Fprintf(l.stderr(), "[WARN] "+format+"\n", v...) 105 } 106 } 107 108 func (l *LeveledLogger) stderr() io.Writer { 109 if l.stderrOverride != nil { 110 return l.stderrOverride 111 } 112 113 return os.Stderr 114 } 115 116 func (l *LeveledLogger) stdout() io.Writer { 117 if l.stdoutOverride != nil { 118 return l.stdoutOverride 119 } 120 121 return os.Stdout 122 } 123 124 // LeveledLoggerInterface provides a basic leveled logging interface for 125 // printing debug, informational, warning, and error messages. 126 // 127 // It's implemented by LeveledLogger and also provides out-of-the-box 128 // compatibility with a Logrus Logger, but may require a thin shim for use with 129 // other logging libraries that you use less standard conventions like Zap. 130 type LeveledLoggerInterface interface { 131 // Debugf logs a debug message using Printf conventions. 132 Debugf(format string, v ...interface{}) 133 134 // Errorf logs a warning message using Printf conventions. 135 Errorf(format string, v ...interface{}) 136 137 // Infof logs an informational message using Printf conventions. 138 Infof(format string, v ...interface{}) 139 140 // Warnf logs a warning message using Printf conventions. 141 Warnf(format string, v ...interface{}) 142 }