lo.go
package lo import ( "log" "os" ) const ( TRACE int = iota DEBUG INFO WARN ERROR FATAL ) type LevelLogger struct { level int logger *log.Logger } var l *LevelLogger func init() { l = &LevelLogger{ level: TRACE, logger: log.New(os.Stdout, "", 0), } } func SetLevel(level int) { l.level = level } func SetLogger(logger *log.Logger) { l.logger = logger } func Trace(v ...any) { if TRACE >= l.level { l.logger.Println("[TRACE]", v) } } func Debug(v ...any) { if DEBUG >= l.level { l.logger.Println("[DEBUG]", v) } } func Info(v ...any) { if INFO >= l.level { l.logger.Println("[INFO]", v) } } func Warn(v ...any) { if WARN >= l.level { l.logger.Println("[WARN]", v) } } func Error(v ...any) { if ERROR >= l.level { l.logger.Println("[ERROR]", v) } } func Fatal(v ...any) { if FATAL >= l.level { l.logger.Fatalln("[FATAL]", v) } }
main.go
package main import ( "awesomeProject1/lo" ) func main() { lo.SetLevel(lo.FATAL) lo.Trace("the is Trace") lo.Debug("the is Debug") lo.Info("the is Info") lo.Warn("the is Warn") lo.Error("the is Error") lo.Fatal("the is Fatal") }
标签:...,level,lo,any,Golang,func,日志,级别,logger From: https://www.cnblogs.com/liudongcai/p/17720461.html