Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Logging fixes for understanding log level
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Apr 14, 2022
1 parent cdc57be commit b8eb8b0
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cachestore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)

// Set logger if not set
if client.options.logger == nil {
client.options.logger = newLogger()
client.options.logger = newBasicLogger(client.IsDebug())
}

// EMPTY! Engine was NOT set, show warning and use in-memory cache
Expand Down
2 changes: 1 addition & 1 deletion cachestore/client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func TestWithLogger(t *testing.T) {

t.Run("test applying option", func(t *testing.T) {
options := &clientOptions{}
customClient := newLogger()
customClient := newBasicLogger(true)
opt := WithLogger(customClient)
opt(options)
assert.Equal(t, customClient, options.logger)
Expand Down
30 changes: 24 additions & 6 deletions cachestore/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,42 @@ import (
"fmt"

zlogger "github.com/mrz1836/go-logger"
"gorm.io/gorm/logger"
)

// newLogger will return a basic logger interface
func newLogger() Logger {
return &basicLogger{}
// newBasicLogger will return a basic logger interface
func newBasicLogger(debugging bool) Logger {
logLevel := logger.Warn
if debugging {
logLevel = logger.Info
}
return &basicLogger{LogLevel: logLevel}
}

// basicLogger is a basic logging implementation
type basicLogger struct{}
type basicLogger struct {
LogLevel logger.LogLevel
}

// LogMode log mode
func (l *basicLogger) LogMode(level logger.LogLevel) Logger {
newLogger := *l
newLogger.LogLevel = level
return &newLogger
}

// Info will print information
func (l *basicLogger) Info(_ context.Context, message string, params ...interface{}) {
displayLog(zlogger.INFO, message, params...)
if l.LogLevel <= logger.Info {
displayLog(zlogger.INFO, message, params...)
}
}

// Warn will print a warning
func (l *basicLogger) Warn(_ context.Context, message string, params ...interface{}) {
displayLog(zlogger.WARN, message, params...)
if l.LogLevel <= logger.Warn {
displayLog(zlogger.WARN, message, params...)
}
}

// displayLog will display a log using logger
Expand Down
10 changes: 5 additions & 5 deletions chainstate/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)
// Use NewRelic if it's enabled (use existing txn if found on ctx)
ctx = client.options.getTxnCtx(ctx)

// Set logger if not set
if client.options.logger == nil {
client.options.logger = newBasicLogger(client.IsDebug())
}

// Start Minercraft
if err := client.startMinerCraft(ctx); err != nil {
return nil, err
Expand All @@ -83,11 +88,6 @@ func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)
// Start NowNodes
client.startNowNodes(ctx)

// Set logger if not set
if client.options.logger == nil {
client.options.logger = newLogger()
}

// Return the client
return client, nil
}
Expand Down
2 changes: 1 addition & 1 deletion chainstate/client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ func TestWithLogger(t *testing.T) {
options := &clientOptions{
config: &syncConfig{},
}
customClient := newLogger()
customClient := newBasicLogger(true)
opt := WithLogger(customClient)
opt(options)
assert.Equal(t, customClient, options.logger)
Expand Down
26 changes: 21 additions & 5 deletions chainstate/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,35 @@ import (
"fmt"

zlogger "github.com/mrz1836/go-logger"
"gorm.io/gorm/logger"
)

// newLogger will return a basic logger interface
func newLogger() Logger {
return &basicLogger{}
// newBasicLogger will return a basic logger interface
func newBasicLogger(debugging bool) Logger {
logLevel := logger.Warn
if debugging {
logLevel = logger.Info
}
return &basicLogger{LogLevel: logLevel}
}

// basicLogger is a basic logging implementation
type basicLogger struct{}
type basicLogger struct {
LogLevel logger.LogLevel
}

// LogMode log mode
func (l *basicLogger) LogMode(level logger.LogLevel) Logger {
newLogger := *l
newLogger.LogLevel = level
return &newLogger
}

// Info print information
func (l *basicLogger) Info(_ context.Context, message string, params ...interface{}) {
displayLog(zlogger.INFO, message, params...)
if l.LogLevel <= logger.Info {
displayLog(zlogger.INFO, message, params...)
}
}

// displayLog will display a log using logger
Expand Down
10 changes: 5 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)
// Use NewRelic if it's enabled (use existing txn if found on ctx)
ctx = client.GetOrStartTxn(ctx, "new_client")

// Set the logger (if no custom logger was detected)
if client.options.logger == nil {
client.options.logger = logger.NewLogger(client.IsDebug())
}

// Load the Cachestore client
var err error
if err = client.loadCache(ctx); err != nil {
Expand Down Expand Up @@ -164,11 +169,6 @@ func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)
return nil, err
}

// Set the logger (if no custom logger was detected)
if client.options.logger == nil {
client.options.logger = logger.NewLogger(client.IsDebug())
}

// Default paymail server config (generic capabilities and domain check disabled)
if client.options.paymail.serverConfig.Configuration == nil {
if err = client.loadDefaultPaymailConfig(); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions datastore/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ func (l *basicLogger) LogMode(level logger.LogLevel) logger.Interface {

// Info print information
func (l *basicLogger) Info(_ context.Context, message string, params ...interface{}) {
if l.LogLevel >= logger.Info {
if l.LogLevel <= logger.Info {
displayLog(zlogger.INFO, message, params...)
}
}

// Warn print warn messages
func (l *basicLogger) Warn(_ context.Context, message string, params ...interface{}) {
if l.LogLevel >= logger.Warn {
if l.LogLevel <= logger.Warn {
displayLog(zlogger.WARN, message, params...)
}
}

// Error print error messages
func (l *basicLogger) Error(_ context.Context, message string, params ...interface{}) {
if l.LogLevel >= logger.Error {
if l.LogLevel <= logger.Error {
displayLog(zlogger.ERROR, message, params...)
}
}
Expand Down
6 changes: 3 additions & 3 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ func (l *basicLogger) LogMode(level glogger.LogLevel) glogger.Interface {

// Info print information
func (l *basicLogger) Info(_ context.Context, message string, params ...interface{}) {
if l.LogLevel >= glogger.Info {
if l.LogLevel <= glogger.Info {
displayLog(zlogger.INFO, message, params...)
}
}

// Warn print warn messages
func (l *basicLogger) Warn(_ context.Context, message string, params ...interface{}) {
if l.LogLevel >= glogger.Warn {
if l.LogLevel <= glogger.Warn {
displayLog(zlogger.WARN, message, params...)
}
}

// Error print error messages
func (l *basicLogger) Error(_ context.Context, message string, params ...interface{}) {
if l.LogLevel >= glogger.Error {
if l.LogLevel <= glogger.Error {
displayLog(zlogger.ERROR, message, params...)
}
}
Expand Down

0 comments on commit b8eb8b0

Please sign in to comment.