Skip to content

Commit

Permalink
Merge pull request #47 from moul/dev/moul/check-level
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jan 12, 2022
2 parents c247484 + 936a1af commit 60c37da
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func CheckAnyLevel(logger *zap.Logger) bool
CheckAnyLevel determines whether at least one log level isn't filtered-out
by the logger.
func CheckLevel(logger *zap.Logger, level zapcore.Level) bool
CheckLevel determines whether a specific log level would produce log or not.
func NewFilteringCore(next zapcore.Core, filter FilterFunc) zapcore.Core
NewFilteringCore returns a core middleware that uses the given filter
function to determine whether to actually call Write on the next core in the
Expand Down
5 changes: 5 additions & 0 deletions zapfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func CheckAnyLevel(logger *zap.Logger) bool {
return false
}

// CheckLevel determines whether a specific log level would produce log or not.
func CheckLevel(logger *zap.Logger, level zapcore.Level) bool {
return logger.Check(level, "") != nil
}

type filteringCore struct {
next zapcore.Core
filter FilterFunc
Expand Down
31 changes: 31 additions & 0 deletions zapfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,37 @@ func ExampleCheckAnyLevel() {
// true
}

func ExampleCheckLevel() {
c := zap.NewExample().Core()
logger := zap.New(zapfilter.NewFilteringCore(c, zapfilter.MustParseRules("debug:*.* info:demo*")))

fmt.Println(zapfilter.CheckLevel(logger, zap.DebugLevel))
fmt.Println(zapfilter.CheckLevel(logger.Named("demo"), zap.DebugLevel))
fmt.Println(zapfilter.CheckLevel(logger.Named("blahdemo"), zap.DebugLevel))
fmt.Println(zapfilter.CheckLevel(logger.Named("demoblah"), zap.DebugLevel))
fmt.Println(zapfilter.CheckLevel(logger.Named("blah"), zap.DebugLevel))
fmt.Println(zapfilter.CheckLevel(logger.Named("blah.blah"), zap.DebugLevel))
fmt.Println(zapfilter.CheckLevel(logger, zap.InfoLevel))
fmt.Println(zapfilter.CheckLevel(logger.Named("demo"), zap.InfoLevel))
fmt.Println(zapfilter.CheckLevel(logger.Named("blahdemo"), zap.InfoLevel))
fmt.Println(zapfilter.CheckLevel(logger.Named("demoblah"), zap.InfoLevel))
fmt.Println(zapfilter.CheckLevel(logger.Named("blah"), zap.InfoLevel))
fmt.Println(zapfilter.CheckLevel(logger.Named("blah.blah"), zap.InfoLevel))
// Output:
// false
// false
// false
// false
// false
// true
// false
// true
// false
// true
// false
// false
}

func Example_with() {
core := zap.NewExample().Core()
logger := zap.New(zapfilter.NewFilteringCore(core, zapfilter.ByNamespaces("demo1.*,demo3.*")))
Expand Down

0 comments on commit 60c37da

Please sign in to comment.