From 17f86160afb392ae0cc08224a4ea470d7612ff19 Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Thu, 19 Oct 2023 16:14:32 +0200 Subject: [PATCH] Improvements to the database log configuration --- pkg/cli/logging.go | 22 +++++++--------------- pkg/constants/logging.go | 10 ++++++++++ pkg/zdb/zdbconfig/logger.go | 34 ++++++++++++++++++++++++---------- pkg/zdb/zdbconfig/zdbconfig.go | 1 + 4 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 pkg/constants/logging.go diff --git a/pkg/cli/logging.go b/pkg/cli/logging.go index 766d0fe..0868332 100644 --- a/pkg/cli/logging.go +++ b/pkg/cli/logging.go @@ -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) { diff --git a/pkg/constants/logging.go b/pkg/constants/logging.go new file mode 100644 index 0000000..49abfe6 --- /dev/null +++ b/pkg/constants/logging.go @@ -0,0 +1,10 @@ +package constants + +const ( + DebugLevel = "debug" + InfoLevel = "info" + WarnLevel = "warn" + ErrorLevel = "error" + FatalLevel = "fatal" + PanicLevel = "panic" +) diff --git a/pkg/zdb/zdbconfig/logger.go b/pkg/zdb/zdbconfig/logger.go index df0d08e..ba538bf 100644 --- a/pkg/zdb/zdbconfig/logger.go +++ b/pkg/zdb/zdbconfig/logger.go @@ -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, }, ) diff --git a/pkg/zdb/zdbconfig/zdbconfig.go b/pkg/zdb/zdbconfig/zdbconfig.go index 7178609..ebd4444 100644 --- a/pkg/zdb/zdbconfig/zdbconfig.go +++ b/pkg/zdb/zdbconfig/zdbconfig.go @@ -20,6 +20,7 @@ type Config struct { } type LogConfig struct { + Prefix string LogLevel string SlowThreshold int IgnoreRecordNotFoundError bool