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.
44 lines
896 B
44 lines
896 B
3 years ago
|
package log
|
||
|
|
||
|
import "io"
|
||
|
|
||
|
type writerWrapper struct {
|
||
|
helper *Helper
|
||
|
level Level
|
||
|
}
|
||
|
|
||
|
type WriterOptionFn func(w *writerWrapper)
|
||
|
|
||
|
// WithWriteLevel 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) {
|
||
|
if key != "" {
|
||
|
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
|
||
|
}
|