fix for gin logging

This commit is contained in:
Pavel 2023-11-22 22:25:12 +03:00
parent 33290aa5c7
commit 95fd9f66d6

View File

@ -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
}