github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/logger/log.go (about) 1 package logger 2 3 import ( 4 "fmt" 5 "io" 6 "log" 7 "os" 8 "runtime" 9 ) 10 11 type ( 12 LogLevel int 13 LogType int 14 ) 15 16 const ( 17 LogFatal = LogType(0x1) 18 LogError = LogType(0x2) 19 LogWarning = LogType(0x4) 20 LogInfo = LogType(0x8) 21 LogDebug = LogType(0x10) 22 ) 23 24 const ( 25 LogLevelNone = LogLevel(0x0) 26 LogLevelFatal = LogLevelNone | LogLevel(LogFatal) 27 LogLevelError = LogLevelFatal | LogLevel(LogError) 28 LogLevelWarn = LogLevelError | LogLevel(LogWarning) 29 LogLevelInfo = LogLevelWarn | LogLevel(LogInfo) 30 LogLevelDebug = LogLevelInfo | LogLevel(LogDebug) 31 LogLevelAll = LogLevelDebug 32 ) 33 34 var _log = New() 35 36 func init() { 37 SetFlags(log.Ldate | log.Ltime | log.Lshortfile) 38 SetHighlighting(runtime.GOOS != "windows") 39 } 40 41 func New() *Logger { 42 return NewLogger(os.Stderr, "") 43 } 44 45 func NewLogger(w io.Writer, prefix string) *Logger { 46 var level LogLevel 47 if l := os.Getenv("LOG_LEVEL"); len(l) != 0 { 48 level = StringToLogLevel(os.Getenv("LOG_LEVEL")) 49 } else { 50 level = LogLevelInfo 51 } 52 return &Logger{_log: log.New(w, prefix, log.LstdFlags), level: level, highlighting: true} 53 } 54 55 func GlobalLogger() *log.Logger { 56 return _log._log 57 } 58 59 func SetLevel(level LogLevel) { 60 _log.SetLevel(level) 61 } 62 func GetLogLevel() LogLevel { 63 return _log.level 64 } 65 66 func SetFlags(flags int) { 67 _log._log.SetFlags(flags) 68 } 69 70 func Info(v ...interface{}) { 71 _log.Info(v...) 72 } 73 74 func Infof(format string, v ...interface{}) { 75 _log.Infof(format, v...) 76 } 77 78 func Panic(v ...interface{}) { 79 _log.Panic(v...) 80 } 81 82 func Panicf(format string, v ...interface{}) { 83 _log.Panicf(format, v...) 84 } 85 86 func Debug(v ...interface{}) { 87 _log.Debug(v...) 88 } 89 90 func Debugf(format string, v ...interface{}) { 91 _log.Debugf(format, v...) 92 } 93 94 func Warn(v ...interface{}) { 95 _log.Warn(v...) 96 } 97 98 func Warnf(format string, v ...interface{}) { 99 _log.Warnf(format, v...) 100 } 101 102 func Error(v ...interface{}) { 103 _log.Error(v...) 104 } 105 106 func Errorf(format string, v ...interface{}) { 107 _log.Errorf(format, v...) 108 } 109 110 func Fatal(v ...interface{}) { 111 _log.Fatal(v...) 112 } 113 114 func Fatalf(format string, v ...interface{}) { 115 _log.Fatalf(format, v...) 116 } 117 118 func SetLevelByString(level string) { 119 _log.SetLevelByString(level) 120 } 121 122 func SetHighlighting(highlighting bool) { 123 _log.SetHighlighting(highlighting) 124 } 125 126 type Logger struct { 127 _log *log.Logger 128 level LogLevel 129 highlighting bool 130 } 131 132 func (l *Logger) SetHighlighting(highlighting bool) { 133 l.highlighting = highlighting 134 } 135 136 func (l *Logger) SetFlags(flags int) { 137 l._log.SetFlags(flags) 138 } 139 140 func (l *Logger) Flags() int { 141 return l._log.Flags() 142 } 143 144 func (l *Logger) SetLevel(level LogLevel) { 145 l.level = level 146 } 147 148 func (l *Logger) SetLevelByString(level string) { 149 l.level = StringToLogLevel(level) 150 } 151 152 func (l *Logger) log(t LogType, v ...interface{}) { 153 l.logf(t, "%v\n", v) 154 } 155 156 func (l *Logger) logf(t LogType, format string, v ...interface{}) { 157 if l.level|LogLevel(t) != l.level { 158 return 159 } 160 161 logStr, logColor := LogTypeToString(t) 162 var s string 163 if l.highlighting { 164 s = "\033" + logColor + "m[" + logStr + "] " + fmt.Sprintf(format, v...) + "\033[0m" 165 } else { 166 s = "[" + logStr + "] " + fmt.Sprintf(format, v...) 167 } 168 _ = l._log.Output(4, s) 169 } 170 171 func (l *Logger) Fatal(v ...interface{}) { 172 l.log(LogFatal, v...) 173 os.Exit(-1) 174 } 175 176 func (l *Logger) Fatalf(format string, v ...interface{}) { 177 l.logf(LogFatal, format, v...) 178 os.Exit(-1) 179 } 180 181 func (l *Logger) Panic(v ...interface{}) { 182 l._log.Panic(v...) 183 } 184 185 func (l *Logger) Panicf(format string, v ...interface{}) { 186 l._log.Panicf(format, v...) 187 } 188 189 func (l *Logger) Error(v ...interface{}) { 190 l.log(LogError, v...) 191 } 192 193 func (l *Logger) Errorf(format string, v ...interface{}) { 194 l.logf(LogError, format, v...) 195 } 196 197 func (l *Logger) Warn(v ...interface{}) { 198 l.log(LogWarning, v...) 199 } 200 201 func (l *Logger) Warnf(format string, v ...interface{}) { 202 l.logf(LogWarning, format, v...) 203 } 204 205 func (l *Logger) Debug(v ...interface{}) { 206 l.log(LogDebug, v...) 207 } 208 209 func (l *Logger) Debugf(format string, v ...interface{}) { 210 l.logf(LogDebug, format, v...) 211 } 212 213 func (l *Logger) Info(v ...interface{}) { 214 l.log(LogInfo, v...) 215 } 216 217 func (l *Logger) Infof(format string, v ...interface{}) { 218 l.logf(LogInfo, format, v...) 219 } 220 221 func StringToLogLevel(level string) LogLevel { 222 switch level { 223 case "fatal": 224 return LogLevelFatal 225 case "error": 226 return LogLevelError 227 case "warn": 228 return LogLevelWarn 229 case "warning": 230 return LogLevelWarn 231 case "debug": 232 return LogLevelDebug 233 case "info": 234 return LogLevelInfo 235 } 236 return LogLevelAll 237 } 238 239 func LogTypeToString(t LogType) (string, string) { 240 switch t { 241 case LogFatal: 242 return "fatal", "[0;31" 243 case LogError: 244 return "error", "[0;31" 245 case LogWarning: 246 return "warning", "[0;33" 247 case LogDebug: 248 return "debug", "[0;36" 249 case LogInfo: 250 return "info", "[0;37" 251 } 252 return "unknown", "[0;37" 253 }