1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +08:00

feat: console and file log use different formatter, and remove build tag

This commit is contained in:
风之凌殇 2021-10-05 01:07:36 +08:00
parent 69e5247bde
commit f784e94a4a
4 changed files with 84 additions and 105 deletions

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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) {