mirror of https://github.com/cloudreve/Cloudreve
Feat: colorful log output for log prefix
parent
f9b37a3359
commit
e835dafc88
2
go.mod
2
go.mod
|
@ -4,6 +4,7 @@ go 1.12
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/DATA-DOG/go-sqlmock v1.3.3
|
github.com/DATA-DOG/go-sqlmock v1.3.3
|
||||||
|
github.com/fatih/color v1.7.0
|
||||||
github.com/gin-contrib/sessions v0.0.1
|
github.com/gin-contrib/sessions v0.0.1
|
||||||
github.com/gin-gonic/gin v1.4.0
|
github.com/gin-gonic/gin v1.4.0
|
||||||
github.com/go-ini/ini v1.50.0
|
github.com/go-ini/ini v1.50.0
|
||||||
|
@ -11,6 +12,7 @@ require (
|
||||||
github.com/go-playground/universal-translator v0.16.0 // indirect
|
github.com/go-playground/universal-translator v0.16.0 // indirect
|
||||||
github.com/jinzhu/gorm v1.9.11
|
github.com/jinzhu/gorm v1.9.11
|
||||||
github.com/leodido/go-urn v1.2.0 // indirect
|
github.com/leodido/go-urn v1.2.0 // indirect
|
||||||
|
github.com/mattn/go-colorable v0.1.4 // indirect
|
||||||
github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2
|
github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2
|
||||||
github.com/mojocn/base64Captcha v0.0.0-20190801020520-752b1cd608b2
|
github.com/mojocn/base64Captcha v0.0.0-20190801020520-752b1cd608b2
|
||||||
github.com/pkg/errors v0.8.0
|
github.com/pkg/errors v0.8.0
|
||||||
|
|
|
@ -2,6 +2,7 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/fatih/color"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -23,9 +24,19 @@ type Logger struct {
|
||||||
level int
|
level int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 日志颜色
|
||||||
|
var colors = map[string]func(a ...interface{}) string{
|
||||||
|
"Warning": color.New(color.FgYellow).Add(color.Bold).SprintFunc(),
|
||||||
|
"Panic": color.New(color.BgRed).Add(color.Bold).SprintFunc(),
|
||||||
|
"Error": color.New(color.FgRed).Add(color.Bold).SprintFunc(),
|
||||||
|
"Info": color.New(color.FgCyan).Add(color.Bold).SprintFunc(),
|
||||||
|
"Debug": color.New(color.FgWhite).Add(color.Bold).SprintFunc(),
|
||||||
|
}
|
||||||
|
|
||||||
// Println 打印
|
// Println 打印
|
||||||
func (ll *Logger) Println(msg string) {
|
func (ll *Logger) Println(prefix string, msg string) {
|
||||||
fmt.Printf("%s %s\n", time.Now().Format("2006-01-02 15:04:05 -0700"), msg)
|
c := color.New()
|
||||||
|
_, _ = c.Printf("%s %s %s\n", colors[prefix]("["+prefix+"]"), time.Now().Format("2006-01-02 15:04:05 -0700"), msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Panic 极端错误
|
// Panic 极端错误
|
||||||
|
@ -33,8 +44,8 @@ func (ll *Logger) Panic(format string, v ...interface{}) {
|
||||||
if LevelError > ll.level {
|
if LevelError > ll.level {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
msg := fmt.Sprintf("[Panic] "+format, v...)
|
msg := fmt.Sprintf(format, v...)
|
||||||
ll.Println(msg)
|
ll.Println("Panic", msg)
|
||||||
panic(msg)
|
panic(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,8 +54,8 @@ func (ll *Logger) Error(format string, v ...interface{}) {
|
||||||
if LevelError > ll.level {
|
if LevelError > ll.level {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
msg := fmt.Sprintf("[Error] "+format, v...)
|
msg := fmt.Sprintf(format, v...)
|
||||||
ll.Println(msg)
|
ll.Println("Error", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warning 警告
|
// Warning 警告
|
||||||
|
@ -52,8 +63,8 @@ func (ll *Logger) Warning(format string, v ...interface{}) {
|
||||||
if LevelWarning > ll.level {
|
if LevelWarning > ll.level {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
msg := fmt.Sprintf("[Warning] "+format, v...)
|
msg := fmt.Sprintf(format, v...)
|
||||||
ll.Println(msg)
|
ll.Println("Warning", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info 信息
|
// Info 信息
|
||||||
|
@ -61,8 +72,8 @@ func (ll *Logger) Info(format string, v ...interface{}) {
|
||||||
if LevelInformational > ll.level {
|
if LevelInformational > ll.level {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
msg := fmt.Sprintf("[Info] "+format, v...)
|
msg := fmt.Sprintf(format, v...)
|
||||||
ll.Println(msg)
|
ll.Println("Info", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug 校验
|
// Debug 校验
|
||||||
|
@ -70,18 +81,18 @@ func (ll *Logger) Debug(format string, v ...interface{}) {
|
||||||
if LevelDebug > ll.level {
|
if LevelDebug > ll.level {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
msg := fmt.Sprintf("[Debug] "+format, v...)
|
msg := fmt.Sprintf(format, v...)
|
||||||
ll.Println(msg)
|
ll.Println("Debug", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print GORM 的 Logger实现
|
// Print GORM 的 Logger实现
|
||||||
func (ll *Logger) Print(v ...interface{}) {
|
//func (ll *Logger) Print(v ...interface{}) {
|
||||||
if LevelDebug > ll.level {
|
// if LevelDebug > ll.level {
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
msg := fmt.Sprintf("[SQL] %s", v...)
|
// msg := fmt.Sprintf("[SQL] %s", v...)
|
||||||
ll.Println(msg)
|
// ll.Println(msg)
|
||||||
}
|
//}
|
||||||
|
|
||||||
// BuildLogger 构建logger
|
// BuildLogger 构建logger
|
||||||
func BuildLogger(level string) {
|
func BuildLogger(level string) {
|
||||||
|
|
Loading…
Reference in New Issue