Skip to content

Commit

Permalink
[FAB-7776] Customizable default logger in SDK Logging
Browse files Browse the repository at this point in the history
The objective is to inject custom logger in place of
default logger which can reuse leveled modules.


Change-Id: I23c47e5b276564e65c336137c90d538684a66f88
Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Jan 18, 2018
1 parent fa39e8e commit 151ff03
Show file tree
Hide file tree
Showing 19 changed files with 865 additions and 613 deletions.
1 change: 1 addition & 0 deletions def/fabapi/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
apisdk "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/api"
"github.com/hyperledger/fabric-sdk-go/pkg/logging"

"github.com/pkg/errors"
)

Expand Down
5 changes: 3 additions & 2 deletions def/factory/defcore/corefactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/errors"
kvs "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/keyvaluestore"
signingMgr "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/signingmgr"
"github.com/hyperledger/fabric-sdk-go/pkg/logging/deflogger"

"github.com/hyperledger/fabric-sdk-go/pkg/logging/modlog"
)

// ProviderFactory represents the default SDK provider factory.
Expand Down Expand Up @@ -92,5 +93,5 @@ func (f *ProviderFactory) NewFabricProvider(config apiconfig.Config, stateStore
// NewLoggerProvider returns a new default implementation of a logger backend
// This function is separated from the factory to allow logger creation first.
func NewLoggerProvider() apilogging.LoggerProvider {
return deflogger.LoggerProvider()
return modlog.LoggerProvider()
}
4 changes: 2 additions & 2 deletions def/factory/defcore/corefactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
kvs "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/keyvaluestore"
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/mocks"
signingMgr "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/signingmgr"
"github.com/hyperledger/fabric-sdk-go/pkg/logging/deflogger"
"github.com/hyperledger/fabric-sdk-go/pkg/logging/modlog"
)

