diff --git a/global/log_format.go b/global/log_format.go new file mode 100644 index 0000000..2893338 --- /dev/null +++ b/global/log_format.go @@ -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 +} diff --git a/global/log_format_with_color.go b/global/log_format_with_color.go new file mode 100644 index 0000000..0c82d58 --- /dev/null +++ b/global/log_format_with_color.go @@ -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 + } +} diff --git a/global/log_hook.go b/global/log_hook.go index c2a6675..00ab0d3 100644 --- a/global/log_hook.go +++ b/global/log_hook.go @@ -6,10 +6,8 @@ import ( "os" "path/filepath" "reflect" - "strings" "sync" - "github.com/gookit/color" "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 - } -}