Skip to content

Commit

Permalink
Added fields.IgnoreLevels based on fields.Filtered.
Browse files Browse the repository at this point in the history
  • Loading branch information
blaubaer committed Jan 3, 2023
1 parent 6d951b2 commit c4dafbf
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
31 changes: 31 additions & 0 deletions fields/filtered.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,34 @@ func (instance requireMaximalLevel) Filter(ctx FilterContext) (value interface{}

return instance.Get(), true
}

// IgnoreLevels represents a filtered value which will only be consumed if the
// level.Level of the current context (for example logging events) is smaller than
// fromLevel or equal/bigger than toLevel (fromLevel:inclusive, toLevel:exclusive).
func IgnoreLevels(fromLevel, toLevel level.Level, value interface{}) Filtered {
return IgnoreLevelsLazy(fromLevel, toLevel, LazyFunc(func() interface{} {
return value
}))
}

// IgnoreLevelsLazy represents a filtered Lazy value which will only be consumed
// if the level.Level of the current context (for example logging events) is smaller
// than fromLevel or equal/bigger than toLevel (fromLevel:inclusive, toLevel:exclusive).
func IgnoreLevelsLazy(fromLevel, toLevel level.Level, value Lazy) Filtered {
return ignoreLevels{value, fromLevel, toLevel}
}

type ignoreLevels struct {
Lazy
fromLevel level.Level
toLevel level.Level
}

func (instance ignoreLevels) Filter(ctx FilterContext) (value interface{}, shouldBeRespected bool) {
lvl := ctx.GetLevel()
if lvl >= instance.fromLevel && lvl < instance.toLevel {
return nil, false
}

return instance.Get(), true
}
70 changes: 70 additions & 0 deletions fields/filtered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,76 @@ func Test_RequireMaximalLevel_Filter_ignored(t *testing.T) {
assert.ToBeEqual(t, false, actualRespected)
}

func Test_IgnoreLevelsLazy_Get(t *testing.T) {
expected := struct{ foo string }{foo: "bar"}
givenLazy := LazyFunc(func() interface{} { return expected })

actualInstance := IgnoreLevelsLazy(level.Info, level.Warn, givenLazy)
actual := actualInstance.Get()

assert.ToBeEqual(t, expected, actual)
}

func Test_IgnoreLevelsLazy_Filter_respectedBelow(t *testing.T) {
expected := struct{ foo string }{foo: "bar"}
givenLazy := LazyFunc(func() interface{} { return expected })

actualInstance := IgnoreLevelsLazy(level.Info, level.Warn, givenLazy)
actual, actualRespected := actualInstance.Filter(filterContext{level: level.Info - 1})

assert.ToBeEqual(t, expected, actual)
assert.ToBeEqual(t, true, actualRespected)
}

func Test_IgnoreLevelsLazy_Filter_respectedAbove(t *testing.T) {
expected := struct{ foo string }{foo: "bar"}
givenLazy := LazyFunc(func() interface{} { return expected })

actualInstance := IgnoreLevelsLazy(level.Info, level.Warn, givenLazy)
actual, actualRespected := actualInstance.Filter(filterContext{level: level.Warn})

assert.ToBeEqual(t, expected, actual)
assert.ToBeEqual(t, true, actualRespected)
}

func Test_IgnoreLevelsLazy_Filter_ignored(t *testing.T) {
givenLazy := LazyFunc(func() interface{} { return struct{ foo string }{foo: "bar"} })

actualInstance := IgnoreLevelsLazy(level.Info, level.Warn, givenLazy)
actual, actualRespected := actualInstance.Filter(filterContextWithLeveInfo)

assert.ToBeNil(t, actual)
assert.ToBeEqual(t, false, actualRespected)
}

func Test_IgnoreLevels_Filter_respectedBelow(t *testing.T) {
expected := struct{ foo string }{foo: "bar"}

actualInstance := IgnoreLevels(level.Info, level.Warn, expected)
actual, actualRespected := actualInstance.Filter(filterContext{level: level.Info - 1})

assert.ToBeEqual(t, expected, actual)
assert.ToBeEqual(t, true, actualRespected)
}

func Test_IgnoreLevels_Filter_respectedAbove(t *testing.T) {
expected := struct{ foo string }{foo: "bar"}

actualInstance := IgnoreLevels(level.Info, level.Warn, expected)
actual, actualRespected := actualInstance.Filter(filterContext{level: level.Warn})

assert.ToBeEqual(t, expected, actual)
assert.ToBeEqual(t, true, actualRespected)
}

func Test_IgnoreLevels_Filter_ignored(t *testing.T) {
actualInstance := IgnoreLevels(level.Info, level.Warn, struct{ foo string }{foo: "bar"})
actual, actualRespected := actualInstance.Filter(filterContextWithLeveInfo)

assert.ToBeNil(t, actual)
assert.ToBeEqual(t, false, actualRespected)
}

type filterContext struct {
level level.Level
fields map[string]interface{}
Expand Down

0 comments on commit c4dafbf

Please sign in to comment.