github.com/XiaoMi/Gaea@v1.2.5/log/xlog/logger.go (about) 1 // Copyright 2019 The Gaea Authors. All Rights Reserved. 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 /* 16 小米网golang日志库 17 18 小米网golang日志库支持6种日志级别: 19 20 1)Debug 21 2)Trace 22 3)Notice 23 4)Warn 24 5)Fatal 25 6)None 26 27 支持两种输出格式: 28 29 1)json格式 30 2)自定义输出 31 32 日志级别优先级: 33 34 Debug < Trace < Notice < Warn < Fatal < None 35 36 即如果定义日志级别为Debug:则Trace、Notice、Warn、Fatal等级别的日志都会输出;反之,如果定义日志级别为Trace,则Debug不会输出,其它级别的日志都会输出。当日志级别为None时,将不会有任何日志输出。 37 38 */ 39 40 package xlog 41 42 import ( 43 "errors" 44 "fmt" 45 "sync" 46 ) 47 48 // LogInstance wraps a XLogger 49 type LogInstance struct { 50 logger XLogger 51 enable bool 52 initial bool 53 source string 54 } 55 56 // LogManager is the manager that hold different kind of xlog loggers 57 type LogManager struct { 58 loggers map[string]*LogInstance 59 lock sync.RWMutex 60 } 61 62 // CreateLogManager create log manager from configs. 63 func CreateLogManager(name string, config map[string]string, source ...string) (*LogManager, error) { 64 mgr := &LogManager{ 65 loggers: make(map[string]*LogInstance), 66 } 67 68 if err := mgr.Init(name, config, source...); err != nil { 69 return nil, err 70 } 71 72 return mgr, nil 73 } 74 75 // Init LogManager with config. The name can be set to "console" or "file". 76 // Please reference to XConsoleLog and XFileLog. 77 func (l *LogManager) Init(name string, config map[string]string, source ...string) (err error) { 78 if err := l.RegisterLogger("console", NewXConsoleLog()); err != nil { 79 return err 80 } 81 82 if err := l.RegisterLogger("file", NewXFileLog()); err != nil { 83 return err 84 } 85 86 err = l.initLogger(name, config) 87 //关闭自动注入的logger 88 for _, v := range l.loggers { 89 if v.logger == nil || !v.enable { 90 continue 91 } 92 93 if v.source == "auto" { 94 v.enable = false 95 } 96 } 97 98 return 99 } 100 101 func (l *LogManager) initLogger(name string, config map[string]string, source ...string) (err error) { 102 l.lock.Lock() 103 defer l.lock.Unlock() 104 105 instance, ok := l.loggers[name] 106 if !ok { 107 err = fmt.Errorf("not found logger[%s]", name) 108 return 109 } 110 111 err = instance.logger.Init(config) 112 if err != nil { 113 return 114 } 115 116 if len(source) > 0 { 117 instance.source = source[0] 118 } else { 119 instance.source = "" 120 } 121 122 instance.enable = true 123 instance.initial = true 124 125 return 126 } 127 128 // RegisterLogger register a logger 129 func (l *LogManager) RegisterLogger(name string, logger XLogger) (err error) { 130 l.lock.Lock() 131 defer l.lock.Unlock() 132 133 _, ok := l.loggers[name] 134 if ok { 135 err = fmt.Errorf("duplicate logger[%s]", name) 136 return 137 } 138 139 l.loggers[name] = &LogInstance{ 140 logger: logger, 141 enable: false, 142 initial: false, 143 } 144 145 return 146 } 147 148 // UnregisterLogger unregister a logger 149 func (l *LogManager) UnregisterLogger(name string) (err error) { 150 l.lock.Lock() 151 defer l.lock.Unlock() 152 153 v, ok := l.loggers[name] 154 if !ok { 155 err = fmt.Errorf("not found logger[%s]", name) 156 return 157 } 158 159 if v != nil { 160 v.logger.Close() 161 } 162 163 delete(l.loggers, name) 164 return 165 } 166 167 // EnableLogger enable or disable a logger 168 func (l *LogManager) EnableLogger(name string, enable bool) (err error) { 169 l.lock.Lock() 170 defer l.lock.Unlock() 171 172 instance, ok := l.loggers[name] 173 if !ok { 174 err = fmt.Errorf("not found logger[%s]", name) 175 return 176 } 177 178 if !instance.initial { 179 instance.enable = false 180 return 181 } 182 183 instance.enable = enable 184 return 185 } 186 187 // GetLogger get logger by name 188 func (l *LogManager) GetLogger(name string) (logger XLogger, err error) { 189 l.lock.RLock() 190 defer l.lock.RUnlock() 191 192 instance, ok := l.loggers[name] 193 if !ok { 194 err = fmt.Errorf("not found logger[%s]", name) 195 return 196 } 197 198 logger = instance.logger 199 return 200 } 201 202 // ReOpen reopen all enabled loggers 203 func (l *LogManager) ReOpen() (err error) { 204 l.lock.RLock() 205 defer l.lock.RUnlock() 206 207 var errorMsg string 208 for k, v := range l.loggers { 209 210 if v.logger == nil || !v.enable { 211 continue 212 } 213 214 errRet := v.logger.ReOpen() 215 if errRet != nil { 216 errorMsg += fmt.Sprintf("logger[%s] reload failed, err[%v]\n", k, errRet) 217 continue 218 } 219 } 220 221 err = errors.New(errorMsg) 222 return 223 } 224 225 // SetLevelAll set level to all loggers 226 func (l *LogManager) SetLevelAll(level string) { 227 l.lock.RLock() 228 defer l.lock.RUnlock() 229 230 for _, v := range l.loggers { 231 if !v.enable || v.logger == nil { 232 continue 233 } 234 235 v.logger.SetLevel(level) 236 } 237 } 238 239 // SetLevel implements XLogger 240 func (l *LogManager) SetLevel(name, level string) (err error) { 241 l.lock.Lock() 242 defer l.lock.Unlock() 243 244 v, ok := l.loggers[name] 245 if !ok || v.logger == nil { 246 err = fmt.Errorf("not found logger[%s]", name) 247 return 248 } 249 250 v.logger.SetLevel(level) 251 return 252 } 253 254 // Warn implements XLogger 255 func (l *LogManager) Warn(format string, a ...interface{}) (err error) { 256 l.lock.RLock() 257 defer l.lock.RUnlock() 258 259 for _, v := range l.loggers { 260 if v.logger == nil || !v.enable { 261 continue 262 } 263 v.logger.Warn(format, a...) 264 } 265 266 return 267 } 268 269 // Fatal implements XLogger 270 func (l *LogManager) Fatal(format string, a ...interface{}) (err error) { 271 l.lock.RLock() 272 defer l.lock.RUnlock() 273 274 for _, v := range l.loggers { 275 if v.logger == nil || !v.enable { 276 continue 277 } 278 v.logger.Fatal(format, a...) 279 } 280 281 return 282 } 283 284 // Notice implements XLogger 285 func (l *LogManager) Notice(format string, a ...interface{}) (err error) { 286 l.lock.RLock() 287 defer l.lock.RUnlock() 288 289 for _, v := range l.loggers { 290 if v.logger == nil || !v.enable { 291 continue 292 } 293 v.logger.Notice(format, a...) 294 } 295 296 return 297 } 298 299 // Trace implements XLogger 300 func (l *LogManager) Trace(format string, a ...interface{}) (err error) { 301 l.lock.RLock() 302 defer l.lock.RUnlock() 303 304 for _, v := range l.loggers { 305 if v.logger == nil || !v.enable { 306 continue 307 } 308 v.logger.Trace(format, a...) 309 } 310 311 return 312 } 313 314 // Debug implements XLogger 315 func (l *LogManager) Debug(format string, a ...interface{}) (err error) { 316 l.lock.RLock() 317 defer l.lock.RUnlock() 318 319 for _, v := range l.loggers { 320 if v.logger == nil || !v.enable { 321 continue 322 } 323 v.logger.Debug(format, a...) 324 } 325 326 return 327 } 328 329 // Warnx implements XLogger 330 func (l *LogManager) Warnx(logID, format string, a ...interface{}) (err error) { 331 l.lock.RLock() 332 defer l.lock.RUnlock() 333 334 for _, v := range l.loggers { 335 if v.logger == nil || !v.enable { 336 continue 337 } 338 v.logger.Warnx(logID, format, a...) 339 } 340 341 return 342 } 343 344 // Fatalx implements XLogger 345 func (l *LogManager) Fatalx(logID, format string, a ...interface{}) (err error) { 346 l.lock.RLock() 347 defer l.lock.RUnlock() 348 349 for _, v := range l.loggers { 350 if v.logger == nil || !v.enable { 351 continue 352 } 353 v.logger.Fatalx(logID, format, a...) 354 } 355 356 return 357 } 358 359 // Noticex implements XLogger 360 func (l *LogManager) Noticex(logID, format string, a ...interface{}) (err error) { 361 l.lock.RLock() 362 defer l.lock.RUnlock() 363 364 for _, v := range l.loggers { 365 if v.logger == nil || !v.enable { 366 continue 367 } 368 v.logger.Noticex(logID, format, a...) 369 } 370 371 return 372 } 373 374 // Tracex implements XLogger 375 func (l *LogManager) Tracex(logID, format string, a ...interface{}) (err error) { 376 l.lock.RLock() 377 defer l.lock.RUnlock() 378 379 for _, v := range l.loggers { 380 if v.logger == nil || !v.enable { 381 continue 382 } 383 v.logger.Tracex(logID, format, a...) 384 } 385 386 return 387 } 388 389 // Debugx implements XLogger 390 func (l *LogManager) Debugx(logID, format string, a ...interface{}) (err error) { 391 l.lock.RLock() 392 defer l.lock.RUnlock() 393 394 for _, v := range l.loggers { 395 if v.logger == nil || !v.enable { 396 continue 397 } 398 v.logger.Debugx(logID, format, a...) 399 } 400 401 return 402 } 403 404 // Close implements XLogger 405 func (l *LogManager) Close() { 406 l.lock.RLock() 407 defer l.lock.RUnlock() 408 409 for _, v := range l.loggers { 410 if v.logger == nil || !v.enable { 411 continue 412 } 413 v.logger.Close() 414 } 415 416 return 417 }