| 1 | package logr |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "log" |
| 6 | "os" |
| 7 | ) |
| 8 | |
| 9 | var defaultLogger = New(LevelInfo, false, os.Stdout) |
| 10 | |
| 11 | type Logger struct { |
| 12 | // threshold is the minimum level output by this logger. |
| 13 | // |
| 14 | // For example, if the threshold is set to LevelWarn, then only logs of LevelWarn and LevelError will be output |
| 15 | // while logs of LevelDebug and LevelInfo will be ignored. |
| 16 | // |
| 17 | // Defaults to LevelInfo. |
| 18 | threshold Level |
| 19 | |
| 20 | // shouldPrefixMessageWithLevel is whether to include the log level prefix in each log. |
| 21 | // |
| 22 | // For example, if this is set to true, then a debug log would output "<date> DEBUG <message>" |
| 23 | // |
| 24 | // Defaults to false. |
| 25 | shouldPrefixMessageWithLevel bool |
| 26 | |
| 27 | stdLogger *log.Logger |
| 28 | } |
| 29 | |
| 30 | // New creates a new logger with the given threshold and output. |
| 31 | func New(threshold Level, shouldPrefixMessageWithLevel bool, output io.Writer) *Logger { |
| 32 | if !threshold.IsValid() { |
| 33 | threshold = LevelInfo // Default to LevelInfo if the threshold is invalid |
| 34 | } |
| 35 | return &Logger{ |
| 36 | threshold: threshold, |
| 37 | shouldPrefixMessageWithLevel: shouldPrefixMessageWithLevel, |
| 38 | stdLogger: log.New(output, "", log.LstdFlags), |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func (logger *Logger) SetOutput(output io.Writer) { |
| 43 | // Because we're using the standard log package under the hood, we can just directly pass this |
| 44 | // to the logger and call it a day. It already takes care of locking and discarding the previous writer. |
| 45 | logger.stdLogger.SetOutput(output) |
| 46 | } |
| 47 | |
| 48 | func (logger *Logger) SetThreshold(threshold Level) { |
| 49 | if !threshold.IsValid() { |
| 50 | threshold = LevelInfo // Default to LevelInfo if the threshold is invalid |
| 51 | } else { |
| 52 | logger.threshold = threshold |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func (logger *Logger) GetThreshold() Level { |
| 57 | return logger.threshold |
| 58 | } |
| 59 | |
| 60 | func (logger *Logger) Log(level Level, message string) { |
| 61 | logger.Logf(level, message) |
| 62 | } |
| 63 | |
| 64 | func (logger *Logger) Logf(level Level, format string, args ...any) { |
| 65 | if level.Value() < logger.threshold.Value() { |
| 66 | // The log level is below the threshold, so ignore the log |
| 67 | return |
| 68 | } |
| 69 | if logger.shouldPrefixMessageWithLevel { |
| 70 | format = "- " + string(level) + " - " + format |
| 71 | } |
| 72 | logger.stdLogger.Printf(format, args...) |
| 73 | if level == LevelFatal { |
| 74 | os.Exit(1) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func (logger *Logger) Debug(message string) { |
| 79 | logger.Log(LevelDebug, message) |
| 80 | } |
| 81 | |
| 82 | func (logger *Logger) Debugf(format string, args ...any) { |
| 83 | logger.Logf(LevelDebug, format, args...) |
| 84 | } |
| 85 | |
| 86 | func (logger *Logger) Info(message string) { |
| 87 | logger.Log(LevelInfo, message) |
| 88 | } |
| 89 | |
| 90 | func (logger *Logger) Infof(format string, args ...any) { |
| 91 | logger.Logf(LevelInfo, format, args...) |
| 92 | } |
| 93 | |
| 94 | func (logger *Logger) Warn(message string) { |
| 95 | logger.Log(LevelWarn, message) |
| 96 | } |
| 97 | |
| 98 | func (logger *Logger) Warnf(format string, args ...any) { |
| 99 | logger.Logf(LevelWarn, format, args...) |
| 100 | } |
| 101 | |
| 102 | func (logger *Logger) Error(message string) { |
| 103 | logger.Log(LevelError, message) |
| 104 | } |
| 105 | |
| 106 | func (logger *Logger) Errorf(format string, args ...any) { |
| 107 | logger.Logf(LevelError, format, args...) |
| 108 | } |
| 109 | |
| 110 | func (logger *Logger) Fatal(message string) { |
| 111 | logger.Log(LevelFatal, message) |
| 112 | } |
| 113 | |
| 114 | func (logger *Logger) Fatalf(format string, args ...any) { |
| 115 | logger.Logf(LevelFatal, format, args...) |
| 116 | } |
| 117 | |
| 118 | // SetOutput sets the output of the default logger |
| 119 | func SetOutput(output io.Writer) { |
| 120 | defaultLogger.SetOutput(output) |
| 121 | } |
| 122 | |
| 123 | // SetThreshold sets the minimum level output by the default logger |
| 124 | func SetThreshold(threshold Level) { |
| 125 | defaultLogger.SetThreshold(threshold) |
| 126 | } |
| 127 | |
| 128 | func GetThreshold() Level { |
| 129 | return defaultLogger.GetThreshold() |
| 130 | } |
| 131 | |
| 132 | func Log(level Level, message string) { |
| 133 | defaultLogger.Log(level, message) |
| 134 | } |
| 135 | |
| 136 | func Logf(level Level, message string, args ...any) { |
| 137 | defaultLogger.Logf(level, message, args) |
| 138 | } |
| 139 | |
| 140 | func Debug(message string) { |
| 141 | defaultLogger.Debug(message) |
| 142 | } |
| 143 | |
| 144 | func Debugf(format string, args ...any) { |
| 145 | defaultLogger.Debugf(format, args...) |
| 146 | } |
| 147 | |
| 148 | func Info(message string) { |
| 149 | defaultLogger.Info(message) |
| 150 | } |
| 151 | |
| 152 | func Infof(format string, args ...any) { |
| 153 | defaultLogger.Infof(format, args...) |
| 154 | } |
| 155 | |
| 156 | func Warn(message string) { |
| 157 | defaultLogger.Warn(message) |
| 158 | } |
| 159 | |
| 160 | func Warnf(format string, args ...any) { |
| 161 | defaultLogger.Warnf(format, args...) |
| 162 | } |
| 163 | |
| 164 | func Error(message string) { |
| 165 | defaultLogger.Error(message) |
| 166 | } |
| 167 | |
| 168 | func Errorf(format string, args ...any) { |
| 169 | defaultLogger.Errorf(format, args...) |
| 170 | } |
| 171 | |
| 172 | func Fatal(message string) { |
| 173 | defaultLogger.Fatal(message) |
| 174 | } |
| 175 | |
| 176 | func Fatalf(format string, args ...any) { |
| 177 | defaultLogger.Fatalf(format, args...) |
| 178 | } |