mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-04 19:17:37 +08:00
fix: default not use new lib, only enable when use build tag with_color
This commit is contained in:
parent
58a17c65a1
commit
69e5247bde
30
global/log_format.go
Normal file
30
global/log_format.go
Normal 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
|
||||||
|
}
|
69
global/log_format_with_color.go
Normal file
69
global/log_format_with_color.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user