From f784e94a4a50fbc19fd95aec31bddc79a77e7b5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E4=B9=8B=E5=87=8C=E6=AE=87?= Date: Tue, 5 Oct 2021 01:07:36 +0800 Subject: [PATCH] feat: console and file log use different formatter, and remove build tag --- global/log_format.go | 30 ------------ global/log_format_with_color.go | 69 -------------------------- global/log_hook.go | 85 +++++++++++++++++++++++++++++++-- main.go | 5 +- 4 files changed, 84 insertions(+), 105 deletions(-) delete mode 100644 global/log_format.go delete mode 100644 global/log_format_with_color.go diff --git a/global/log_format.go b/global/log_format.go deleted file mode 100644 index 2893338..0000000 --- a/global/log_format.go +++ /dev/null @@ -1,30 +0,0 @@ -//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 deleted file mode 100644 index 0c82d58..0000000 --- a/global/log_format_with_color.go +++ /dev/null @@ -1,69 +0,0 @@ -//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 00ab0d3..4d97f16 100644 --- a/global/log_hook.go +++ b/global/log_hook.go @@ -6,8 +6,10 @@ import ( "os" "path/filepath" "reflect" + "strings" "sync" + "github.com/gookit/color" "github.com/sirupsen/logrus" ) @@ -79,10 +81,20 @@ func (hook *LocalHook) Fire(entry *logrus.Entry) error { } // SetFormatter 设置日志格式 -func (hook *LocalHook) SetFormatter(formatter logrus.Formatter) { +func (hook *LocalHook) SetFormatter(consoleFormatter, fileFormatter logrus.Formatter) { hook.lock.Lock() defer hook.lock.Unlock() + consoleFormatter = tryChangeFormatter(consoleFormatter) + fileFormatter = tryChangeFormatter(fileFormatter) + + // 用于在console写出 + logrus.SetFormatter(consoleFormatter) + // 用于写入文件 + hook.formatter = fileFormatter +} + +func tryChangeFormatter(formatter logrus.Formatter) logrus.Formatter { if formatter == nil { // 用默认的 formatter = &logrus.TextFormatter{DisableColors: true} @@ -95,8 +107,8 @@ func (hook *LocalHook) SetFormatter(formatter logrus.Formatter) { // todo } } - logrus.SetFormatter(formatter) - hook.formatter = formatter + + return formatter } // SetWriter 设置Writer @@ -114,11 +126,11 @@ func (hook *LocalHook) SetPath(path string) { } // NewLocalHook 初始化本地日志钩子实现 -func NewLocalHook(args interface{}, formatter logrus.Formatter, levels ...logrus.Level) *LocalHook { +func NewLocalHook(args interface{}, consoleFormatter, fileFormatter logrus.Formatter, levels ...logrus.Level) *LocalHook { hook := &LocalHook{ lock: new(sync.Mutex), } - hook.SetFormatter(formatter) + hook.SetFormatter(consoleFormatter, fileFormatter) hook.levels = append(hook.levels, levels...) switch arg := args.(type) { @@ -174,3 +186,66 @@ func GetLogLevel(level string) []logrus.Level { } } } + +// LogFormat specialize for go-cqhttp +type LogFormat struct { + EnableColor bool +} + +// Format implements logrus.Formatter +func (f LogFormat) Format(entry *logrus.Entry) ([]byte, error) { + buf := NewBuffer() + defer PutBuffer(buf) + + if f.EnableColor { + 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") + + if f.EnableColor { + 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/main.go b/main.go index cb71b6c..49fe9f5 100644 --- a/main.go +++ b/main.go @@ -68,7 +68,10 @@ func main() { log.Errorf("rotatelogs init err: %v", err) panic(err) } - log.AddHook(global.NewLocalHook(w, global.LogFormat{}, global.GetLogLevel(base.LogLevel)...)) + + consoleFormatter := global.LogFormat{EnableColor: true} + fileFormatter := global.LogFormat{EnableColor: false} + log.AddHook(global.NewLocalHook(w, consoleFormatter, fileFormatter, global.GetLogLevel(base.LogLevel)...)) mkCacheDir := func(path string, _type string) { if !global.PathExists(path) {