From 7b1cfc27d307bb118af66e23d187427dd2401f44 Mon Sep 17 00:00:00 2001 From: "sudesh.shetty" Date: Wed, 31 Jul 2019 15:47:29 -0400 Subject: [PATCH] feat: logger provider injection - added implementation for logger provider injection using log.Initialize() function, which should be called before logging any line to use custom loger implementation - implemented log.NewLogger() which returns new instance of logger based on logger provider initialized - exposed APIs for log level and caller info management - closes:#21 Signed-off-by: sudesh.shetty --- pkg/common/log/logger.go | 150 ++++++++++++++++++ pkg/common/log/logger_test.go | 127 +++++++++++++++ pkg/common/log/provider.go | 55 +++++++ pkg/common/log/provider_test.go | 44 +++++ pkg/didcomm/transport/http/inbound.go | 11 +- pkg/didcomm/transport/http/inbound_test.go | 5 +- pkg/didcomm/transport/http/outbound.go | 5 +- pkg/didcomm/transport/http/outbound_test.go | 3 +- pkg/didcomm/transport/http/support_test.go | 5 +- .../common/logging/metadata/callerInfo.go | 22 ++- .../logging/metadata/callerInfo_test.go | 46 +++--- pkg/internal/common/logging/metadata/level.go | 27 +++- .../common/logging/metadata/level_test.go | 72 ++++----- pkg/internal/common/logging/metadata/opts.go | 14 +- .../common/logging/metadata/opts_test.go | 79 +++++---- .../common/logging/metadata/util_test.go | 27 ++-- pkg/internal/common/logging/metadata/utils.go | 10 +- pkg/internal/common/logging/modlog/deflog.go | 43 ++--- .../common/logging/modlog/deflog_test.go | 25 ++- pkg/internal/common/logging/modlog/logapi.go | 29 ++++ .../common/logging/modlog/logtestutils.go | 91 +++++------ pkg/internal/common/logging/modlog/modlog.go | 34 ++-- .../common/logging/modlog/modlog_test.go | 2 +- .../common/logging/modlog/provider.go | 55 ------- .../common/logging/modlog/provider_test.go | 47 ------ 25 files changed, 673 insertions(+), 355 deletions(-) create mode 100644 pkg/common/log/logger.go create mode 100644 pkg/common/log/logger_test.go create mode 100644 pkg/common/log/provider.go create mode 100644 pkg/common/log/provider_test.go create mode 100644 pkg/internal/common/logging/modlog/logapi.go delete mode 100644 pkg/internal/common/logging/modlog/provider.go delete mode 100644 pkg/internal/common/logging/modlog/provider_test.go diff --git a/pkg/common/log/logger.go b/pkg/common/log/logger.go new file mode 100644 index 0000000000..267f0ea4bc --- /dev/null +++ b/pkg/common/log/logger.go @@ -0,0 +1,150 @@ +/* +Copyright SecureKey Technologies Inc. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package log + +import ( + "sync" + + "github.com/hyperledger/aries-framework-go/pkg/internal/common/logging/metadata" +) + +const ( + //loggerNotInitializedMsg is used when a logger is not initialized before logging + loggerNotInitializedMsg = "Default logger initialized (please call log.Initialize() if you wish to use a custom logger)" + loggerModule = "aries-framework/common" +) + +//Log is an implementation of Logger interface. +//It encapsulates default or custom logger to provide module and level based logging. +type Log struct { + instance Logger + module string + once sync.Once +} + +// NewLogger creates and returns a Logger object based on the given module name. +// note: the underlying logger instance is lazy initialized on first use. +// To use your own logger implementation provide logger provider in 'Initialize()' before logging any line. +// If 'Initialize()' is not called before logging any line then default logging implementation will be used. +func NewLogger(module string) Logger { + return &Log{module: module} +} + +//Fatalf calls Fatalf function of underlying logger +//should possibly cause system shutdown based on implementation +func (l *Log) Fatalf(msg string, args ...interface{}) { + l.logger().Fatalf(msg, args...) +} + +//Panicf calls Panic function of underlying logger +//should possibly cause panic based on implementation +func (l *Log) Panicf(msg string, args ...interface{}) { + l.logger().Panicf(msg, args...) +} + +//Debugf calls Debugf function of underlying logger +func (l *Log) Debugf(msg string, args ...interface{}) { + l.logger().Debugf(msg, args...) +} + +//Infof calls Infof function of underlying logger +func (l *Log) Infof(msg string, args ...interface{}) { + l.logger().Infof(msg, args...) +} + +//Warnf calls Warnf function of underlying logger +func (l *Log) Warnf(msg string, args ...interface{}) { + l.logger().Warnf(msg, args...) +} + +//Errorf calls Errorf function of underlying logger +func (l *Log) Errorf(msg string, args ...interface{}) { + l.logger().Errorf(msg, args...) +} + +func (l *Log) logger() Logger { + l.once.Do(func() { + l.instance = loggerProvider().GetLogger(l.module) + }) + return l.instance +} + +//SetLevel - setting log level for given module +// Parameters: +// module is module name +// level is logging level +//If not set default logging level is info +func SetLevel(module string, level Level) { + metadata.SetLevel(module, metadata.Level(level)) +} + +//GetLevel - getting log level for given module +// Parameters: +// module is module name +// +// Returns: +// logging level +//If not set default logging level is info +func GetLevel(module string) Level { + return Level(metadata.GetLevel(module)) +} + +//IsEnabledFor - Check if given log level is enabled for given module +// Parameters: +// module is module name +// level is logging level +// +// Returns: +// is logging enabled for this module and level +//If not set default logging level is info +func IsEnabledFor(module string, level Level) bool { + return metadata.IsEnabledFor(module, metadata.Level(level)) +} + +// ParseLevel returns the log level from a string representation. +// Parameters: +// level is logging level in string representation +// +// Returns: +// logging level +func ParseLevel(level string) (Level, error) { + l, err := metadata.ParseLevel(level) + return Level(l), err +} + +//ShowCallerInfo - Show caller info in log lines for given log level and module +// Parameters: +// module is module name +// level is logging level +// +//note: based on implementation of custom logger, callerinfo information may not be available for custom logging provider +func ShowCallerInfo(module string, level Level) { + metadata.ShowCallerInfo(module, metadata.Level(level)) +} + +//HideCallerInfo - Do not show caller info in log lines for given log level and module +// Parameters: +// module is module name +// level is logging level +// +//note: based on implementation of custom logger, callerinfo information may not be available for custom logging provider +func HideCallerInfo(module string, level Level) { + metadata.HideCallerInfo(module, metadata.Level(level)) +} + +//IsCallerInfoEnabled - returns if caller info enabled for given log level and module +// Parameters: +// module is module name +// level is logging level +// +// Returns: +// is caller info enabled for this module and level +// +//note: based on implementation of custom logger, callerinfo information may not be available for custom logging provider +func IsCallerInfoEnabled(module string, level Level) bool { + return metadata.IsCallerInfoEnabled(module, metadata.Level(level)) +} diff --git a/pkg/common/log/logger_test.go b/pkg/common/log/logger_test.go new file mode 100644 index 0000000000..684f25a5f9 --- /dev/null +++ b/pkg/common/log/logger_test.go @@ -0,0 +1,127 @@ +/* +Copyright SecureKey Technologies Inc. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package log + +import ( + "sync" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/hyperledger/aries-framework-go/pkg/internal/common/logging/metadata" + "github.com/hyperledger/aries-framework-go/pkg/internal/common/logging/modlog" +) + +//TestDefaultLogger tests default logging feature when no custom logging provider is supplied through 'Initialize()' call +func TestDefaultLogger(t *testing.T) { + + defer func() { loggerProviderOnce = sync.Once{} }() + const module = "sample-module" + + //get new logger since Initialize is not called, default logger implementation will be used + logger := NewLogger(module) + + //force logger instance loading to switch output of logger to buffer for testing + logger.Infof("sample output") + modlog.SwitchLogOutputToBuffer(logger.(*Log).instance) + + //verify default logger + modlog.VerifyDefaultLogging(t, logger, module, metadata.SetLevel) + +} + +//TestAllLevels tests logging level behaviour +//logging levels can be set per modules, if not set then it will default to 'INFO' +func TestAllLevels(t *testing.T) { + + module := "sample-module-critical" + SetLevel(module, CRITICAL) + require.Equal(t, CRITICAL, GetLevel(module)) + verifyLevels(t, module, []Level{CRITICAL}, []Level{ERROR, WARNING, INFO, DEBUG}) + + module = "sample-module-error" + SetLevel(module, ERROR) + require.Equal(t, ERROR, GetLevel(module)) + verifyLevels(t, module, []Level{CRITICAL, ERROR}, []Level{WARNING, INFO, DEBUG}) + + module = "sample-module-warning" + SetLevel(module, WARNING) + require.Equal(t, WARNING, GetLevel(module)) + verifyLevels(t, module, []Level{CRITICAL, ERROR, WARNING}, []Level{INFO, DEBUG}) + + module = "sample-module-info" + SetLevel(module, INFO) + require.Equal(t, INFO, GetLevel(module)) + verifyLevels(t, module, []Level{CRITICAL, ERROR, WARNING, INFO}, []Level{DEBUG}) + + module = "sample-module-debug" + SetLevel(module, DEBUG) + require.Equal(t, DEBUG, GetLevel(module)) + verifyLevels(t, module, []Level{CRITICAL, ERROR, WARNING, INFO, DEBUG}, []Level{}) + +} + +//TestCallerInfos callerinfo behavior which displays caller function details in log lines +//CallerInfo is available in default logger. +//Based on implementation it may not be available for custom logger +func TestCallerInfos(t *testing.T) { + module := "sample-module-caller-info" + + ShowCallerInfo(module, CRITICAL) + ShowCallerInfo(module, DEBUG) + HideCallerInfo(module, INFO) + HideCallerInfo(module, ERROR) + HideCallerInfo(module, WARNING) + + require.True(t, IsCallerInfoEnabled(module, CRITICAL)) + require.True(t, IsCallerInfoEnabled(module, DEBUG)) + require.False(t, IsCallerInfoEnabled(module, INFO)) + require.False(t, IsCallerInfoEnabled(module, ERROR)) + require.False(t, IsCallerInfoEnabled(module, WARNING)) + +} + +//TestLogLevel testing 'LogLevel()' used for parsing log levels from strings +func TestLogLevel(t *testing.T) { + + verifyLevelsNoError := func(expected Level, levels ...string) { + for _, level := range levels { + actual, err := ParseLevel(level) + require.NoError(t, err, "not supposed to fail while parsing level string [%s]", level) + require.Equal(t, expected, actual) + } + } + + verifyLevelsNoError(CRITICAL, "critical", "CRITICAL", "CriticAL") + verifyLevelsNoError(ERROR, "error", "ERROR", "ErroR") + verifyLevelsNoError(WARNING, "warning", "WARNING", "WarninG") + verifyLevelsNoError(DEBUG, "debug", "DEBUG", "DebUg") + verifyLevelsNoError(INFO, "info", "INFO", "iNFo") +} + +//TestParseLevelError testing 'LogLevel()' used for parsing log levels from strings +func TestParseLevelError(t *testing.T) { + + verifyLevelError := func(expected Level, levels ...string) { + for _, level := range levels { + _, err := ParseLevel(level) + require.Error(t, err, "not supposed to succeed while parsing level string [%s]", level) + } + } + + verifyLevelError(DEBUG, "", "D", "DE BUG", ".") + +} + +func verifyLevels(t *testing.T, module string, enabled []Level, disabled []Level) { + for _, level := range enabled { + require.True(t, IsEnabledFor(module, level), "expected level [%s] to be enabled for module [%s]", metadata.ParseString(metadata.Level(level)), module) + } + for _, level := range disabled { + require.False(t, IsEnabledFor(module, level), "expected level [%s] to be disabled for module [%s]", metadata.ParseString(metadata.Level(level)), module) + } +} diff --git a/pkg/common/log/provider.go b/pkg/common/log/provider.go new file mode 100644 index 0000000000..7a10d6437c --- /dev/null +++ b/pkg/common/log/provider.go @@ -0,0 +1,55 @@ +/* +Copyright SecureKey Technologies Inc. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package log + +import ( + "sync" + + "github.com/hyperledger/aries-framework-go/pkg/internal/common/logging/modlog" +) + +// loggerProviderInstance is logger factory singleton - access only via loggerProvider() +var loggerProviderInstance LoggerProvider +var loggerProviderOnce sync.Once + +//Initialize sets new custom logging provider which takes over logging operations. +//It is required to call this function before making any loggings for using custom loggers. +func Initialize(l LoggerProvider) { + loggerProviderOnce.Do(func() { + loggerProviderInstance = &modlogProvider{l} + logger := loggerProviderInstance.GetLogger(loggerModule) + logger.Debugf("Logger provider initialized") + }) +} + +func loggerProvider() LoggerProvider { + loggerProviderOnce.Do(func() { + // A custom logger must be initialized prior to the first log output + // Otherwise the built-in logger is used + loggerProviderInstance = &modlogProvider{} + logger := loggerProviderInstance.GetLogger(loggerModule) + logger.Debugf(loggerNotInitializedMsg) + }) + return loggerProviderInstance +} + +// modlogProvider is a module based logger provider wrapped on given custom logging provider +// if custom logger provider is not provided, then default logger will be used +type modlogProvider struct { + custom LoggerProvider +} + +//GetLogger returns moduled logger implementation. +func (p *modlogProvider) GetLogger(module string) Logger { + var logger Logger + if p.custom != nil { + logger = p.custom.GetLogger(module) + } else { + logger = modlog.NewDefLog(module) + } + return modlog.NewModLog(logger, module) +} diff --git a/pkg/common/log/provider_test.go b/pkg/common/log/provider_test.go new file mode 100644 index 0000000000..153d9799a3 --- /dev/null +++ b/pkg/common/log/provider_test.go @@ -0,0 +1,44 @@ +/* +Copyright SecureKey Technologies Inc. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package log + +import ( + "sync" + "testing" + + "github.com/hyperledger/aries-framework-go/pkg/internal/common/logging/modlog" +) + +//TestDefaultLogger tests custom logging feature when custom logging provider is supplied through 'Initialize()' call +func TestCustomLogger(t *testing.T) { + + defer func() { loggerProviderOnce = sync.Once{} }() + const module = "sample-module" + + //intialize logger provider with custom logger provider + Initialize(newCustomProvider(module)) + + //get logger instance + logger := NewLogger(module) + + modlog.VerifyCustomLogger(t, logger, module) +} + +//newCustomProvider return new sample logging provider to demonstrate custom logging provider +func newCustomProvider(module string) *sampleProvider { + return &sampleProvider{modlog.GetSampleCustomLogger(module)} +} + +// sampleProvider is a custom logging provider +type sampleProvider struct { + logger Logger +} + +//GetLogger returns custom logger implementation +func (p *sampleProvider) GetLogger(module string) Logger { + return p.logger +} diff --git a/pkg/didcomm/transport/http/inbound.go b/pkg/didcomm/transport/http/inbound.go index 12f38411ae..04cfef276b 100644 --- a/pkg/didcomm/transport/http/inbound.go +++ b/pkg/didcomm/transport/http/inbound.go @@ -9,12 +9,16 @@ package http import ( "fmt" "io/ioutil" - "log" + + "github.com/hyperledger/aries-framework-go/pkg/common/log" + "net/http" "github.com/pkg/errors" ) +var logger = log.NewLogger("aries-framework/transport") + // MessageHandler is a function that handles the inbound request payload // the payload will be unpacked prior to calling this function. type MessageHandler func(payload []byte) @@ -27,7 +31,7 @@ type MessageHandler func(payload []byte) // Users of this library must manage the handling of all inbound payloads in this function. func NewInboundHandler(msgHandler MessageHandler) (http.Handler, error) { if msgHandler == nil { - log.Println("Error creating a new inbound handler: message handler function is nil") + logger.Errorf("Error creating a new inbound handler: message handler function is nil") return nil, errors.New("Failed to create NewInboundHandler") } @@ -36,7 +40,6 @@ func NewInboundHandler(msgHandler MessageHandler) (http.Handler, error) { }), nil } -// TODO Log error message with a common logger library for aries-framework-go func processPOSTRequest(w http.ResponseWriter, r *http.Request, messageHandler MessageHandler) { if valid := validateHTTPMethod(w, r); !valid { return @@ -47,7 +50,7 @@ func processPOSTRequest(w http.ResponseWriter, r *http.Request, messageHandler M body, err := ioutil.ReadAll(r.Body) if err != nil { - log.Printf("Error reading request body: %s - returning Code: %d", err, http.StatusInternalServerError) + logger.Errorf("Error reading request body: %s - returning Code: %d", err, http.StatusInternalServerError) http.Error(w, "Failed to read payload", http.StatusInternalServerError) } diff --git a/pkg/didcomm/transport/http/inbound_test.go b/pkg/didcomm/transport/http/inbound_test.go index fa496cb79c..84784e2ad5 100644 --- a/pkg/didcomm/transport/http/inbound_test.go +++ b/pkg/didcomm/transport/http/inbound_test.go @@ -11,7 +11,6 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "log" "net/http" "testing" @@ -21,7 +20,7 @@ import ( // mockMsgHandler is an http.msgHandler type, it is similar to MockHandler struct // but will be injected in WithInboundSetting() directly, will be used by each transport comm handle function func mockMsgHandler(payload []byte) { - log.Printf("Payload received is %s", payload) + logger.Debugf("Payload received is %s", payload) } func TestInboundHandler(t *testing.T) { @@ -41,7 +40,7 @@ func TestInboundHandler(t *testing.T) { defer func() { e := server.Close() if e != nil { - log.Fatalf("Failed to stop server: %s", e) + t.Fatalf("Failed to stop server: %s", e) } }() diff --git a/pkg/didcomm/transport/http/outbound.go b/pkg/didcomm/transport/http/outbound.go index 651d26f612..569b4d5683 100644 --- a/pkg/didcomm/transport/http/outbound.go +++ b/pkg/didcomm/transport/http/outbound.go @@ -9,7 +9,6 @@ package http import ( "bytes" "crypto/tls" - "log" "net/http" "time" @@ -82,7 +81,7 @@ func NewOutbound(opts ...OutboundHTTPOpt) (*OutboundHTTPClient, error) { func (cs *OutboundHTTPClient) Send(data string, url string) (string, error) { resp, err := cs.client.Post(url, commContentType, bytes.NewBuffer([]byte(data))) if err != nil { - log.Printf("HTTP Transport - Error posting did envelope to agent at [%s]: %v", url, err) + logger.Errorf("HTTP Transport - Error posting did envelope to agent at [%s]: %v", url, err) return "", err } @@ -96,7 +95,7 @@ func (cs *OutboundHTTPClient) Send(data string, url string) (string, error) { defer func() { e := resp.Body.Close() if e != nil { - log.Printf("HTTP Transport - Error closing response body: %v", e) + logger.Errorf("HTTP Transport - Error closing response body: %v", e) } }() buf := new(bytes.Buffer) diff --git a/pkg/didcomm/transport/http/outbound_test.go b/pkg/didcomm/transport/http/outbound_test.go index 85003a5b7a..8a4ed9b057 100644 --- a/pkg/didcomm/transport/http/outbound_test.go +++ b/pkg/didcomm/transport/http/outbound_test.go @@ -10,7 +10,6 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "log" "testing" "github.com/stretchr/testify/require" @@ -43,7 +42,7 @@ func TestOutboundHTTPTransport(t *testing.T) { defer func() { err := server.Close() if err != nil { - log.Fatalf("Failed to stop server: %s", err) + t.Fatalf("Failed to stop server: %s", err) } }() diff --git a/pkg/didcomm/transport/http/support_test.go b/pkg/didcomm/transport/http/support_test.go index 3ee6e10cb9..484ab1c49e 100644 --- a/pkg/didcomm/transport/http/support_test.go +++ b/pkg/didcomm/transport/http/support_test.go @@ -11,7 +11,6 @@ import ( "encoding/pem" "fmt" "io/ioutil" - "log" "net" "net/http" "path/filepath" @@ -49,12 +48,12 @@ func startMockServer(handler http.Handler) net.Listener { // ":0" will make the listener auto assign a free port listener, err := net.Listen("tcp", ":0") if err != nil { - log.Fatalf("HTTP listener failed to start: %s", err) + logger.Fatalf("HTTP listener failed to start: %s", err) } go func() { err := http.ServeTLS(listener, handler, certPrefix+"ec-pubCert1.pem", certPrefix+"ec-key1.pem") if err != nil && !strings.Contains(err.Error(), "use of closed network connection") { - log.Fatalf("HTTP server failed to start: %s", err) + logger.Fatalf("HTTP server failed to start: %s", err) } }() return listener diff --git a/pkg/internal/common/logging/metadata/callerInfo.go b/pkg/internal/common/logging/metadata/callerInfo.go index 1d4f0348f7..1789ef7fc6 100644 --- a/pkg/internal/common/logging/metadata/callerInfo.go +++ b/pkg/internal/common/logging/metadata/callerInfo.go @@ -6,25 +6,21 @@ SPDX-License-Identifier: Apache-2.0 package metadata -import ( - "github.com/hyperledger/aries-framework-go/pkg/common/log" -) - func newCallerInfo() *callerInfo { return &callerInfo{ info: map[callerInfoKey]bool{ - {"", log.CRITICAL}: true, - {"", log.ERROR}: true, - {"", log.WARNING}: true, - {"", log.INFO}: true, - {"", log.DEBUG}: true, + {"", CRITICAL}: true, + {"", ERROR}: true, + {"", WARNING}: true, + {"", INFO}: true, + {"", DEBUG}: true, }, } } type callerInfoKey struct { module string - level log.Level + level Level } //callerInfo maintains module-level based information to show or hide caller info @@ -33,17 +29,17 @@ type callerInfo struct { } //ShowCallerInfo enables caller info for given module and level -func (l *callerInfo) ShowCallerInfo(module string, level log.Level) { +func (l *callerInfo) ShowCallerInfo(module string, level Level) { l.info[callerInfoKey{module, level}] = true } //HideCallerInfo disables caller info for given module and level -func (l *callerInfo) HideCallerInfo(module string, level log.Level) { +func (l *callerInfo) HideCallerInfo(module string, level Level) { l.info[callerInfoKey{module, level}] = false } //IsCallerInfoEnabled returns if caller info enabled for given module and level -func (l *callerInfo) IsCallerInfoEnabled(module string, level log.Level) bool { +func (l *callerInfo) IsCallerInfoEnabled(module string, level Level) bool { show, exists := l.info[callerInfoKey{module, level}] if !exists { diff --git a/pkg/internal/common/logging/metadata/callerInfo_test.go b/pkg/internal/common/logging/metadata/callerInfo_test.go index 09dd0e7b57..f9fe503218 100644 --- a/pkg/internal/common/logging/metadata/callerInfo_test.go +++ b/pkg/internal/common/logging/metadata/callerInfo_test.go @@ -10,8 +10,6 @@ import ( "testing" "github.com/stretchr/testify/require" - - "github.com/hyperledger/aries-framework-go/pkg/common/log" ) func TestCallerInfoSetting(t *testing.T) { @@ -20,37 +18,37 @@ func TestCallerInfoSetting(t *testing.T) { sampleModuleName := "sample-module-name" //By default caller info should be enabled if not set - require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, log.DEBUG), "Callerinfo supposed to be enabled for this level") - require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, log.INFO), "Callerinfo supposed to be enabled for this level") - require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, log.WARNING), "Callerinfo supposed to be enabled for this level") - require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, log.ERROR), "Callerinfo supposed to be enabled for this level") - require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, log.CRITICAL), "Callerinfo supposed to be enabled for this level") + require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, DEBUG), "Callerinfo supposed to be enabled for this level") + require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, INFO), "Callerinfo supposed to be enabled for this level") + require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, WARNING), "Callerinfo supposed to be enabled for this level") + require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, ERROR), "Callerinfo supposed to be enabled for this level") + require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, CRITICAL), "Callerinfo supposed to be enabled for this level") - sampleCallerInfoSetting.HideCallerInfo(sampleModuleName, log.DEBUG) - require.False(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, log.DEBUG), "Callerinfo supposed to be disabled for this level") + sampleCallerInfoSetting.HideCallerInfo(sampleModuleName, DEBUG) + require.False(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, DEBUG), "Callerinfo supposed to be disabled for this level") - sampleCallerInfoSetting.ShowCallerInfo(sampleModuleName, log.DEBUG) - require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, log.DEBUG), "Callerinfo supposed to be enabled for this level") + sampleCallerInfoSetting.ShowCallerInfo(sampleModuleName, DEBUG) + require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, DEBUG), "Callerinfo supposed to be enabled for this level") - sampleCallerInfoSetting.HideCallerInfo(sampleModuleName, log.WARNING) - require.False(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, log.WARNING), "Callerinfo supposed to be disabled for this level") + sampleCallerInfoSetting.HideCallerInfo(sampleModuleName, WARNING) + require.False(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, WARNING), "Callerinfo supposed to be disabled for this level") - sampleCallerInfoSetting.ShowCallerInfo(sampleModuleName, log.WARNING) - require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, log.WARNING), "Callerinfo supposed to be enabled for this level") + sampleCallerInfoSetting.ShowCallerInfo(sampleModuleName, WARNING) + require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, WARNING), "Callerinfo supposed to be enabled for this level") - sampleCallerInfoSetting.HideCallerInfo(sampleModuleName, log.DEBUG) - require.False(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, log.DEBUG), "Callerinfo supposed to be disabled for this level") + sampleCallerInfoSetting.HideCallerInfo(sampleModuleName, DEBUG) + require.False(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, DEBUG), "Callerinfo supposed to be disabled for this level") - sampleCallerInfoSetting.ShowCallerInfo(sampleModuleName, log.DEBUG) - require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, log.DEBUG), "Callerinfo supposed to be enabled for this level") + sampleCallerInfoSetting.ShowCallerInfo(sampleModuleName, DEBUG) + require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(sampleModuleName, DEBUG), "Callerinfo supposed to be enabled for this level") //By default caller info should be enabled for any module name not set before moduleNames := []string{"sample-module-name-doesnt-exists", "", "@$#@$@"} for _, moduleName := range moduleNames { - require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(moduleName, log.INFO), "Callerinfo supposed to be enabled for this level") - require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(moduleName, log.WARNING), "Callerinfo supposed to be enabled for this level") - require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(moduleName, log.ERROR), "Callerinfo supposed to be enabled for this level") - require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(moduleName, log.CRITICAL), "Callerinfo supposed to be enabled for this level") - require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(moduleName, log.DEBUG), "Callerinfo supposed to be enabled for this level") + require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(moduleName, INFO), "Callerinfo supposed to be enabled for this level") + require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(moduleName, WARNING), "Callerinfo supposed to be enabled for this level") + require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(moduleName, ERROR), "Callerinfo supposed to be enabled for this level") + require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(moduleName, CRITICAL), "Callerinfo supposed to be enabled for this level") + require.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(moduleName, DEBUG), "Callerinfo supposed to be enabled for this level") } } diff --git a/pkg/internal/common/logging/metadata/level.go b/pkg/internal/common/logging/metadata/level.go index a4aa75bb6d..30cd866669 100644 --- a/pkg/internal/common/logging/metadata/level.go +++ b/pkg/internal/common/logging/metadata/level.go @@ -6,26 +6,37 @@ SPDX-License-Identifier: Apache-2.0 package metadata -import ( - "github.com/hyperledger/aries-framework-go/pkg/common/log" +// Level defines all available log levels for logging messages. +type Level int + +// Log levels. +// note: below constants are copy of 'log.Level' constants added to avoid circular references, +// care should be taken before changing below constants including their order. +// For any updates in level constants below, corresponding changes has to be done for constants in 'log.Level'. +const ( + CRITICAL Level = iota + ERROR + WARNING + INFO //default logging level + DEBUG ) const ( - defaultLogLevel = log.INFO + defaultLogLevel = INFO defaultModuleName = "" ) func newModuledLevels() *moduleLevels { - return &moduleLevels{levels: make(map[string]log.Level)} + return &moduleLevels{levels: make(map[string]Level)} } //moduleLevels maintains log levels based on modules type moduleLevels struct { - levels map[string]log.Level + levels map[string]Level } // GetLevel returns the log level for given module and level. -func (l *moduleLevels) GetLevel(module string) log.Level { +func (l *moduleLevels) GetLevel(module string) Level { level, exists := l.levels[module] if !exists { level, exists = l.levels[defaultModuleName] @@ -38,11 +49,11 @@ func (l *moduleLevels) GetLevel(module string) log.Level { } // SetLevel sets the log level for given module and level. -func (l *moduleLevels) SetLevel(module string, level log.Level) { +func (l *moduleLevels) SetLevel(module string, level Level) { l.levels[module] = level } // IsEnabledFor will return true if logging is enabled for given module and level. -func (l *moduleLevels) IsEnabledFor(module string, level log.Level) bool { +func (l *moduleLevels) IsEnabledFor(module string, level Level) bool { return level <= l.GetLevel(module) } diff --git a/pkg/internal/common/logging/metadata/level_test.go b/pkg/internal/common/logging/metadata/level_test.go index 88d3305077..0c82b63966 100644 --- a/pkg/internal/common/logging/metadata/level_test.go +++ b/pkg/internal/common/logging/metadata/level_test.go @@ -9,60 +9,58 @@ import ( "testing" "github.com/stretchr/testify/require" - - "github.com/hyperledger/aries-framework-go/pkg/common/log" ) func TestLogLevels(t *testing.T) { mlevel := newModuledLevels() - mlevel.SetLevel("module-xyz-info", log.INFO) - mlevel.SetLevel("module-xyz-debug", log.DEBUG) - mlevel.SetLevel("module-xyz-error", log.ERROR) - mlevel.SetLevel("module-xyz-warning", log.WARNING) - mlevel.SetLevel("module-xyz-critical", log.CRITICAL) + mlevel.SetLevel("module-xyz-info", INFO) + mlevel.SetLevel("module-xyz-debug", DEBUG) + mlevel.SetLevel("module-xyz-error", ERROR) + mlevel.SetLevel("module-xyz-warning", WARNING) + mlevel.SetLevel("module-xyz-critical", CRITICAL) //Run info level checks - require.True(t, mlevel.IsEnabledFor("module-xyz-info", log.CRITICAL)) - require.True(t, mlevel.IsEnabledFor("module-xyz-info", log.ERROR)) - require.True(t, mlevel.IsEnabledFor("module-xyz-info", log.WARNING)) - require.True(t, mlevel.IsEnabledFor("module-xyz-info", log.INFO)) - require.False(t, mlevel.IsEnabledFor("module-xyz-info", log.DEBUG)) + require.True(t, mlevel.IsEnabledFor("module-xyz-info", CRITICAL)) + require.True(t, mlevel.IsEnabledFor("module-xyz-info", ERROR)) + require.True(t, mlevel.IsEnabledFor("module-xyz-info", WARNING)) + require.True(t, mlevel.IsEnabledFor("module-xyz-info", INFO)) + require.False(t, mlevel.IsEnabledFor("module-xyz-info", DEBUG)) //Run debug level checks - require.True(t, mlevel.IsEnabledFor("module-xyz-debug", log.CRITICAL)) - require.True(t, mlevel.IsEnabledFor("module-xyz-debug", log.ERROR)) - require.True(t, mlevel.IsEnabledFor("module-xyz-debug", log.WARNING)) - require.True(t, mlevel.IsEnabledFor("module-xyz-debug", log.INFO)) - require.True(t, mlevel.IsEnabledFor("module-xyz-debug", log.DEBUG)) + require.True(t, mlevel.IsEnabledFor("module-xyz-debug", CRITICAL)) + require.True(t, mlevel.IsEnabledFor("module-xyz-debug", ERROR)) + require.True(t, mlevel.IsEnabledFor("module-xyz-debug", WARNING)) + require.True(t, mlevel.IsEnabledFor("module-xyz-debug", INFO)) + require.True(t, mlevel.IsEnabledFor("module-xyz-debug", DEBUG)) //Run warning level checks - require.True(t, mlevel.IsEnabledFor("module-xyz-warning", log.CRITICAL)) - require.True(t, mlevel.IsEnabledFor("module-xyz-warning", log.ERROR)) - require.True(t, mlevel.IsEnabledFor("module-xyz-warning", log.WARNING)) - require.False(t, mlevel.IsEnabledFor("module-xyz-warning", log.INFO)) - require.False(t, mlevel.IsEnabledFor("module-xyz-warning", log.DEBUG)) + require.True(t, mlevel.IsEnabledFor("module-xyz-warning", CRITICAL)) + require.True(t, mlevel.IsEnabledFor("module-xyz-warning", ERROR)) + require.True(t, mlevel.IsEnabledFor("module-xyz-warning", WARNING)) + require.False(t, mlevel.IsEnabledFor("module-xyz-warning", INFO)) + require.False(t, mlevel.IsEnabledFor("module-xyz-warning", DEBUG)) //Run error level checks - require.True(t, mlevel.IsEnabledFor("module-xyz-error", log.CRITICAL)) - require.True(t, mlevel.IsEnabledFor("module-xyz-error", log.ERROR)) - require.False(t, mlevel.IsEnabledFor("module-xyz-error", log.WARNING)) - require.False(t, mlevel.IsEnabledFor("module-xyz-error", log.INFO)) - require.False(t, mlevel.IsEnabledFor("module-xyz-error", log.DEBUG)) + require.True(t, mlevel.IsEnabledFor("module-xyz-error", CRITICAL)) + require.True(t, mlevel.IsEnabledFor("module-xyz-error", ERROR)) + require.False(t, mlevel.IsEnabledFor("module-xyz-error", WARNING)) + require.False(t, mlevel.IsEnabledFor("module-xyz-error", INFO)) + require.False(t, mlevel.IsEnabledFor("module-xyz-error", DEBUG)) //Run error critical checks - require.True(t, mlevel.IsEnabledFor("module-xyz-critical", log.CRITICAL)) - require.False(t, mlevel.IsEnabledFor("module-xyz-critical", log.ERROR)) - require.False(t, mlevel.IsEnabledFor("module-xyz-critical", log.WARNING)) - require.False(t, mlevel.IsEnabledFor("module-xyz-critical", log.INFO)) - require.False(t, mlevel.IsEnabledFor("module-xyz-critical", log.DEBUG)) + require.True(t, mlevel.IsEnabledFor("module-xyz-critical", CRITICAL)) + require.False(t, mlevel.IsEnabledFor("module-xyz-critical", ERROR)) + require.False(t, mlevel.IsEnabledFor("module-xyz-critical", WARNING)) + require.False(t, mlevel.IsEnabledFor("module-xyz-critical", INFO)) + require.False(t, mlevel.IsEnabledFor("module-xyz-critical", DEBUG)) //Run default log level check --> which is info level - require.True(t, mlevel.IsEnabledFor("module-xyz-random-module", log.CRITICAL)) - require.True(t, mlevel.IsEnabledFor("module-xyz-random-module", log.ERROR)) - require.True(t, mlevel.IsEnabledFor("module-xyz-random-module", log.WARNING)) - require.True(t, mlevel.IsEnabledFor("module-xyz-random-module", log.INFO)) - require.False(t, mlevel.IsEnabledFor("module-xyz-random-module", log.DEBUG)) + require.True(t, mlevel.IsEnabledFor("module-xyz-random-module", CRITICAL)) + require.True(t, mlevel.IsEnabledFor("module-xyz-random-module", ERROR)) + require.True(t, mlevel.IsEnabledFor("module-xyz-random-module", WARNING)) + require.True(t, mlevel.IsEnabledFor("module-xyz-random-module", INFO)) + require.False(t, mlevel.IsEnabledFor("module-xyz-random-module", DEBUG)) } diff --git a/pkg/internal/common/logging/metadata/opts.go b/pkg/internal/common/logging/metadata/opts.go index beb8a72790..26b42fad51 100644 --- a/pkg/internal/common/logging/metadata/opts.go +++ b/pkg/internal/common/logging/metadata/opts.go @@ -8,8 +8,6 @@ package metadata import ( "sync" - - "github.com/hyperledger/aries-framework-go/pkg/common/log" ) var rwmutex = &sync.RWMutex{} @@ -17,42 +15,42 @@ var levels = newModuledLevels() var callerInfos = newCallerInfo() //SetLevel - setting log level for given module -func SetLevel(module string, level log.Level) { +func SetLevel(module string, level Level) { rwmutex.Lock() defer rwmutex.Unlock() levels.SetLevel(module, level) } //GetLevel - getting log level for given module -func GetLevel(module string) log.Level { +func GetLevel(module string) Level { rwmutex.RLock() defer rwmutex.RUnlock() return levels.GetLevel(module) } //IsEnabledFor - Check if given log level is enabled for given module -func IsEnabledFor(module string, level log.Level) bool { +func IsEnabledFor(module string, level Level) bool { rwmutex.RLock() defer rwmutex.RUnlock() return levels.IsEnabledFor(module, level) } //ShowCallerInfo - Show caller info in log lines for given log level and module -func ShowCallerInfo(module string, level log.Level) { +func ShowCallerInfo(module string, level Level) { rwmutex.Lock() defer rwmutex.Unlock() callerInfos.ShowCallerInfo(module, level) } //HideCallerInfo - Do not show caller info in log lines for given log level and module -func HideCallerInfo(module string, level log.Level) { +func HideCallerInfo(module string, level Level) { rwmutex.Lock() defer rwmutex.Unlock() callerInfos.HideCallerInfo(module, level) } //IsCallerInfoEnabled - returns if caller info enabled for given log level and module -func IsCallerInfoEnabled(module string, level log.Level) bool { +func IsCallerInfoEnabled(module string, level Level) bool { rwmutex.RLock() defer rwmutex.RUnlock() return callerInfos.IsCallerInfoEnabled(module, level) diff --git a/pkg/internal/common/logging/metadata/opts_test.go b/pkg/internal/common/logging/metadata/opts_test.go index 6129d98cda..d6fea56c40 100644 --- a/pkg/internal/common/logging/metadata/opts_test.go +++ b/pkg/internal/common/logging/metadata/opts_test.go @@ -9,68 +9,67 @@ package metadata import ( "testing" - "github.com/hyperledger/aries-framework-go/pkg/common/log" "github.com/stretchr/testify/require" ) func TestLevels(t *testing.T) { module := "sample-module-critical" - SetLevel(module, log.CRITICAL) - require.Equal(t, log.CRITICAL, GetLevel(module)) - verifyLevels(t, module, []log.Level{log.CRITICAL}, []log.Level{log.ERROR, log.WARNING, log.INFO, log.DEBUG}) + SetLevel(module, CRITICAL) + require.Equal(t, CRITICAL, GetLevel(module)) + verifyLevels(t, module, []Level{CRITICAL}, []Level{ERROR, WARNING, INFO, DEBUG}) module = "sample-module-error" - SetLevel(module, log.ERROR) - require.Equal(t, log.ERROR, GetLevel(module)) - verifyLevels(t, module, []log.Level{log.CRITICAL, log.ERROR}, []log.Level{log.WARNING, log.INFO, log.DEBUG}) + SetLevel(module, ERROR) + require.Equal(t, ERROR, GetLevel(module)) + verifyLevels(t, module, []Level{CRITICAL, ERROR}, []Level{WARNING, INFO, DEBUG}) module = "sample-module-warning" - SetLevel(module, log.WARNING) - require.Equal(t, log.WARNING, GetLevel(module)) - verifyLevels(t, module, []log.Level{log.CRITICAL, log.ERROR, log.WARNING}, []log.Level{log.INFO, log.DEBUG}) + SetLevel(module, WARNING) + require.Equal(t, WARNING, GetLevel(module)) + verifyLevels(t, module, []Level{CRITICAL, ERROR, WARNING}, []Level{INFO, DEBUG}) module = "sample-module-info" - SetLevel(module, log.INFO) - require.Equal(t, log.INFO, GetLevel(module)) - verifyLevels(t, module, []log.Level{log.CRITICAL, log.ERROR, log.WARNING, log.INFO}, []log.Level{log.DEBUG}) + SetLevel(module, INFO) + require.Equal(t, INFO, GetLevel(module)) + verifyLevels(t, module, []Level{CRITICAL, ERROR, WARNING, INFO}, []Level{DEBUG}) module = "sample-module-debug" - SetLevel(module, log.DEBUG) - require.Equal(t, log.DEBUG, GetLevel(module)) - verifyLevels(t, module, []log.Level{log.CRITICAL, log.ERROR, log.WARNING, log.INFO, log.DEBUG}, []log.Level{}) + SetLevel(module, DEBUG) + require.Equal(t, DEBUG, GetLevel(module)) + verifyLevels(t, module, []Level{CRITICAL, ERROR, WARNING, INFO, DEBUG}, []Level{}) } func TestCallerInfos(t *testing.T) { module := "sample-module-caller-info" - require.True(t, IsCallerInfoEnabled(module, log.CRITICAL)) - require.True(t, IsCallerInfoEnabled(module, log.DEBUG)) - require.True(t, IsCallerInfoEnabled(module, log.INFO)) - require.True(t, IsCallerInfoEnabled(module, log.ERROR)) - require.True(t, IsCallerInfoEnabled(module, log.WARNING)) - - ShowCallerInfo(module, log.CRITICAL) - ShowCallerInfo(module, log.DEBUG) - HideCallerInfo(module, log.INFO) - HideCallerInfo(module, log.ERROR) - HideCallerInfo(module, log.WARNING) - - require.True(t, IsCallerInfoEnabled(module, log.CRITICAL)) - require.True(t, IsCallerInfoEnabled(module, log.DEBUG)) - require.False(t, IsCallerInfoEnabled(module, log.INFO)) - require.False(t, IsCallerInfoEnabled(module, log.ERROR)) - require.False(t, IsCallerInfoEnabled(module, log.WARNING)) - - require.True(t, IsCallerInfoEnabled(module, log.CRITICAL)) - require.True(t, IsCallerInfoEnabled(module, log.DEBUG)) - require.False(t, IsCallerInfoEnabled(module, log.INFO)) - require.False(t, IsCallerInfoEnabled(module, log.ERROR)) - require.False(t, IsCallerInfoEnabled(module, log.WARNING)) + require.True(t, IsCallerInfoEnabled(module, CRITICAL)) + require.True(t, IsCallerInfoEnabled(module, DEBUG)) + require.True(t, IsCallerInfoEnabled(module, INFO)) + require.True(t, IsCallerInfoEnabled(module, ERROR)) + require.True(t, IsCallerInfoEnabled(module, WARNING)) + + ShowCallerInfo(module, CRITICAL) + ShowCallerInfo(module, DEBUG) + HideCallerInfo(module, INFO) + HideCallerInfo(module, ERROR) + HideCallerInfo(module, WARNING) + + require.True(t, IsCallerInfoEnabled(module, CRITICAL)) + require.True(t, IsCallerInfoEnabled(module, DEBUG)) + require.False(t, IsCallerInfoEnabled(module, INFO)) + require.False(t, IsCallerInfoEnabled(module, ERROR)) + require.False(t, IsCallerInfoEnabled(module, WARNING)) + + require.True(t, IsCallerInfoEnabled(module, CRITICAL)) + require.True(t, IsCallerInfoEnabled(module, DEBUG)) + require.False(t, IsCallerInfoEnabled(module, INFO)) + require.False(t, IsCallerInfoEnabled(module, ERROR)) + require.False(t, IsCallerInfoEnabled(module, WARNING)) } -func verifyLevels(t *testing.T, module string, enabled []log.Level, disabled []log.Level) { +func verifyLevels(t *testing.T, module string, enabled []Level, disabled []Level) { for _, level := range enabled { require.True(t, IsEnabledFor(module, level), "expected level [%s] to be enabled for module [%s]", ParseString(level), module) } diff --git a/pkg/internal/common/logging/metadata/util_test.go b/pkg/internal/common/logging/metadata/util_test.go index bc584ee202..173d44342b 100644 --- a/pkg/internal/common/logging/metadata/util_test.go +++ b/pkg/internal/common/logging/metadata/util_test.go @@ -8,13 +8,12 @@ package metadata import ( "testing" - "github.com/hyperledger/aries-framework-go/pkg/common/log" "github.com/stretchr/testify/require" ) func TestParseLevel(t *testing.T) { - verifyLevelsNoError := func(expected log.Level, levels ...string) { + verifyLevelsNoError := func(expected Level, levels ...string) { for _, level := range levels { actual, err := ParseLevel(level) require.NoError(t, err, "not supposed to fail while parsing level string [%s]", level) @@ -22,30 +21,30 @@ func TestParseLevel(t *testing.T) { } } - verifyLevelsNoError(log.CRITICAL, "critical", "CRITICAL", "CriticAL") - verifyLevelsNoError(log.ERROR, "error", "ERROR", "ErroR") - verifyLevelsNoError(log.WARNING, "warning", "WARNING", "WarninG") - verifyLevelsNoError(log.DEBUG, "debug", "DEBUG", "DebUg") - verifyLevelsNoError(log.INFO, "info", "INFO", "iNFo") + verifyLevelsNoError(CRITICAL, "critical", "CRITICAL", "CriticAL") + verifyLevelsNoError(ERROR, "error", "ERROR", "ErroR") + verifyLevelsNoError(WARNING, "warning", "WARNING", "WarninG") + verifyLevelsNoError(DEBUG, "debug", "DEBUG", "DebUg") + verifyLevelsNoError(INFO, "info", "INFO", "iNFo") } func TestParseLevelError(t *testing.T) { - verifyLevelError := func(expected log.Level, levels ...string) { + verifyLevelError := func(expected Level, levels ...string) { for _, level := range levels { _, err := ParseLevel(level) require.Error(t, err, "not supposed to succeed while parsing level string [%s]", level) } } - verifyLevelError(log.DEBUG, "", "D", "DE BUG", ".") + verifyLevelError(DEBUG, "", "D", "DE BUG", ".") } func TestParseString(t *testing.T) { - require.Equal(t, "CRITICAL", ParseString(log.CRITICAL)) - require.Equal(t, "ERROR", ParseString(log.ERROR)) - require.Equal(t, "WARNING", ParseString(log.WARNING)) - require.Equal(t, "DEBUG", ParseString(log.DEBUG)) - require.Equal(t, "INFO", ParseString(log.INFO)) + require.Equal(t, "CRITICAL", ParseString(CRITICAL)) + require.Equal(t, "ERROR", ParseString(ERROR)) + require.Equal(t, "WARNING", ParseString(WARNING)) + require.Equal(t, "DEBUG", ParseString(DEBUG)) + require.Equal(t, "INFO", ParseString(INFO)) } diff --git a/pkg/internal/common/logging/metadata/utils.go b/pkg/internal/common/logging/metadata/utils.go index 7967fd0e5b..737b25163c 100644 --- a/pkg/internal/common/logging/metadata/utils.go +++ b/pkg/internal/common/logging/metadata/utils.go @@ -9,8 +9,6 @@ package metadata import ( "errors" "strings" - - "github.com/hyperledger/aries-framework-go/pkg/common/log" ) //levelNames - log level names in string @@ -23,16 +21,16 @@ var levelNames = []string{ } // ParseLevel returns the log level from a string representation. -func ParseLevel(level string) (log.Level, error) { +func ParseLevel(level string) (Level, error) { for i, name := range levelNames { if strings.EqualFold(name, level) { - return log.Level(i), nil + return Level(i), nil } } - return log.ERROR, errors.New("logger: invalid log level") + return ERROR, errors.New("logger: invalid log level") } //ParseString returns string representation of given log level -func ParseString(level log.Level) string { +func ParseString(level Level) string { return levelNames[level] } diff --git a/pkg/internal/common/logging/modlog/deflog.go b/pkg/internal/common/logging/modlog/deflog.go index 9a50a59dee..dc0c43dbf0 100644 --- a/pkg/internal/common/logging/modlog/deflog.go +++ b/pkg/internal/common/logging/modlog/deflog.go @@ -16,8 +16,6 @@ import ( "strings" "github.com/hyperledger/aries-framework-go/pkg/internal/common/logging/metadata" - - logapi "github.com/hyperledger/aries-framework-go/pkg/common/log" ) const ( @@ -26,58 +24,63 @@ const ( callerInfoFormatter = "- %s " ) -// defLog is a logger implementation built on top of standard go log. +//NewDefLog returns new DefLog instance based on given module +func NewDefLog(module string) *DefLog { + return &DefLog{logger: log.New(os.Stdout, fmt.Sprintf(logPrefixFormatter, module), log.Ldate|log.Ltime|log.LUTC), module: module} +} + +// DefLog is a logger implementation built on top of standard go log. // There is a configurable caller info feature which displays caller function information name in logged lines. // caller info can be configured by log levels and modules. By default it is enabled. // Log Format : []