From 95fd9f66d607853676f4fc9f48ba3526ee0bed69 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Wed, 22 Nov 2023 22:25:12 +0300 Subject: [PATCH] fix for gin logging --- internal/api/web/middleware/log/adapter.go | 59 +++++++++++++++++++--- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/internal/api/web/middleware/log/adapter.go b/internal/api/web/middleware/log/adapter.go index 31e6313..74cea14 100644 --- a/internal/api/web/middleware/log/adapter.go +++ b/internal/api/web/middleware/log/adapter.go @@ -2,6 +2,7 @@ package log import ( "io" + "strings" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -13,25 +14,67 @@ type writerLogger struct { } func AsWriter(log *zap.SugaredLogger, level zapcore.Level) io.Writer { - return &writerLogger{log: log} + return &writerLogger{log: log, level: level} } func (w *writerLogger) Write(p []byte) (n int, err error) { switch w.level { case zapcore.DebugLevel: - w.log.Debug(string(p)) + for _, line := range strings.Split(string(p), "\n") { + line = strings.TrimSpace(line) + if line == "" { + return + } + w.log.Debug(line) + } case zapcore.InfoLevel: - w.log.Info(string(p)) + for _, line := range strings.Split(string(p), "\n") { + line = strings.TrimSpace(line) + if line == "" { + return + } + w.log.Info(line) + } case zapcore.WarnLevel: - w.log.Warn(string(p)) + for _, line := range strings.Split(string(p), "\n") { + line = strings.TrimSpace(line) + if line == "" { + return + } + w.log.Warn(line) + } case zapcore.ErrorLevel: - w.log.Error(string(p)) + for _, line := range strings.Split(string(p), "\n") { + line = strings.TrimSpace(line) + if line == "" { + return + } + w.log.Error(line) + } case zapcore.DPanicLevel: - w.log.DPanic(string(p)) + for _, line := range strings.Split(string(p), "\n") { + line = strings.TrimSpace(line) + if line == "" { + return + } + w.log.DPanic(line) + } case zapcore.PanicLevel: - w.log.Panic(string(p)) + for _, line := range strings.Split(string(p), "\n") { + line = strings.TrimSpace(line) + if line == "" { + return + } + w.log.Panic(line) + } case zapcore.FatalLevel: - w.log.Fatal(string(p)) + for _, line := range strings.Split(string(p), "\n") { + line = strings.TrimSpace(line) + if line == "" { + return + } + w.log.Fatal(line) + } } return len(p), nil }