func TestNewConfigProvider(t *testing.T) {
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestNewFactoryFabricProvider(t *testing.T) {
func TestNewLoggingProvider(t *testing.T) {
logger := NewLoggerProvider()

_, ok := logger.(*deflogger.Provider)
_, ok := logger.(*modlog.Provider)
if !ok {
t.Fatalf("Unexpected logger provider created")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/fabsdk/defpkgsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/def/factory/defcore"
"github.com/hyperledger/fabric-sdk-go/def/factory/defsvc"
apisdk "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/api"
"github.com/hyperledger/fabric-sdk-go/pkg/logging/deflogger"
"github.com/hyperledger/fabric-sdk-go/pkg/logging/modlog"
)

type defPkgSuite struct{}
Expand All @@ -34,5 +34,5 @@ func (ps *defPkgSuite) Session() (apisdk.SessionClientFactory, error) {
}

func (ps *defPkgSuite) Logger() (apilogging.LoggerProvider, error) {
return deflogger.LoggerProvider(), nil
return modlog.LoggerProvider(), nil
}
4 changes: 2 additions & 2 deletions pkg/fabsdk/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/def/factory/defsvc"
"github.com/hyperledger/fabric-sdk-go/pkg/errors"
apisdk "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/api"
"github.com/hyperledger/fabric-sdk-go/pkg/logging/deflogger"
"github.com/hyperledger/fabric-sdk-go/pkg/logging/modlog"
)

type mockCorePkg struct {
Expand Down Expand Up @@ -116,5 +116,5 @@ func (ps *mockPkgSuite) Logger() (apilogging.LoggerProvider, error) {
if ps.errOnLogger {
return nil, errors.New("Error")
}
return deflogger.LoggerProvider(), nil
return modlog.LoggerProvider(), nil
}
4 changes: 2 additions & 2 deletions pkg/fabsdk/singleton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/api/apilogging"
configImpl "github.com/hyperledger/fabric-sdk-go/pkg/config"
"github.com/hyperledger/fabric-sdk-go/pkg/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/logging/deflogger"
"github.com/hyperledger/fabric-sdk-go/pkg/logging/modlog"
)

func TestDefLoggerFactory(t *testing.T) {
Expand Down Expand Up @@ -97,7 +97,7 @@ type MockLoggerFactory struct {
func NewMockLoggerFactory() *MockLoggerFactory {
lf := MockLoggerFactory{}
lf.ActiveModules = make(map[string]bool)
lf.logger = deflogger.LoggerProvider()
lf.logger = modlog.LoggerProvider()

return &lf
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package deflogger
package decorator

import (
"github.com/hyperledger/fabric-sdk-go/api/apilogging"
Expand All @@ -15,25 +15,29 @@ type callerInfoKey struct {
level apilogging.Level
}

type callerInfo struct {
//CallerInfo maintains module-level based information to toggle caller info
type CallerInfo struct {
showcaller map[callerInfoKey]bool
}

func (l *callerInfo) ShowCallerInfo(module string, level apilogging.Level) {
//ShowCallerInfo enables caller info for given module and level
func (l *CallerInfo) ShowCallerInfo(module string, level apilogging.Level) {
if l.showcaller == nil {
l.showcaller = l.getDefaultCallerInfoSetting()
}
l.showcaller[callerInfoKey{module, level}] = true
}

func (l *callerInfo) HideCallerInfo(module string, level apilogging.Level) {
//HideCallerInfo disables caller info for given module and level
func (l *CallerInfo) HideCallerInfo(module string, level apilogging.Level) {
if l.showcaller == nil {
l.showcaller = l.getDefaultCallerInfoSetting()
}
l.showcaller[callerInfoKey{module, level}] = false
}

func (l *callerInfo) IsCallerInfoEnabled(module string, level apilogging.Level) bool {
//IsCallerInfoEnabled returns if callerinfo enabled for given module and level
func (l *CallerInfo) IsCallerInfoEnabled(module string, level apilogging.Level) bool {
showcaller, exists := l.showcaller[callerInfoKey{module, level}]
if exists == false {
//If no callerinfo setting exists, then look for default
Expand All @@ -46,7 +50,7 @@ func (l *callerInfo) IsCallerInfoEnabled(module string, level apilogging.Level)
}

//getDefaultCallerInfoSetting default setting for callerinfo
func (l *callerInfo) getDefaultCallerInfoSetting() map[callerInfoKey]bool {
func (l *CallerInfo) getDefaultCallerInfoSetting() map[callerInfoKey]bool {
return map[callerInfoKey]bool{
callerInfoKey{"", apilogging.CRITICAL}: true,
callerInfoKey{"", apilogging.ERROR}: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package deflogger
package decorator

import (
"testing"
Expand All @@ -15,7 +15,7 @@ import (

func TestCallerInfoSetting(t *testing.T) {

sampleCallerInfoSetting := callerInfo{}
sampleCallerInfoSetting := CallerInfo{}
samppleModuleName := "sample-module-name"

sampleCallerInfoSetting.ShowCallerInfo(samppleModuleName, apilogging.DEBUG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package deflogger
package decorator

import (
"github.com/hyperledger/fabric-sdk-go/api/apilogging"
)

type moduleLeveled struct {
//ModuleLevels maintains log levels based on module
type ModuleLevels struct {
levels map[string]apilogging.Level
}

// GetLevel returns the log level for the given module.
func (l *moduleLeveled) GetLevel(module string) apilogging.Level {
func (l *ModuleLevels) GetLevel(module string) apilogging.Level {
level, exists := l.levels[module]
if exists == false {
level, exists = l.levels[""]
Expand All @@ -28,14 +29,14 @@ func (l *moduleLeveled) GetLevel(module string) apilogging.Level {
}

// SetLevel sets the log level for the given module.
func (l *moduleLeveled) SetLevel(module string, level apilogging.Level) {
func (l *ModuleLevels) SetLevel(module string, level apilogging.Level) {
if l.levels == nil {
l.levels = make(map[string]apilogging.Level)
}
l.levels[module] = level
}

// IsEnabledFor will return true if logging is enabled for the given module.
func (l *moduleLeveled) IsEnabledFor(module string, level apilogging.Level) bool {
func (l *ModuleLevels) IsEnabledFor(module string, level apilogging.Level) bool {
return level <= l.GetLevel(module)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package deflogger
package decorator

import (
"testing"
Expand All @@ -14,7 +14,7 @@ import (

func TestLogLevels(t *testing.T) {

mlevel := moduleLeveled{}
mlevel := ModuleLevels{}

mlevel.SetLevel("module-xyz-info", apilogging.INFO)
mlevel.SetLevel("module-xyz-debug", apilogging.DEBUG)
Expand Down
Loading

0 comments on commit 151ff03

Please sign in to comment.