-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: handler - config add new field LevelMode, Level
- use for support create level value compare filter handler
- Loading branch information
Showing
4 changed files
with
283 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
## Change Log | ||
|
||
### Update | ||
|
||
- c17c0a4 up: update the benchmark tests package version to latest, add new tests log | ||
- 214491a :necktie: up: update logger caller skip value, fix test error | ||
- 4799e4b :necktie: up: update log write logic, add more unit tests | ||
- d671f33 :necktie: up: update logger handler, remove defaultH setting. | ||
|
||
### Other | ||
|
||
- 6f145bd pref: optimize the log write logic, add default handler | ||
- fb36553 style: re-format some code style by gofmt | ||
- 5a34789 build(deps): bump WillAbides/setup-go-faster from 1.7.0 to 1.8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package handler | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/gookit/slog" | ||
"github.com/gookit/slog/rotatefile" | ||
) | ||
|
||
// | ||
// --------------------------------------------------------------------------- | ||
// handler builder | ||
// --------------------------------------------------------------------------- | ||
// | ||
|
||
// Builder struct for create handler | ||
type Builder struct { | ||
*Config | ||
Output io.Writer | ||
} | ||
|
||
// NewBuilder create | ||
func NewBuilder() *Builder { | ||
return &Builder{ | ||
Config: NewEmptyConfig(), | ||
} | ||
} | ||
|
||
// WithOutput to the builder | ||
func (b *Builder) WithOutput(w io.Writer) *Builder { | ||
b.Output = w | ||
return b | ||
} | ||
|
||
// With some config fn | ||
// | ||
// Deprecated: please use WithConfigFn() | ||
func (b *Builder) With(fns ...ConfigFn) *Builder { | ||
return b.WithConfigFn(fns...) | ||
} | ||
|
||
// WithConfigFn some config fn | ||
func (b *Builder) WithConfigFn(fns ...ConfigFn) *Builder { | ||
b.Config.With(fns...) | ||
return b | ||
} | ||
|
||
// WithLogfile setting | ||
func (b *Builder) WithLogfile(logfile string) *Builder { | ||
b.Logfile = logfile | ||
return b | ||
} | ||
|
||
// WithLevelMode setting | ||
func (b *Builder) WithLevelMode(mode uint8) *Builder { | ||
b.LevelMode = mode | ||
return b | ||
} | ||
|
||
// WithLogLevel setting | ||
func (b *Builder) WithLogLevel(level slog.Level) *Builder { | ||
b.Level = level | ||
return b | ||
} | ||
|
||
// WithLogLevels setting | ||
func (b *Builder) WithLogLevels(levels []slog.Level) *Builder { | ||
b.Levels = levels | ||
return b | ||
} | ||
|
||
// WithBuffMode setting | ||
func (b *Builder) WithBuffMode(bufMode string) *Builder { | ||
b.BuffMode = bufMode | ||
return b | ||
} | ||
|
||
// WithBuffSize setting | ||
func (b *Builder) WithBuffSize(bufSize int) *Builder { | ||
b.BuffSize = bufSize | ||
return b | ||
} | ||
|
||
// WithMaxSize setting | ||
func (b *Builder) WithMaxSize(maxSize uint64) *Builder { | ||
b.MaxSize = maxSize | ||
return b | ||
} | ||
|
||
// WithRotateTime setting | ||
func (b *Builder) WithRotateTime(rt rotatefile.RotateTime) *Builder { | ||
b.RotateTime = rt | ||
return b | ||
} | ||
|
||
// WithCompress setting | ||
func (b *Builder) WithCompress(compress bool) *Builder { | ||
b.Compress = compress | ||
return b | ||
} | ||
|
||
// WithUseJSON setting | ||
func (b *Builder) WithUseJSON(useJSON bool) *Builder { | ||
b.UseJSON = useJSON | ||
return b | ||
} | ||
|
||
// Build slog handler. | ||
func (b *Builder) Build() slog.Handler { | ||
if b.Output != nil { | ||
return b.buildFromWriter(b.Output) | ||
} | ||
|
||
if b.Logfile != "" { | ||
w, err := b.CreateWriter() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return b.buildFromWriter(w) | ||
} | ||
|
||
panic("missing some information for build slog handler") | ||
} | ||
|
||
// Build slog handler. | ||
func (b *Builder) reset() { | ||
b.Output = nil | ||
b.Config = NewEmptyConfig() | ||
} | ||
|
||
// Build slog handler. | ||
func (b *Builder) buildFromWriter(w io.Writer) (h slog.Handler) { | ||
defer b.reset() | ||
bufSize := b.BuffSize | ||
lf := b.newLevelFormattable() | ||
|
||
if scw, ok := w.(SyncCloseWriter); ok { | ||
if bufSize > 0 { | ||
scw = b.wrapBuffer(scw) | ||
} | ||
|
||
h = &SyncCloseHandler{ | ||
Output: scw, | ||
// with log level and formatter | ||
LevelFormattable: lf, | ||
} | ||
} else if fcw, ok := w.(FlushCloseWriter); ok { | ||
if bufSize > 0 { | ||
fcw = b.wrapBuffer(fcw) | ||
} | ||
|
||
h = &FlushCloseHandler{ | ||
Output: fcw, | ||
// with log level and formatter | ||
LevelFormattable: lf, | ||
} | ||
} else if wc, ok := w.(io.WriteCloser); ok { | ||
if bufSize > 0 { | ||
wc = b.wrapBuffer(wc) | ||
} | ||
|
||
h = &WriteCloserHandler{ | ||
Output: wc, | ||
// with log level and formatter | ||
LevelFormattable: lf, | ||
} | ||
} else { | ||
if bufSize > 0 { | ||
w = b.wrapBuffer(w) | ||
} | ||
|
||
h = &IOWriterHandler{ | ||
Output: w, | ||
// with log level and formatter | ||
LevelFormattable: lf, | ||
} | ||
} | ||
|
||
// use json format. | ||
if b.UseJSON { | ||
type formatterSetter interface { | ||
SetFormatter(slog.Formatter) | ||
} | ||
|
||
// has setter | ||
if _, ok := h.(formatterSetter); ok { | ||
h.(formatterSetter).SetFormatter(slog.NewJSONFormatter()) | ||
} | ||
} | ||
return | ||
} |
Oops, something went wrong.