1
0
mirror of https://github.com/go-kratos/kratos.git synced 2024-12-28 21:09:04 +02:00
kratos/log/helper_writer.go
jesse.tang 9737a3c5e4
fix: typo and grammatical errors (#2368)
* fix: grammar

* fix: grammar

* fix overrode

* translate to English

* translate to English
2022-09-19 10:39:54 +08:00

42 lines
875 B
Go

package log
import "io"
type writerWrapper struct {
helper *Helper
level Level
}
type WriterOptionFn func(w *writerWrapper)
// WithWriterLevel set writerWrapper level.
func WithWriterLevel(level Level) WriterOptionFn {
return func(w *writerWrapper) {
w.level = level
}
}
// WithWriteMessageKey set writerWrapper helper message key.
func WithWriteMessageKey(key string) WriterOptionFn {
return func(w *writerWrapper) {
w.helper.msgKey = key
}
}
// NewWriter return a writer wrapper.
func NewWriter(logger Logger, opts ...WriterOptionFn) io.Writer {
ww := &writerWrapper{
helper: NewHelper(logger, WithMessageKey(DefaultMessageKey)),
level: LevelInfo, // default level
}
for _, opt := range opts {
opt(ww)
}
return ww
}
func (ww *writerWrapper) Write(p []byte) (int, error) {
ww.helper.Log(ww.level, ww.helper.msgKey, string(p))
return 0, nil
}