44 lines
785 B
Go
44 lines
785 B
Go
|
package logger
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"io"
|
||
|
|
||
|
"gitlab.com/wpetit/goweb/logger"
|
||
|
)
|
||
|
|
||
|
type Writer struct {
|
||
|
ctx context.Context
|
||
|
level logger.Level
|
||
|
}
|
||
|
|
||
|
// Write implements io.Writer.
|
||
|
func (w *Writer) Write(p []byte) (n int, err error) {
|
||
|
w.log(string(p))
|
||
|
|
||
|
return len(p), nil
|
||
|
}
|
||
|
|
||
|
func (w *Writer) log(message string) {
|
||
|
switch w.level {
|
||
|
case logger.LevelDebug:
|
||
|
logger.Debug(w.ctx, message)
|
||
|
case logger.LevelInfo:
|
||
|
logger.Info(w.ctx, message)
|
||
|
case logger.LevelWarn:
|
||
|
logger.Warn(w.ctx, message)
|
||
|
case logger.LevelError:
|
||
|
logger.Error(w.ctx, message)
|
||
|
case logger.LevelCritical:
|
||
|
logger.Critical(w.ctx, message)
|
||
|
default:
|
||
|
logger.Debug(w.ctx, message)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewWriter(ctx context.Context, level logger.Level) *Writer {
|
||
|
return &Writer{ctx, level}
|
||
|
}
|
||
|
|
||
|
var _ io.Writer = &Writer{}
|