From 98976bba135a4d708a58361f99b3086f5eb6da2b Mon Sep 17 00:00:00 2001 From: jinnrry Date: Mon, 4 Mar 2024 14:14:24 +0800 Subject: [PATCH] fix --- server/hooks/framework/framework.go | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/server/hooks/framework/framework.go b/server/hooks/framework/framework.go index 3fe3b36..044b091 100644 --- a/server/hooks/framework/framework.go +++ b/server/hooks/framework/framework.go @@ -1,7 +1,9 @@ package framework import ( + "bytes" "encoding/json" + "fmt" log "github.com/sirupsen/logrus" "io" "net" @@ -45,7 +47,38 @@ func CreatePlugin(name string, hook EmailHook) *Plugin { } } +type logFormatter struct { +} + +// Format 定义日志输出格式 +func (l *logFormatter) Format(entry *log.Entry) ([]byte, error) { + b := bytes.Buffer{} + + b.WriteString(fmt.Sprintf("[%s]", entry.Level.String())) + b.WriteString(fmt.Sprintf("[%s]", entry.Time.Format("2006-01-02 15:04:05"))) + if entry.Context != nil { + ctx := entry.Context.(*context.Context) + if ctx != nil { + b.WriteString(fmt.Sprintf("[%s]", ctx.GetValue(context.LogID))) + } + } + b.WriteString(fmt.Sprintf("[%s:%d]", entry.Caller.File, entry.Caller.Line)) + b.WriteString(entry.Message) + + b.WriteString("\n") + return b.Bytes(), nil +} + func (p *Plugin) Run() { + + // 设置日志格式为json格式 + log.SetFormatter(&logFormatter{}) + log.SetReportCaller(true) + + // 设置将日志输出到标准输出(默认的输出为stderr,标准错误) + // 日志消息输出可以是任意的io.writer类型 + log.SetOutput(os.Stdout) + if len(os.Args) < 2 { panic("Command Params Error!") }