Skip to content

Commit

Permalink
Improvements to the database log configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslopezf committed Oct 19, 2023
1 parent 67883e7 commit 17f8616
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
22 changes: 7 additions & 15 deletions pkg/cli/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,20 @@ package cli

import (
"errors"
"github.com/zondax/golem/pkg/constants"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"strings"
)

const (
DebugLevel = "debug"
InfoLevel = "info"
WarnLevel = "warn"
ErrorLevel = "error"
FatalLevel = "fatal"
PanicLevel = "panic"
)

var stringToLevel = map[string]zapcore.Level{
DebugLevel: zapcore.DebugLevel,
InfoLevel: zapcore.InfoLevel,
WarnLevel: zapcore.WarnLevel,
ErrorLevel: zapcore.ErrorLevel,
FatalLevel: zapcore.FatalLevel,
PanicLevel: zapcore.PanicLevel,
constants.DebugLevel: zapcore.DebugLevel,
constants.InfoLevel: zapcore.InfoLevel,
constants.WarnLevel: zapcore.WarnLevel,
constants.ErrorLevel: zapcore.ErrorLevel,
constants.FatalLevel: zapcore.FatalLevel,
constants.PanicLevel: zapcore.PanicLevel,
}

func InitGlobalLogger(level string) (*zap.Logger, error) {
Expand Down
10 changes: 10 additions & 0 deletions pkg/constants/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package constants

const (
DebugLevel = "debug"
InfoLevel = "info"
WarnLevel = "warn"
ErrorLevel = "error"
FatalLevel = "fatal"
PanicLevel = "panic"
)
34 changes: 24 additions & 10 deletions pkg/zdb/zdbconfig/logger.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
package zdbconfig

import (
"github.com/zondax/golem/pkg/constants"
"gorm.io/gorm/logger"
"log"
"os"
"strings"
"time"
)

const (
defaultPrefix = "\r\n"
)

var stringToLevel = map[string]logger.LogLevel{
constants.InfoLevel: logger.Info,
constants.WarnLevel: logger.Warn,
constants.ErrorLevel: logger.Error,
constants.FatalLevel: logger.Silent,
}

func getDBLogger(config LogConfig) logger.Interface {
logLevel := logger.Error
switch config.LogLevel {
case "info":
logLevel = logger.Info
case "warn":
logLevel = logger.Warn
case "error":
logLevel = logger.Error
case "silent":
logLevel = logger.Silent

gormLevel, ok := stringToLevel[strings.ToLower(config.LogLevel)]
if ok {
logLevel = gormLevel
}

prefix := defaultPrefix
if config.Prefix != "" {
prefix = config.Prefix
}

return logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags),
log.New(os.Stdout, prefix, log.LstdFlags),
logger.Config{
SlowThreshold: time.Duration(config.SlowThreshold) * time.Second,
LogLevel: logLevel,
IgnoreRecordNotFoundError: config.IgnoreRecordNotFoundError,
ParameterizedQueries: config.ParameterizedQuery,
Colorful: config.Colorful,
},
)
Expand Down
1 change: 1 addition & 0 deletions pkg/zdb/zdbconfig/zdbconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
}

type LogConfig struct {
Prefix string
LogLevel string
SlowThreshold int
IgnoreRecordNotFoundError bool
Expand Down

0 comments on commit 17f8616

Please sign in to comment.