Skip to content

Commit

Permalink
[FAB-8667] Remove GetLogger method / Rename InitLogger
Browse files Browse the repository at this point in the history
Change-Id: Id259c4574869e4529cc029db8362ce57926da3c6
Signed-off-by: Troy Ronda <[email protected]>
  • Loading branch information
troyronda committed Mar 6, 2018
1 parent 9606a4c commit 9d194d5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pkg/fabsdk/fabsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func initSDK(sdk *FabricSDK, config core.Config, opts []Option) error {
if sdk.opts.Logger == nil {
return errors.New("Missing logger from pkg suite")
}
logging.InitLogger(sdk.opts.Logger)
logging.Initialize(sdk.opts.Logger)

// Initialize crypto provider
cryptoSuite, err := sdk.opts.Core.CreateCryptoSuiteProvider(config)
Expand Down
17 changes: 4 additions & 13 deletions pkg/fabsdk/singleton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,17 @@ func TestDefLoggerFactory(t *testing.T) {
}

const moduleName = "mymodule"
l1, err := logging.GetLogger(moduleName)
if err != nil {
t.Fatal("Unexpected error getting logger")
}
l1 := logging.NewLogger(moduleName)

// output a log message to force initializatin
l1.Info("message")

// ensure that the logger cannot be overridden
// (initializing a new logger should have no effect)
lf := NewMockLoggerFactory()
logging.InitLogger(lf)
logging.Initialize(lf)

l2, err := logging.GetLogger(moduleName)
if err != nil {
t.Fatal("Unexpected error getting logger")
}
l2 := logging.NewLogger(moduleName)

// output a log message to force initializatin
l2.Info("message")
Expand All @@ -66,10 +60,7 @@ func TestOptLoggerFactory(t *testing.T) {
}

const moduleName = "mymodule"
l, err := logging.GetLogger(moduleName)
if err != nil {
t.Fatal("Unexpected error getting logger")
}
l := logging.NewLogger(moduleName)

// output a log message to force initializatin
l.Info("message")
Expand Down
19 changes: 5 additions & 14 deletions pkg/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,10 @@ const (
loggerModule = "fabsdk/core"
)

// GetLogger creates and returns a Logger object based on the module name.
func GetLogger(module string) (*Logger, error) {
// note: the underlying logger instance is lazy initialized on first use
return &Logger{module: module}, nil
}

// NewLogger is like GetLogger but panics if the logger can't be created.
// NewLogger creates and returns a Logger object based on the module name.
func NewLogger(module string) *Logger {
logger, err := GetLogger(module)
if err != nil {
panic("logger: " + module + ": " + err.Error())
}
return logger
// note: the underlying logger instance is lazy initialized on first use
return &Logger{module: module}
}

func loggerProvider() api.LoggerProvider {
Expand All @@ -72,9 +63,9 @@ func loggerProvider() api.LoggerProvider {
return loggerProviderInstance
}

//InitLogger sets new logger which takes over logging operations.
//Initialize sets new logger which takes over logging operations.
//It is required to call this function before making any loggings.
func InitLogger(l api.LoggerProvider) {
func Initialize(l api.LoggerProvider) {
loggerProviderOnce.Do(func() {
loggerProviderInstance = l
logger := loggerProviderInstance.GetLogger(loggerModule)
Expand Down
6 changes: 3 additions & 3 deletions pkg/logging/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestLoggingForCustomLogger(t *testing.T) {

//Now add sample logger
resetLoggerInstance()
InitLogger(testdata.GetSampleLoggingProvider(&buf))
Initialize(testdata.GetSampleLoggingProvider(&buf))
//Create new logger
logger := NewLogger(moduleName)

Expand Down Expand Up @@ -73,7 +73,7 @@ func TestDefaultModulledLoggingBehavior(t *testing.T) {

//Init logger with default logger
resetLoggerInstance()
InitLogger(modlog.LoggerProvider())
Initialize(modlog.LoggerProvider())
//Get new logger
dlogger := NewLogger(moduleName)
// force initialization
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestLoggerSetting(t *testing.T) {
logger.Info("brown fox jumps over the lazy dog")
assert.True(t, loggerProviderInstance != nil, "Logger is supposed to be initialized now")
resetLoggerInstance()
InitLogger(modlog.LoggerProvider())
Initialize(modlog.LoggerProvider())
assert.True(t, loggerProviderInstance != nil, "Logger is supposed to be initialized now")
}

Expand Down

0 comments on commit 9d194d5

Please sign in to comment.