-
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 - refactoring some handler create logic, support use …
…max level or levels
- Loading branch information
Showing
16 changed files
with
390 additions
and
292 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
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
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,19 @@ | ||
package handler_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gookit/goutil/testutil/assert" | ||
"github.com/gookit/slog" | ||
"github.com/gookit/slog/handler" | ||
) | ||
|
||
func TestConsoleWithMaxLevel(t *testing.T) { | ||
l := slog.NewWithHandlers(handler.ConsoleWithMaxLevel(slog.InfoLevel)) | ||
l.DoNothingOnPanicFatal() | ||
|
||
for _, level := range slog.AllLevels { | ||
l.Log(level, "a test message") | ||
} | ||
assert.NoErr(t, l.LastErr()) | ||
} |
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 |
---|---|---|
@@ -1,76 +1,61 @@ | ||
package handler | ||
|
||
import ( | ||
"github.com/gookit/goutil/basefn" | ||
"github.com/gookit/slog" | ||
) | ||
|
||
// MustFileHandler create file handler | ||
func MustFileHandler(logfile string, fns ...ConfigFn) *SyncCloseHandler { | ||
h, err := NewFileHandler(logfile, fns...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return h | ||
} | ||
|
||
// JSONFileHandler create new FileHandler with JSON formatter | ||
func JSONFileHandler(logfile string, fns ...ConfigFn) (*SyncCloseHandler, error) { | ||
fns = append(fns, WithUseJSON(true)) | ||
|
||
return NewFileHandler(logfile, fns...) | ||
return NewFileHandler(logfile, append(fns, WithUseJSON(true))...) | ||
} | ||
|
||
// NewBuffFileHandler create file handler with buff size | ||
func NewBuffFileHandler(logfile string, buffSize int, fns ...ConfigFn) (*SyncCloseHandler, error) { | ||
fns = append(fns, WithBuffSize(buffSize)) | ||
return NewFileHandler(logfile, append(fns, WithBuffSize(buffSize))...) | ||
} | ||
|
||
return NewFileHandler(logfile, fns...) | ||
// MustFileHandler create file handler | ||
func MustFileHandler(logfile string, fns ...ConfigFn) *SyncCloseHandler { | ||
return basefn.Must(NewFileHandler(logfile, fns...)) | ||
} | ||
|
||
// NewFileHandler create new FileHandler | ||
func NewFileHandler(logfile string, fns ...ConfigFn) (h *SyncCloseHandler, err error) { | ||
cfg := NewEmptyConfig(fns...).With(WithLogfile(logfile)) | ||
|
||
return cfg.CreateHandler() | ||
return NewEmptyConfig(fns...).With(WithLogfile(logfile)).CreateHandler() | ||
} | ||
|
||
// MustSimpleFile new instance | ||
func MustSimpleFile(filepath string) *SyncCloseHandler { | ||
h, err := NewSimpleFileHandler(filepath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// | ||
// ------------- simple file handler ------------- | ||
// | ||
|
||
return h | ||
// MustSimpleFile new instance | ||
func MustSimpleFile(filepath string, maxLv ...slog.Level) *SyncCloseHandler { | ||
return basefn.Must(NewSimpleFileHandler(filepath, maxLv...)) | ||
} | ||
|
||
// NewSimpleFile new instance | ||
func NewSimpleFile(filepath string) (*SyncCloseHandler, error) { | ||
return NewSimpleFileHandler(filepath) | ||
func NewSimpleFile(filepath string, maxLv ...slog.Level) (*SyncCloseHandler, error) { | ||
return NewSimpleFileHandler(filepath, maxLv...) | ||
} | ||
|
||
// NewSimpleFileHandler instance | ||
// NewSimpleFileHandler instance, default log level is InfoLevel | ||
// | ||
// Usage: | ||
// | ||
// h, err := NewSimpleFileHandler("/tmp/error.log") | ||
// | ||
// custom formatter | ||
// Custom formatter: | ||
// | ||
// h.SetFormatter(slog.NewJSONFormatter()) | ||
// slog.PushHandler(h) | ||
// slog.Info("log message") | ||
func NewSimpleFileHandler(filePath string) (*SyncCloseHandler, error) { | ||
func NewSimpleFileHandler(filePath string, maxLv ...slog.Level) (*SyncCloseHandler, error) { | ||
file, err := QuickOpenFile(filePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
h := &SyncCloseHandler{ | ||
Output: file, | ||
// init default log level | ||
LevelFormattable: slog.NewLvFormatter(slog.InfoLevel), | ||
} | ||
|
||
h := SyncCloserWithMaxLevel(file, basefn.FirstOr(maxLv, slog.InfoLevel)) | ||
return h, nil | ||
} |
Oops, something went wrong.