-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor handlers with level and formatter logic
- Loading branch information
Showing
22 changed files
with
357 additions
and
388 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
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,96 @@ | ||
package slog | ||
|
||
import "io" | ||
|
||
// FlushWriter is the interface satisfied by logging destinations. | ||
type FlushWriter interface { | ||
Flush() error | ||
// Writer the output writer | ||
io.Writer | ||
} | ||
|
||
// FlushCloseWriter is the interface satisfied by logging destinations. | ||
type FlushCloseWriter interface { | ||
Flush() error | ||
// WriteCloser the output writer | ||
io.WriteCloser | ||
} | ||
|
||
// FormatterWriterHandler interface | ||
type FormatterWriterHandler interface { | ||
Handler | ||
// Formatter record formatter | ||
Formatter() Formatter | ||
// Writer the output writer | ||
Writer() io.Writer | ||
} | ||
|
||
// | ||
// Handler interface | ||
// | ||
|
||
// Handler interface definition | ||
type Handler interface { | ||
// Closer Close handler. | ||
// You should first call Flush() on close logic. | ||
// Refer the FileHandler.Close() handle | ||
io.Closer | ||
// Flush and sync logs to disk file. | ||
Flush() error | ||
// IsHandling Checks whether the given record will be handled by this handler. | ||
IsHandling(level Level) bool | ||
// Handle a log record. | ||
// | ||
// All records may be passed to this method, and the handler should discard | ||
// those that it does not want to handle. | ||
Handle(*Record) error | ||
} | ||
|
||
/******************************************************************************** | ||
* Common parts for handler | ||
********************************************************************************/ | ||
|
||
// LevelWithFormatter struct definition | ||
// | ||
// - support set log formatter | ||
// - only support set one log level | ||
type LevelWithFormatter struct { | ||
Formattable | ||
// Level for log message. if current level <= Level will log message | ||
Level Level | ||
} | ||
|
||
// NewLvFormatter create new instance | ||
func NewLvFormatter(lv Level) *LevelWithFormatter { | ||
return &LevelWithFormatter{Level: lv} | ||
} | ||
|
||
// IsHandling Check if the current level can be handling | ||
func (h *LevelWithFormatter) IsHandling(level Level) bool { | ||
return h.Level.ShouldHandling(level) | ||
} | ||
|
||
// LevelsWithFormatter struct definition | ||
// | ||
// - support set log formatter | ||
// - support setting multi log levels | ||
type LevelsWithFormatter struct { | ||
Formattable | ||
// Levels for log message | ||
Levels []Level | ||
} | ||
|
||
// NewLvsFormatter create new instance | ||
func NewLvsFormatter(levels []Level) *LevelsWithFormatter { | ||
return &LevelsWithFormatter{Levels: levels} | ||
} | ||
|
||
// IsHandling Check if the current level can be handling | ||
func (h *LevelsWithFormatter) IsHandling(level Level) bool { | ||
for _, l := range h.Levels { | ||
if l == level { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file was deleted.
Oops, something went wrong.
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,74 @@ | ||
package handler | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/gookit/slog" | ||
"github.com/gookit/slog/bufwrite" | ||
) | ||
|
||
// NewBuffered create new BufferedHandler | ||
func NewBuffered(cWriter io.WriteCloser, bufSize int, levels ...slog.Level) *FlushCloseHandler { | ||
return NewBufferedHandler(cWriter, bufSize, levels...) | ||
} | ||
|
||
// NewBufferedHandler create new BufferedHandler | ||
func NewBufferedHandler(cWriter io.WriteCloser, bufSize int, levels ...slog.Level) *FlushCloseHandler { | ||
if len(levels) == 0 { | ||
levels = slog.AllLevels | ||
} | ||
|
||
return &FlushCloseHandler{ | ||
Output: bufwrite.NewBufIOWriterSize(cWriter, bufSize), | ||
// log levels | ||
LevelFormattable: slog.NewLvsFormatter(levels), | ||
} | ||
} | ||
|
||
// LineBufferedFile handler | ||
func LineBufferedFile(logfile string, bufSize int, levels []slog.Level) (slog.Handler, error) { | ||
cfg := NewConfig( | ||
WithLogfile(logfile), | ||
WithBuffSize(bufSize), | ||
WithLogLevels(levels), | ||
WithBuffMode(BuffModeLine), | ||
) | ||
|
||
output, err := cfg.SyncCloseWriter() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &SyncCloseHandler{ | ||
Output: output, | ||
// init log levels | ||
LevelFormattable: slog.NewLvsFormatter(cfg.Levels), | ||
}, nil | ||
} | ||
|
||
// LineBuffOsFile handler | ||
func LineBuffOsFile(f *os.File, bufSize int, levels []slog.Level) slog.Handler { | ||
if f == nil { | ||
panic("slog: the os file cannot be nil") | ||
} | ||
|
||
return &SyncCloseHandler{ | ||
Output: bufwrite.NewLineWriterSize(f, bufSize), | ||
// init log levels | ||
LevelFormattable: slog.NewLvsFormatter(levels), | ||
} | ||
} | ||
|
||
// LineBuffWriter handler | ||
func LineBuffWriter(w io.Writer, bufSize int, levels []slog.Level) slog.Handler { | ||
if w == nil { | ||
panic("slog: the io writer cannot be nil") | ||
} | ||
|
||
return &IOWriterHandler{ | ||
Output: bufwrite.NewLineWriterSize(w, bufSize), | ||
// init log levels | ||
LevelFormattable: slog.NewLvsFormatter(levels), | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.