github.com/brandur/modulir@v0.0.0-20240305213423-94ee82929cbd/log.go (about) 1 package modulir 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 8 "github.com/logrusorgru/aurora" 9 ) 10 11 ////////////////////////////////////////////////////////////////////////////// 12 // 13 // 14 // 15 // Public 16 // 17 // 18 // 19 ////////////////////////////////////////////////////////////////////////////// 20 21 const ( 22 // LevelError sets a logger to show error messages only. 23 LevelError Level = 1 24 25 // LevelWarn sets a logger to show warning messages or anything more 26 // severe. 27 LevelWarn Level = 2 28 29 // LevelInfo sets a logger to show informational messages or anything more 30 // severe. 31 LevelInfo Level = 3 32 33 // LevelDebug sets a logger to show informational messages or anything more 34 // severe. 35 LevelDebug Level = 4 36 ) 37 38 // Level represents a logging level. 39 type Level uint32 40 41 // Logger is a basic implementation of LoggerInterface. 42 type Logger struct { 43 // Level is the minimum logging level that will be emitted by this logger. 44 // 45 // For example, a Level set to LevelWarn will emit warnings and errors, but 46 // not informational or debug messages. 47 // 48 // Always set this with a constant like LevelWarn because the individual 49 // values are not guaranteed to be stable. 50 Level Level 51 52 // Internal testing use only. 53 stderrOverride io.Writer 54 stdoutOverride io.Writer 55 } 56 57 // Debugf logs a debug message using Printf conventions. 58 func (l *Logger) Debugf(format string, v ...interface{}) { 59 if l.Level >= LevelDebug { 60 fmt.Fprintf(l.stdout(), "[DEBUG] "+format+"\n", v...) 61 } 62 } 63 64 // Errorf logs a warning message using Printf conventions. 65 func (l *Logger) Errorf(format string, v ...interface{}) { 66 // Infof logs a debug message using Printf conventions. 67 if l.Level >= LevelError { 68 fmt.Fprintf(l.stderr(), "[ERROR] "+format+"\n", v...) 69 } 70 } 71 72 // Infof logs an informational message using Printf conventions. 73 func (l *Logger) Infof(format string, v ...interface{}) { 74 if l.Level >= LevelInfo { 75 fmt.Fprintf(l.stdout(), "[INFO] "+format+"\n", v...) 76 } 77 } 78 79 // Warnf logs a warning message using Printf conventions. 80 func (l *Logger) Warnf(format string, v ...interface{}) { 81 if l.Level >= LevelWarn { 82 fmt.Fprintf(l.stderr(), "[WARN] "+format+"\n", v...) 83 } 84 } 85 86 func (l *Logger) stderr() io.Writer { 87 if l.stderrOverride != nil { 88 return l.stderrOverride 89 } 90 91 return os.Stderr 92 } 93 94 func (l *Logger) stdout() io.Writer { 95 if l.stdoutOverride != nil { 96 return l.stdoutOverride 97 } 98 99 return os.Stdout 100 } 101 102 // LoggerInterface is an interface that should be implemented by loggers used 103 // with the library. Logger provides a basic implementation, but it's also 104 // compatible with libraries such as Logrus. 105 type LoggerInterface interface { 106 // Debugf logs a debug message using Printf conventions. 107 Debugf(format string, v ...interface{}) 108 109 // Errorf logs a warning message using Printf conventions. 110 Errorf(format string, v ...interface{}) 111 112 // Infof logs an informational message using Printf conventions. 113 Infof(format string, v ...interface{}) 114 115 // Warnf logs a warning message using Printf conventions. 116 Warnf(format string, v ...interface{}) 117 } 118 119 ////////////////////////////////////////////////////////////////////////////// 120 // 121 // 122 // 123 // Package private 124 // 125 // 126 // 127 ////////////////////////////////////////////////////////////////////////////// 128 129 type colorizer struct { 130 LogColor bool 131 } 132 133 func (c *colorizer) Blue(arg interface{}) aurora.Value { //nolint:ireturn 134 if !c.LogColor { 135 return aurora.Reset(arg) 136 } 137 return aurora.Blue(arg) 138 } 139 140 func (c *colorizer) Bold(arg interface{}) aurora.Value { //nolint:ireturn 141 if !c.LogColor { 142 return aurora.Reset(arg) 143 } 144 return aurora.Bold(arg) 145 } 146 147 func (c *colorizer) BrightBlue(arg interface{}) aurora.Value { //nolint:ireturn 148 if !c.LogColor { 149 return aurora.Reset(arg) 150 } 151 return aurora.BrightBlue(arg) 152 } 153 154 func (c *colorizer) Cyan(arg interface{}) aurora.Value { //nolint:ireturn 155 if !c.LogColor { 156 return aurora.Reset(arg) 157 } 158 return aurora.Cyan(arg) 159 } 160 161 func (c *colorizer) Green(arg interface{}) aurora.Value { //nolint:ireturn 162 if !c.LogColor { 163 return aurora.Reset(arg) 164 } 165 return aurora.Green(arg) 166 } 167 168 func (c *colorizer) Red(arg interface{}) aurora.Value { //nolint:ireturn 169 if !c.LogColor { 170 return aurora.Reset(arg) 171 } 172 return aurora.Red(arg) 173 } 174 175 func (c *colorizer) Yellow(arg interface{}) aurora.Value { //nolint:ireturn 176 if !c.LogColor { 177 return aurora.Reset(arg) 178 } 179 return aurora.Yellow(arg) 180 }