Skip to content

Commit

Permalink
[FAB-6886] SDK logger new utility function
Browse files Browse the repository at this point in the history
Current utility 'IsEnabledFor' needs module name
as an argument to find if given level is enabled
for given module, which forces clients to preserve
logger module names for this use.

This new utility, IsEnabledForLogger - takes logger itself
as an argument and it will find module name by accessing
logger argument.


Change-Id: I0242667c12428a7a99e85184f4d7e639e3beb84e
Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Nov 3, 2017
1 parent 2efa8bf commit 2443ac7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func IsEnabledFor(level Level, module string) bool {
return moduleLevels.IsEnabledFor(level, module)
}

// IsEnabledFor will return true if given logging level is enabled for the given logger.
func IsEnabledForLogger(level Level, logger *Logger) bool {
return moduleLevels.IsEnabledFor(level, logger.module)
}

//Fatal calls Fatal function of underlying logger
func (l *Logger) Fatal(args ...interface{}) {
l.getCurrentLogger().Fatal(args...)
Expand Down
8 changes: 8 additions & 0 deletions pkg/logging/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,17 @@ func TestDefaultLogging(t *testing.T) {

verifyEmpty(t, buf.String(), "debug log isn't supposed to show up for info level")

//Should be false
verifyFalse(t, IsEnabledForLogger(DEBUG, logger), "logging.IsEnabled for is not working as expected, expected false but got true")
verifyFalse(t, IsEnabledFor(DEBUG, moduleName), "logging.IsEnabled for is not working as expected, expected false but got true")

//Now change the log level to DEBUG
SetLevel(DEBUG, moduleName)

//Should be false
verifyTrue(t, IsEnabledForLogger(DEBUG, logger), "logging.IsEnabled for is not working as expected, expected true but got false")
verifyTrue(t, IsEnabledFor(DEBUG, moduleName), "logging.IsEnabled for is not working as expected, expected true but got false")

//Test logger.debug outputs
verifyBasicLogging(t, DEBUG, logger.Debug, nil, &buf, false)
verifyBasicLogging(t, DEBUG, logger.Debugln, nil, &buf, false)
Expand Down

0 comments on commit 2443ac7

Please sign in to comment.