-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
fields.RequireMaximalLevel
based on fields.Filtered
.
- Loading branch information
Showing
16 changed files
with
277 additions
and
40 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 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
File renamed without changes.
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,58 @@ | ||
package fields | ||
|
||
import "github.com/echocat/slf4g/level" | ||
|
||
// Filtered is a value which will be executed on usage to retrieve the actual | ||
// value or exclude it. | ||
// | ||
// This is useful in context where fields should be only respected based on a | ||
// specific log level, another field has a specific value, ... | ||
type Filtered interface { | ||
// Filter is the method which will be called at the moment where the value | ||
// should be consumed. | ||
// | ||
// Only if shouldBeRespected is true it will be respected by the consumers. | ||
Filter(FilterContext) (value interface{}, shouldBeRespected bool) | ||
|
||
// Get will return the original value (unfiltered). | ||
Get() interface{} | ||
} | ||
|
||
// FilterContext provides information about the context where a field exists | ||
// within. | ||
type FilterContext interface { | ||
// GetLevel provides the current level.Level of this context. | ||
GetLevel() level.Level | ||
|
||
// Get provides access to other fields within this context. | ||
Get(key string) (value interface{}, exists bool) | ||
} | ||
|
||
// RequireMaximalLevel represents a filtered value which will only be consumed if the | ||
// level.Level of the current context (for example logging events) is not bigger than | ||
// the requested maximalLevel. | ||
func RequireMaximalLevel(maximalLevel level.Level, value interface{}) Filtered { | ||
return RequireMaximalLevelLazy(maximalLevel, LazyFunc(func() interface{} { | ||
return value | ||
})) | ||
} | ||
|
||
// RequireMaximalLevelLazy represents a filtered Lazy value which will only be consumed | ||
// if the level.Level of the current context (for example logging events) is not bigger | ||
// than requested maximalLevel. | ||
func RequireMaximalLevelLazy(minimalLevel level.Level, value Lazy) Filtered { | ||
return requireMaximalLevel{value, minimalLevel} | ||
} | ||
|
||
type requireMaximalLevel struct { | ||
Lazy | ||
level level.Level | ||
} | ||
|
||
func (instance requireMaximalLevel) Filter(ctx FilterContext) (value interface{}, shouldBeRespected bool) { | ||
if ctx.GetLevel() > instance.level { | ||
return nil, false | ||
} | ||
|
||
return instance.Get(), true | ||
} |
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,93 @@ | ||
package fields | ||
|
||
import ( | ||
"fmt" | ||
"github.com/echocat/slf4g/level" | ||
"testing" | ||
|
||
"github.com/echocat/slf4g/internal/test/assert" | ||
) | ||
|
||
var veryComplexValue = struct{}{} | ||
var filterContextWithLeveInfo = filterContext{ | ||
level: level.Info, | ||
} | ||
var filterContextWithLeveDebug = filterContext{ | ||
level: level.Debug, | ||
} | ||
|
||
func ExampleRequireMaximalLevel() { | ||
filteredValue := RequireMaximalLevel(level.Debug, veryComplexValue) | ||
|
||
// Will be <nil>, <false> | ||
fmt.Println(filteredValue.Filter(filterContextWithLeveInfo)) | ||
|
||
// Will be <veryComplexValue>, <true> | ||
fmt.Println(filteredValue.Filter(filterContextWithLeveDebug)) | ||
} | ||
|
||
func Test_RequireMaximalLevelLazy_Get(t *testing.T) { | ||
expected := struct{ foo string }{foo: "bar"} | ||
givenLazy := LazyFunc(func() interface{} { return expected }) | ||
|
||
actualInstance := RequireMaximalLevelLazy(level.Info, givenLazy) | ||
actual := actualInstance.Get() | ||
|
||
assert.ToBeEqual(t, expected, actual) | ||
} | ||
|
||
func Test_RequireMaximalLevelLazy_Filter_respected(t *testing.T) { | ||
expected := struct{ foo string }{foo: "bar"} | ||
givenLazy := LazyFunc(func() interface{} { return expected }) | ||
|
||
actualInstance := RequireMaximalLevelLazy(level.Debug, givenLazy) | ||
actual, actualRespected := actualInstance.Filter(filterContextWithLeveDebug) | ||
|
||
assert.ToBeEqual(t, expected, actual) | ||
assert.ToBeEqual(t, true, actualRespected) | ||
} | ||
|
||
func Test_RequireMaximalLevelLazy_Filter_ignored(t *testing.T) { | ||
givenLazy := LazyFunc(func() interface{} { return struct{ foo string }{foo: "bar"} }) | ||
|
||
actualInstance := RequireMaximalLevelLazy(level.Debug, givenLazy) | ||
actual, actualRespected := actualInstance.Filter(filterContextWithLeveInfo) | ||
|
||
assert.ToBeNil(t, actual) | ||
assert.ToBeEqual(t, false, actualRespected) | ||
} | ||
|
||
func Test_RequireMaximalLevel_Filter_respected(t *testing.T) { | ||
expected := struct{ foo string }{foo: "bar"} | ||
|
||
actualInstance := RequireMaximalLevel(level.Debug, expected) | ||
actual, actualRespected := actualInstance.Filter(filterContextWithLeveDebug) | ||
|
||
assert.ToBeEqual(t, expected, actual) | ||
assert.ToBeEqual(t, true, actualRespected) | ||
} | ||
|
||
func Test_RequireMaximalLevel_Filter_ignored(t *testing.T) { | ||
actualInstance := RequireMaximalLevel(level.Debug, 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{} | ||
} | ||
|
||
func (instance filterContext) GetLevel() level.Level { | ||
return instance.level | ||
} | ||
|
||
func (instance filterContext) Get(key string) (value interface{}, exists bool) { | ||
if instance.fields == nil { | ||
return nil, false | ||
} | ||
v, ok := instance.fields[key] | ||
return v, ok | ||
} |
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 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 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
Oops, something went wrong.