micro/logger/stdlogger.go
Vasiliy Tolstov ebd7eab627
Some checks failed
lint / lint (pull_request) Successful in 58s
pr / test (pull_request) Failing after 1m6s
fixup tests
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-10-15 23:55:16 +03:00

38 lines
903 B
Go

package logger
import (
"bytes"
"log"
)
type stdLogger struct {
l Logger
level Level
}
// NewStdLogger returns new *log.Logger baked by logger.Logger implementation
func NewStdLogger(l Logger, level Level) *log.Logger {
return log.New(&stdLogger{l: l.Clone(WithCallerSkipCount(l.Options().CallerSkipCount + 1)), level: level}, "" /* prefix */, 0 /* flags */)
}
func (sl *stdLogger) Write(p []byte) (int, error) {
p = bytes.TrimSpace(p)
sl.l.Log(sl.l.Options().Context, sl.level, string(p))
return len(p), nil
}
// RedirectStdLogger replace *log.Logger with logger.Logger implementation
func RedirectStdLogger(l Logger, level Level) func() {
flags := log.Flags()
prefix := log.Prefix()
writer := log.Writer()
log.SetFlags(0)
log.SetPrefix("")
log.SetOutput(&stdLogger{l: l, level: level})
return func() {
log.SetFlags(flags)
log.SetPrefix(prefix)
log.SetOutput(writer)
}
}