From 2a380347c681a4fe6f9a41675f393b00a8844454 Mon Sep 17 00:00:00 2001 From: thesai <1021828630@qq.com> Date: Sun, 16 Jun 2024 12:26:48 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BF=AE=E6=94=B9]=E6=97=A5=E5=BF=97=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=A0=86=E6=A0=88=E4=BF=A1=E6=81=AF=E6=97=A0=E6=8D=A2?= =?UTF-8?q?=E8=A1=8C=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- logger/logger.go | 12 +++++-- logger/my_json_handler.go | 63 ++++++++++++++++++++++++++++++++++ logger/my_json_handler_test.go | 21 ++++++++++++ starter/init.go | 6 +--- 5 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 logger/my_json_handler.go create mode 100644 logger/my_json_handler_test.go diff --git a/.gitignore b/.gitignore index b184d22..55be726 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,5 @@ output/ # Go workspace file /go.work.sum -/pom.xml \ No newline at end of file +/pom.xml +/bj-rtsts-server diff --git a/logger/logger.go b/logger/logger.go index 154e6ea..4d0d0c3 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -36,7 +36,11 @@ func InitSlog() { if logging.Stdout { // 日志输出到控制台 - slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ + //slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ + // Level: level, + // AddSource: false, + //}))) + slog.SetDefault(slog.New(newMyJsonHandler(os.Stdout, &slog.HandlerOptions{ Level: level, AddSource: false, }))) @@ -50,7 +54,11 @@ func InitSlog() { LocalTime: true, // 日志备份使用本地时间 Compress: logging.Compress, // 是否压缩日志 } - slog.SetDefault(slog.New(slog.NewJSONHandler(lumberJackLogger, &slog.HandlerOptions{ + //slog.SetDefault(slog.New(slog.NewJSONHandler(lumberJackLogger, &slog.HandlerOptions{ + // Level: level, + // AddSource: false, + //}))) + slog.SetDefault(slog.New(newMyJsonHandler(lumberJackLogger, &slog.HandlerOptions{ Level: level, AddSource: false, }))) diff --git a/logger/my_json_handler.go b/logger/my_json_handler.go new file mode 100644 index 0000000..0039f19 --- /dev/null +++ b/logger/my_json_handler.go @@ -0,0 +1,63 @@ +package logger + +import ( + "context" + "io" + "log/slog" + "sync" +) + +type MyJsonHandler struct { + output io.Writer + mu sync.Mutex + jsonHandler slog.Handler +} + +var stackChan = make(chan string, 100) + +func newMyJsonHandler(output io.Writer, opts *slog.HandlerOptions) *MyJsonHandler { + if opts == nil { + opts = &slog.HandlerOptions{} + } + ra := opts.ReplaceAttr + opts.ReplaceAttr = func(groups []string, a slog.Attr) slog.Attr { + if a.Key == "stack" { + stackChan <- a.Value.String() + return slog.Attr{} + } else { + if ra != nil { + return ra(groups, a) + } else { + return a + } + } + } + return &MyJsonHandler{output: output, mu: sync.Mutex{}, jsonHandler: slog.NewJSONHandler(output, opts)} +} + +func (s *MyJsonHandler) Enabled(ctx context.Context, level slog.Level) bool { + return s.jsonHandler.Enabled(ctx, level) +} + +func (s *MyJsonHandler) Handle(ctx context.Context, record slog.Record) error { + err := s.jsonHandler.Handle(ctx, record) + if err != nil { + return err + } + select { + case stack := <-stackChan: + s.mu.Lock() + defer s.mu.Unlock() + _, err = s.output.Write([]byte(stack)) + default: + } + return err +} + +func (s *MyJsonHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + return s.jsonHandler.WithAttrs(attrs) +} + +func (s *MyJsonHandler) WithGroup(name string) slog.Handler { + return s.jsonHandler.WithGroup(name) +} diff --git a/logger/my_json_handler_test.go b/logger/my_json_handler_test.go new file mode 100644 index 0000000..7caef4d --- /dev/null +++ b/logger/my_json_handler_test.go @@ -0,0 +1,21 @@ +package logger + +import ( + "log/slog" + "os" + "testing" +) + +func BenchmarkMyJsonHandler_Handle(b *testing.B) { + for i := 0; i < b.N; i++ { + //slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ + // AddSource: false, + // Level: nil, + //}))) + slog.SetDefault(slog.New(newMyJsonHandler(os.Stdout, &slog.HandlerOptions{ + AddSource: false, + Level: nil, + }))) + slog.Info("这是一条日志", "error", "错误", "stack", "goroutine 10 [running]:\nruntime/debug.Stack()\n\tD:/develop/Go/go1.21.4/src/runtime/debug/stack.go:24 +0x6b\njoylink.club/bj-rtsts-server/starter.customRecoveryWithSlog.func1.1()\n\tD:/GoProject/rts-sim-testing-service/starter/init.go:126 +0x36f\npanic({0x1f0cb40?, 0x226fd50?})\n\tD:/develop/Go/go1.21.4/src/runtime/panic.go:920 +0x290\njoylink.club/bj-rtsts-server/api.createByProjectId(0xc001a06100)\n\tD:/GoProject/rts-sim-testing-service/api/simulation.go:91 +0x26\ngithub.com/gin-gonic/gin.(*Context).Next(0xc001a06100)\n\tC:/Users/thesai/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 +0x7a\njoylink.club/bj-rtsts-server/middleware.permissionMiddleware.func1(0xc001a06100)\n\tD:/GoProject/rts-sim-testing-service/middleware/auth.go:35 +0x178\ngithub.com/gin-gonic/gin.(*Context).Next(0xc001a06100)\n\tC:/Users/thesai/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 +0x7a\ngithub.com/appleboy/gin-jwt/v2.(*GinJWTMiddleware).middlewareImpl(0xc000504340, 0xc001a06100)\n\tC:/Users/thesai/go/pkg/mod/github.com/appleboy/gin-jwt/v2@v2.9.1/auth_jwt.go:462 +0x579\ngithub.com/appleboy/gin-jwt/v2.(*GinJWTMiddleware).MiddlewareFunc.func1(0xc001a06100)\n\tC:/Users/thesai/go/pkg/mod/github.com/appleboy/gin-jwt/v2@v2.9.1/auth_jwt.go:415 +0x26\ngithub.com/gin-gonic/gin.(*Context).Next(0xc001a06100)\n\tC:/Users/thesai/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 +0x7a\njoylink.club/bj-rtsts-server/starter.customRecoveryWithSlog.func1(0xc001a06100)\n\tD:/GoProject/rts-sim-testing-service/starter/init.go:135 +0xef\ngithub.com/gin-gonic/gin.(*Context).Next(0xc001a06100)\n\tC:/Users/thesai/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 +0x7a\ngithub.com/samber/slog-gin.NewWithConfig.func1(0xc001a06100)\n\tC:/Users/thesai/go/pkg/mod/github.com/samber/slog-gin@v1.1.0/middleware.go:70 +0x13f\ngithub.com/gin-gonic/gin.(*Context).Next(0xc001a06100)\n\tC:/Users/thesai/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 +0x7a\ngithub.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc0004d51e0, 0xc001a06100)\n\tC:/Users/thesai/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:620 +0x427\ngithub.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc0004d51e0, {0x227e480, 0xc00012aee0}, 0xc001a07100)\n\tC:/Users/thesai/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:576 +0xbc\nnet/http.serverHandler.ServeHTTP({0xc0017f0960}, {0x227e480, 0xc00012aee0}, 0xc001a07100)\n\tD:/develop/Go/go1.21.4/src/net/http/server.go:2938 +0x257\nnet/http.(*conn).serve(0xc0019422d0, {0x2285b38, 0xc001982000})\n\tD:/develop/Go/go1.21.4/src/net/http/server.go:2009 +0x1a39\ncreated by net/http.(*Server).Serve in goroutine 1\n\tD:/develop/Go/go1.21.4/src/net/http/server.go:3086 +0xa25") + } +} diff --git a/starter/init.go b/starter/init.go index e7d13e9..49e3ef2 100644 --- a/starter/init.go +++ b/starter/init.go @@ -122,11 +122,7 @@ func customRecoveryWithSlog(logger *slog.Logger, stack bool, recovery gin.Recove } if stack { - logger.Error("请求处理Panic异常", - "error", err, - "stack", string(debug.Stack()), - ) - debug.PrintStack() + logger.Error("请求处理Panic异常", "error", err, "stack", string(debug.Stack())) } else { logger.Error("请求处理Panic异常", "error", err,