github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/util/logger/logger.go (about) 1 // Copyright 2021 ByteDance Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package logger 16 17 import ( 18 "context" 19 "fmt" 20 ) 21 22 // Logger is a logger interface that provides logging function with levels. 23 type Logger interface { 24 Trace(v ...interface{}) 25 Debug(v ...interface{}) 26 Info(v ...interface{}) 27 Notice(v ...interface{}) 28 Warn(v ...interface{}) 29 Error(v ...interface{}) 30 Fatal(v ...interface{}) 31 32 Tracef(format string, v ...interface{}) 33 Debugf(format string, v ...interface{}) 34 Infof(format string, v ...interface{}) 35 Noticef(format string, v ...interface{}) 36 Warnf(format string, v ...interface{}) 37 Errorf(format string, v ...interface{}) 38 Fatalf(format string, v ...interface{}) 39 40 CtxTracef(ctx context.Context, format string, v ...interface{}) 41 CtxDebugf(ctx context.Context, format string, v ...interface{}) 42 CtxInfof(ctx context.Context, format string, v ...interface{}) 43 CtxNoticef(ctx context.Context, format string, v ...interface{}) 44 CtxWarnf(ctx context.Context, format string, v ...interface{}) 45 CtxErrorf(ctx context.Context, format string, v ...interface{}) 46 CtxFatalf(ctx context.Context, format string, v ...interface{}) 47 } 48 49 // Level defines the priority of a log message. 50 // When a logger is configured with a level, any log message with a lower 51 // log level (smaller by integer comparison) will not be output. 52 type Level int 53 54 // The levels of logs. 55 const ( 56 LevelTrace Level = iota 57 LevelDebug 58 LevelInfo 59 LevelNotice 60 LevelWarn 61 LevelError 62 LevelFatal 63 ) 64 65 // SetLevel sets the level of logs below which logs will not be output. 66 // The default log level is LevelTrace. 67 func SetLevel(lv Level) { 68 if lv < LevelTrace || lv > LevelFatal { 69 panic("invalid level") 70 } 71 level = lv 72 } 73 74 // Fatal calls the default logger's Fatal method and then os.Exit(1). 75 func Fatal(v ...interface{}) { 76 defaultLogger.Fatal(v) 77 } 78 79 // Error calls the default logger's Error method. 80 func Error(v ...interface{}) { 81 if level > LevelError { 82 return 83 } 84 defaultLogger.Error(v) 85 } 86 87 // Warn calls the default logger's Warn method. 88 func Warn(v ...interface{}) { 89 if level > LevelWarn { 90 return 91 } 92 defaultLogger.Warn(v) 93 } 94 95 // Notice calls the default logger's Notice method. 96 func Notice(v ...interface{}) { 97 if level > LevelNotice { 98 return 99 } 100 defaultLogger.Notice(v) 101 } 102 103 // Info calls the default logger's Info method. 104 func Info(v ...interface{}) { 105 if level > LevelInfo { 106 return 107 } 108 defaultLogger.Info(v) 109 } 110 111 // Debug calls the default logger's Debug method. 112 func Debug(v ...interface{}) { 113 if level > LevelDebug { 114 return 115 } 116 defaultLogger.Debug(v) 117 } 118 119 // Trace calls the default logger's Trace method. 120 func Trace(v ...interface{}) { 121 if level > LevelTrace { 122 return 123 } 124 defaultLogger.Trace(v) 125 } 126 127 // Fatalf calls the default logger's Fatalf method and then os.Exit(1). 128 func Fatalf(format string, v ...interface{}) { 129 defaultLogger.Fatalf(format, v...) 130 } 131 132 // Errorf calls the default logger's Errorf method. 133 func Errorf(format string, v ...interface{}) { 134 if level > LevelError { 135 return 136 } 137 defaultLogger.Errorf(format, v...) 138 } 139 140 // Warnf calls the default logger's Warnf method. 141 func Warnf(format string, v ...interface{}) { 142 if level > LevelWarn { 143 return 144 } 145 defaultLogger.Warnf(format, v...) 146 } 147 148 // Noticef calls the default logger's Noticef method. 149 func Noticef(format string, v ...interface{}) { 150 if level > LevelNotice { 151 return 152 } 153 defaultLogger.Noticef(format, v...) 154 } 155 156 // Infof calls the default logger's Infof method. 157 func Infof(format string, v ...interface{}) { 158 if level > LevelInfo { 159 return 160 } 161 defaultLogger.Infof(format, v...) 162 } 163 164 // Debugf calls the default logger's Debugf method. 165 func Debugf(format string, v ...interface{}) { 166 if level > LevelDebug { 167 return 168 } 169 defaultLogger.Debugf(format, v...) 170 } 171 172 // Tracef calls the default logger's Tracef method. 173 func Tracef(format string, v ...interface{}) { 174 if level > LevelTrace { 175 return 176 } 177 defaultLogger.Tracef(format, v...) 178 } 179 180 // CtxFatalf calls the default logger's CtxFatalf method and then os.Exit(1). 181 func CtxFatalf(ctx context.Context, format string, v ...interface{}) { 182 defaultLogger.CtxFatalf(ctx, format, v...) 183 } 184 185 // CtxErrorf calls the default logger's CtxErrorf method. 186 func CtxErrorf(ctx context.Context, format string, v ...interface{}) { 187 if level > LevelError { 188 return 189 } 190 defaultLogger.CtxErrorf(ctx, format, v...) 191 } 192 193 // CtxWarnf calls the default logger's CtxWarnf method. 194 func CtxWarnf(ctx context.Context, format string, v ...interface{}) { 195 if level > LevelWarn { 196 return 197 } 198 defaultLogger.CtxWarnf(ctx, format, v...) 199 } 200 201 // CtxNoticef calls the default logger's CtxNoticef method. 202 func CtxNoticef(ctx context.Context, format string, v ...interface{}) { 203 if level > LevelNotice { 204 return 205 } 206 defaultLogger.CtxNoticef(ctx, format, v...) 207 } 208 209 // CtxInfof calls the default logger's CtxInfof method. 210 func CtxInfof(ctx context.Context, format string, v ...interface{}) { 211 if level > LevelInfo { 212 return 213 } 214 defaultLogger.CtxInfof(ctx, format, v...) 215 } 216 217 // CtxDebugf calls the default logger's CtxDebugf method. 218 func CtxDebugf(ctx context.Context, format string, v ...interface{}) { 219 if level > LevelDebug { 220 return 221 } 222 defaultLogger.CtxDebugf(ctx, format, v...) 223 } 224 225 // CtxTracef calls the default logger's CtxTracef method. 226 func CtxTracef(ctx context.Context, format string, v ...interface{}) { 227 if level > LevelTrace { 228 return 229 } 230 defaultLogger.CtxTracef(ctx, format, v...) 231 } 232 233 var level Level 234 235 var strs = []string{ 236 "[Trace] ", 237 "[Debug] ", 238 "[Info] ", 239 "[Notice] ", 240 "[Warn] ", 241 "[Error] ", 242 "[Fatal] ", 243 } 244 245 func (lv Level) toString() string { 246 if lv >= LevelTrace && lv <= LevelFatal { 247 return strs[lv] 248 } 249 return fmt.Sprintf("[?%d] ", lv) 250 }