1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 03:23:49 +08:00

style: clean log color constant

This commit is contained in:
wdvxdr 2021-10-16 16:36:51 +08:00
parent 6317e6c853
commit 2eec74ee09
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6

View File

@ -85,9 +85,6 @@ func (hook *LocalHook) SetFormatter(consoleFormatter, fileFormatter logrus.Forma
hook.lock.Lock() hook.lock.Lock()
defer hook.lock.Unlock() defer hook.lock.Unlock()
consoleFormatter = tryChangeFormatter(consoleFormatter)
fileFormatter = tryChangeFormatter(fileFormatter)
// 支持处理windows平台的console色彩 // 支持处理windows平台的console色彩
logrus.SetOutput(colorable.NewColorableStdout()) logrus.SetOutput(colorable.NewColorableStdout())
// 用于在console写出 // 用于在console写出
@ -96,23 +93,6 @@ func (hook *LocalHook) SetFormatter(consoleFormatter, fileFormatter logrus.Forma
hook.formatter = fileFormatter hook.formatter = fileFormatter
} }
func tryChangeFormatter(formatter logrus.Formatter) logrus.Formatter {
if formatter == nil {
// 用默认的
formatter = &logrus.TextFormatter{DisableColors: true}
} else {
switch f := formatter.(type) {
case *logrus.TextFormatter:
textFormatter := f
textFormatter.DisableColors = true
default:
// todo
}
}
return formatter
}
// SetWriter 设置Writer // SetWriter 设置Writer
func (hook *LocalHook) SetWriter(writer io.Writer) { func (hook *LocalHook) SetWriter(writer io.Writer) {
hook.lock.Lock() hook.lock.Lock()
@ -212,31 +192,22 @@ func (f LogFormat) Format(entry *logrus.Entry) ([]byte, error) {
buf.WriteString(" \n") buf.WriteString(" \n")
if f.EnableColor { if f.EnableColor {
buf.WriteString(ResetSet) buf.WriteString(colorReset)
} }
ret := append([]byte(nil), buf.Bytes()...) // copy buffer ret := append([]byte(nil), buf.Bytes()...) // copy buffer
return ret, nil return ret, nil
} }
// 为了不引入新依赖,直接将对应库需要的部分复制过来了,具体可参考 github.com\gookit\color@v1.4.2\color.go
// ResetSet 重置色彩 ansi code
const ResetSet = "\x1b[0m"
const ( const (
// SettingTpl 开始色彩 ansi code colorCodePanic = "\x1b[1;31m" // color.Style{color.Bold, color.Red}.String()
SettingTpl = "\x1b[%sm" colorCodeFatal = "\x1b[1;31m" // color.Style{color.Bold, color.Red}.String()
) colorCodeError = "\x1b[31m" // color.Style{color.Red}.String()
colorCodeWarn = "\x1b[33m" // color.Style{color.Yellow}.String()
var ( colorCodeInfo = "\x1b[32m" // color.Style{color.Green}.String()
colorCodePanic = fmt.Sprintf(SettingTpl, "1;31") // color.Style{color.Bold, color.Red}.String() colorCodeDebug = "\x1b[37m" // color.Style{color.White}.String()
colorCodeFatal = fmt.Sprintf(SettingTpl, "1;31") // color.Style{color.Bold, color.Red}.String() colorCodeTrace = "\x1b[36m" // color.Style{color.Cyan}.String()
colorCodeError = fmt.Sprintf(SettingTpl, "31") // color.Style{color.Red}.String() colorReset = "\x1b[0m"
colorCodeWarn = fmt.Sprintf(SettingTpl, "33") // color.Style{color.Yellow}.String()
colorCodeInfo = fmt.Sprintf(SettingTpl, "32") // color.Style{color.Green}.String()
colorCodeDebug = fmt.Sprintf(SettingTpl, "37") // color.Style{color.White}.String()
colorCodeTrace = fmt.Sprintf(SettingTpl, "36") // color.Style{color.Cyan}.String()
) )
// GetLogLevelColorCode 获取日志等级对应色彩code // GetLogLevelColorCode 获取日志等级对应色彩code