2023-11-22 22:21:11 +03:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2023-11-22 22:25:12 +03:00
|
|
|
"strings"
|
2023-11-22 22:21:11 +03:00
|
|
|
|
|
|
|
"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}
|
2023-11-22 22:21:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2023-11-22 22:21:11 +03:00
|
|
|
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)
|
|
|
|
}
|
2023-11-22 22:21:11 +03:00
|
|
|
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)
|
|
|
|
}
|
2023-11-22 22:21:11 +03:00
|
|
|
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)
|
|
|
|
}
|
2023-11-22 22:21:11 +03:00
|
|
|
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)
|
|
|
|
}
|
2023-11-22 22:21:11 +03:00
|
|
|
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)
|
|
|
|
}
|
2023-11-22 22:21:11 +03:00
|
|
|
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)
|
|
|
|
}
|
2023-11-22 22:21:11 +03:00
|
|
|
}
|
|
|
|
return len(p), nil
|
|
|
|
}
|