github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/util/logger.go (about) 1 package util 2 3 import ( 4 "fmt" 5 "github.com/fatih/color" 6 "sync" 7 "time" 8 ) 9 10 const ( 11 // LevelError 错误 12 LevelError = iota 13 // LevelWarning 警告 14 LevelWarning 15 // LevelInformational 提示 16 LevelInformational 17 // LevelDebug 除错 18 LevelDebug 19 ) 20 21 var GloablLogger *Logger 22 var Level = LevelDebug 23 24 // Logger 日志 25 type Logger struct { 26 level int 27 mu sync.Mutex 28 } 29 30 // 日志颜色 31 var colors = map[string]func(a ...interface{}) string{ 32 "Warning": color.New(color.FgYellow).Add(color.Bold).SprintFunc(), 33 "Panic": color.New(color.BgRed).Add(color.Bold).SprintFunc(), 34 "Error": color.New(color.FgRed).Add(color.Bold).SprintFunc(), 35 "Info": color.New(color.FgCyan).Add(color.Bold).SprintFunc(), 36 "Debug": color.New(color.FgWhite).Add(color.Bold).SprintFunc(), 37 } 38 39 // 不同级别前缀与时间的间隔,保持宽度一致 40 var spaces = map[string]string{ 41 "Warning": "", 42 "Panic": " ", 43 "Error": " ", 44 "Info": " ", 45 "Debug": " ", 46 } 47 48 // Println 打印 49 func (ll *Logger) Println(prefix string, msg string) { 50 // TODO Release时去掉 51 // color.NoColor = false 52 53 c := color.New() 54 55 ll.mu.Lock() 56 defer ll.mu.Unlock() 57 58 _, _ = c.Printf( 59 "%s%s %s %s\n", 60 colors[prefix]("["+prefix+"]"), 61 spaces[prefix], 62 time.Now().Format("2006-01-02 15:04:05"), 63 msg, 64 ) 65 } 66 67 // Panic 极端错误 68 func (ll *Logger) Panic(format string, v ...interface{}) { 69 if LevelError > ll.level { 70 return 71 } 72 msg := fmt.Sprintf(format, v...) 73 ll.Println("Panic", msg) 74 panic(msg) 75 } 76 77 // Error 错误 78 func (ll *Logger) Error(format string, v ...interface{}) { 79 if LevelError > ll.level { 80 return 81 } 82 msg := fmt.Sprintf(format, v...) 83 ll.Println("Error", msg) 84 } 85 86 // Warning 警告 87 func (ll *Logger) Warning(format string, v ...interface{}) { 88 if LevelWarning > ll.level { 89 return 90 } 91 msg := fmt.Sprintf(format, v...) 92 ll.Println("Warning", msg) 93 } 94 95 // Info 信息 96 func (ll *Logger) Info(format string, v ...interface{}) { 97 if LevelInformational > ll.level { 98 return 99 } 100 msg := fmt.Sprintf(format, v...) 101 ll.Println("Info", msg) 102 } 103 104 // Debug 校验 105 func (ll *Logger) Debug(format string, v ...interface{}) { 106 if LevelDebug > ll.level { 107 return 108 } 109 msg := fmt.Sprintf(format, v...) 110 ll.Println("Debug", msg) 111 } 112 113 // Print GORM 的 Logger实现 114 //func (ll *Logger) Print(v ...interface{}) { 115 // if LevelDebug > ll.level { 116 // return 117 // } 118 // msg := fmt.Sprintf("[SQL] %s", v...) 119 // ll.Println(msg) 120 //} 121 122 // BuildLogger 构建logger 123 func BuildLogger(level string) { 124 intLevel := LevelError 125 switch level { 126 case "error": 127 intLevel = LevelError 128 case "warning": 129 intLevel = LevelWarning 130 case "info": 131 intLevel = LevelInformational 132 case "debug": 133 intLevel = LevelDebug 134 } 135 l := Logger{ 136 level: intLevel, 137 } 138 GloablLogger = &l 139 } 140 141 // Log 返回日志对象 142 func Log() *Logger { 143 if GloablLogger == nil { 144 l := Logger{ 145 level: Level, 146 } 147 GloablLogger = &l 148 } 149 return GloablLogger 150 }