sshpoke/internal/api/rest/middleware/log/adapter.go

81 lines
1.6 KiB
Go
Raw Normal View History

package log
import (
"io"
2023-11-22 22:25:12 +03:00
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type writerLogger struct {
log *zap.SugaredLogger
level zapcore.Level
}
func AsWriter(log *zap.SugaredLogger, level zapcore.Level) io.Writer {
2023-11-22 22:25:12 +03:00
return &writerLogger{log: log, level: level}
}
func (w *writerLogger) Write(p []byte) (n int, err error) {
switch w.level {
case zapcore.DebugLevel:
2023-11-22 22:25:12 +03:00
for _, line := range strings.Split(string(p), "\n") {
line = strings.TrimSpace(line)
if line == "" {
return
}
w.log.Debug(line)
}
case zapcore.InfoLevel:
2023-11-22 22:25:12 +03:00
for _, line := range strings.Split(string(p), "\n") {
line = strings.TrimSpace(line)
if line == "" {
return
}
w.log.Info(line)
}
case zapcore.WarnLevel:
2023-11-22 22:25:12 +03:00
for _, line := range strings.Split(string(p), "\n") {
line = strings.TrimSpace(line)
if line == "" {
return
}
w.log.Warn(line)
}
case zapcore.ErrorLevel:
2023-11-22 22:25:12 +03:00
for _, line := range strings.Split(string(p), "\n") {
line = strings.TrimSpace(line)
if line == "" {
return
}
w.log.Error(line)
}
case zapcore.DPanicLevel:
2023-11-22 22:25:12 +03:00
for _, line := range strings.Split(string(p), "\n") {
line = strings.TrimSpace(line)
if line == "" {
return
}
w.log.DPanic(line)
}
case zapcore.PanicLevel:
2023-11-22 22:25:12 +03:00
for _, line := range strings.Split(string(p), "\n") {
line = strings.TrimSpace(line)
if line == "" {
return
}
w.log.Panic(line)
}
case zapcore.FatalLevel:
2023-11-22 22:25:12 +03:00
for _, line := range strings.Split(string(p), "\n") {
line = strings.TrimSpace(line)
if line == "" {
return
}
w.log.Fatal(line)
}
}
return len(p), nil
}