You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
875 B
41 lines
875 B
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
|
|
}
|
|
|