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

fix: default not use new lib, only enable when use build tag with_color

This commit is contained in:
风之凌殇 2021-10-04 19:37:11 +08:00
parent 58a17c65a1
commit 69e5247bde
3 changed files with 99 additions and 59 deletions

30
global/log_format.go Normal file
View File

@ -0,0 +1,30 @@
//go:build !with_color
// +build !with_color
package global
import (
"strings"
"github.com/sirupsen/logrus"
)
// LogFormat specialize for go-cqhttp
type LogFormat struct{}
// Format implements logrus.Formatter
func (f LogFormat) Format(entry *logrus.Entry) ([]byte, error) {
buf := NewBuffer()
defer PutBuffer(buf)
buf.WriteByte('[')
buf.WriteString(entry.Time.Format("2006-01-02 15:04:05"))
buf.WriteString("] [")
buf.WriteString(strings.ToUpper(entry.Level.String()))
buf.WriteString("]: ")
buf.WriteString(entry.Message)
buf.WriteString(" \n")
ret := append([]byte(nil), buf.Bytes()...) // copy buffer
return ret, nil
}

View File

@ -0,0 +1,69 @@
//go:build with_color
// +build with_color
package global
import (
"fmt"
"strings"
"github.com/gookit/color"
"github.com/sirupsen/logrus"
)
// LogFormat specialize for go-cqhttp
type LogFormat struct{}
// Format implements logrus.Formatter
func (f LogFormat) Format(entry *logrus.Entry) ([]byte, error) {
buf := NewBuffer()
defer PutBuffer(buf)
buf.WriteString(getLogLevelColorCode(entry.Level))
buf.WriteByte('[')
buf.WriteString(entry.Time.Format("2006-01-02 15:04:05"))
buf.WriteString("] [")
buf.WriteString(strings.ToUpper(entry.Level.String()))
buf.WriteString("]: ")
buf.WriteString(entry.Message)
buf.WriteString(" \n")
buf.WriteString(color.ResetSet)
ret := append([]byte(nil), buf.Bytes()...) // copy buffer
return ret, nil
}
var (
colorCodePanic = fmt.Sprintf(color.SettingTpl, color.Style{color.Bold, color.Red}.String())
colorCodeFatal = fmt.Sprintf(color.SettingTpl, color.Style{color.Bold, color.Red}.String())
colorCodeError = fmt.Sprintf(color.SettingTpl, color.Style{color.Red}.String())
colorCodeWarn = fmt.Sprintf(color.SettingTpl, color.Style{color.Yellow}.String())
colorCodeInfo = fmt.Sprintf(color.SettingTpl, color.Style{color.Green}.String())
colorCodeDebug = fmt.Sprintf(color.SettingTpl, color.Style{color.White}.String())
colorCodeTrace = fmt.Sprintf(color.SettingTpl, color.Style{color.Cyan}.String())
)
// getLogLevelColorCode 获取日志等级对应色彩code
func getLogLevelColorCode(level logrus.Level) string {
switch level {
case logrus.PanicLevel:
return colorCodePanic
case logrus.FatalLevel:
return colorCodeFatal
case logrus.ErrorLevel:
return colorCodeError
case logrus.WarnLevel:
return colorCodeWarn
case logrus.InfoLevel:
return colorCodeInfo
case logrus.DebugLevel:
return colorCodeDebug
case logrus.TraceLevel:
return colorCodeTrace
default:
return colorCodeInfo
}
}

View File

@ -6,10 +6,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings"
"sync" "sync"
"github.com/gookit/color"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -176,60 +174,3 @@ func GetLogLevel(level string) []logrus.Level {
} }
} }
} }
// LogFormat specialize for go-cqhttp
type LogFormat struct{}
// Format implements logrus.Formatter
func (f LogFormat) Format(entry *logrus.Entry) ([]byte, error) {
buf := NewBuffer()
defer PutBuffer(buf)
buf.WriteString(GetLogLevelColorCode(entry.Level))
buf.WriteByte('[')
buf.WriteString(entry.Time.Format("2006-01-02 15:04:05"))
buf.WriteString("] [")
buf.WriteString(strings.ToUpper(entry.Level.String()))
buf.WriteString("]: ")
buf.WriteString(entry.Message)
buf.WriteString(" \n")
buf.WriteString(color.ResetSet)
ret := append([]byte(nil), buf.Bytes()...) // copy buffer
return ret, nil
}
var (
colorCodePanic = fmt.Sprintf(color.SettingTpl, color.Style{color.Bold, color.Red}.String())
colorCodeFatal = fmt.Sprintf(color.SettingTpl, color.Style{color.Bold, color.Red}.String())
colorCodeError = fmt.Sprintf(color.SettingTpl, color.Style{color.Red}.String())
colorCodeWarn = fmt.Sprintf(color.SettingTpl, color.Style{color.Yellow}.String())
colorCodeInfo = fmt.Sprintf(color.SettingTpl, color.Style{color.Green}.String())
colorCodeDebug = fmt.Sprintf(color.SettingTpl, color.Style{color.White}.String())
colorCodeTrace = fmt.Sprintf(color.SettingTpl, color.Style{color.Cyan}.String())
)
// GetLogLevelColorCode 获取日志等级对应色彩code
func GetLogLevelColorCode(level logrus.Level) string {
switch level {
case logrus.PanicLevel:
return colorCodePanic
case logrus.FatalLevel:
return colorCodeFatal
case logrus.ErrorLevel:
return colorCodeError
case logrus.WarnLevel:
return colorCodeWarn
case logrus.InfoLevel:
return colorCodeInfo
case logrus.DebugLevel:
return colorCodeDebug
case logrus.TraceLevel:
return colorCodeTrace
default:
return colorCodeInfo
}
}