first comm

This commit is contained in:
远野千束 2024-10-01 06:19:45 +08:00
parent 41f5377d1c
commit 00415def7a
5 changed files with 228 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
# idea
.idea/
.vscode/

View File

@ -1,2 +1,9 @@
# go-logger
Liteyuki Logger for Go, 用于轻雪项目统一日志格式
基于loguru封装
## 安装
```shell
go get -u github.com/liteyukistudio/go-logger
```

7
go.mod Normal file
View File

@ -0,0 +1,7 @@
module github.com/LiteyukiStudio/go-logger
go 1.22.6
require github.com/sirupsen/logrus v1.9.3
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect

15
go.sum Normal file
View File

@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

195
log/init.go Normal file
View File

@ -0,0 +1,195 @@
package log
import (
"bytes"
"fmt"
"github.com/sirupsen/logrus"
"time"
)
// init Logger
func init() {
Logger = logrus.New()
Logger.Infoln()
Logger.SetFormatter(&LiteyukiFormatter{})
}
const (
TraceLevel = logrus.TraceLevel
DebugLevel = logrus.DebugLevel
InfoLevel = logrus.InfoLevel
WarnLevel = logrus.WarnLevel
ErrorLevel = logrus.ErrorLevel
FatalLevel = logrus.FatalLevel
PanicLevel = logrus.PanicLevel
)
const (
Reset = "\033[0m"
Black = "\033[30m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Magenta = "\033[35m"
Cyan = "\033[36m"
White = "\033[37m"
BlackBold = "\033[30;1m"
RedBold = "\033[31;1m"
GreenBold = "\033[32;1m"
YellowBold = "\033[33;1m"
BlueBold = "\033[34;1m"
MagentaBold = "\033[35;1m"
CyanBold = "\033[36;1m"
WhiteBold = "\033[37;1m"
)
var Logger *logrus.Logger
// LiteyukiFormatter 标准输出格式化
type LiteyukiFormatter struct{}
// Format 标准输出格式
func (f *LiteyukiFormatter) Format(entry *logrus.Entry) ([]byte, error) {
var b bytes.Buffer
timestamp := entry.Time.Format("2006-01-02 15:04:05")
// 时间为绿色
timeText := Color(Green, timestamp)
logLevel := entry.Level.String()
message := entry.Message
// 定义颜色
var color string
switch entry.Level {
case logrus.TraceLevel:
color = Black
case logrus.DebugLevel:
color = Cyan
case logrus.InfoLevel:
color = Blue
case logrus.WarnLevel:
color = Yellow
case logrus.ErrorLevel:
color = Red
case logrus.FatalLevel:
color = RedBold
case logrus.PanicLevel:
color = RedBold
default:
color = "\033[0m"
}
levelText := Color(color, "["+logLevel+"]")
// 格式化日志信息并添加颜色
b.WriteString(fmt.Sprintf("%s %s %s\n", timeText, levelText, message))
return b.Bytes(), nil
}
func Trace(args ...interface{}) {
Logger.Trace(args)
}
func Traceln(args ...interface{}) {
Logger.Traceln(args)
}
func Tracef(format string, args ...interface{}) {
Logger.Tracef(format, args)
}
func Debug(args ...interface{}) {
Logger.Debug(args)
}
func Debugln(args ...interface{}) {
Logger.Debugln(args)
}
func Debugf(format string, args ...interface{}) {
Logger.Debugf(format, args)
}
func Info(args ...interface{}) {
Logger.Info(args)
}
func Infoln(args ...interface{}) {
Logger.Infoln(args)
}
func Infof(format string, args ...interface{}) {
Logger.Infof(format, args)
}
func Warn(args ...interface{}) {
Logger.Warn(args)
}
func Warnln(args ...interface{}) {
Logger.Warnln(args)
}
func Warnf(format string, args ...interface{}) {
Logger.Warnf(format, args)
}
func Error(args ...interface{}) {
Logger.Error(args)
}
func Errorln(args ...interface{}) {
Logger.Errorln(args)
}
func Errorf(format string, args ...interface{}) {
Logger.Errorf(format, args)
}
func Fatal(args ...interface{}) {
Logger.Fatal(args)
}
func Fatalln(args ...interface{}) {
Logger.Fatalln(args)
}
func Fatalf(format string, args ...interface{}) {
Logger.Fatalf(format, args)
}
func Panic(args ...interface{}) {
Logger.Panic(args)
}
func Panicln(args ...interface{}) {
Logger.Panicln(args)
}
func Panicf(format string, args ...interface{}) {
Logger.Panicf(format, args)
}
// Color 字符串添加颜色
func Color(color, text string) string {
return fmt.Sprintf("%s%s%s", color, text, Reset)
}
// SetLevel 设置日志级别
func SetLevel(level logrus.Level) {
Logger.SetLevel(level)
}
// SetFormatter 设置日志格式
func SetFormatter(formatter logrus.Formatter) {
Logger.SetFormatter(formatter)
}
// SetOutput 设置日志输出
func SetOutput(out *bytes.Buffer) {
Logger.SetOutput(out)
}
// getCurrentDate 获取当前日期, 返回格式为yyyy-mm-dd
func getCurrentDate() string {
return time.Now().Format("2006-01-02")
}