feat: enhance logging configuration to use dynamic log level from environment variable
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,5 +14,13 @@ func configureLog() {
|
|||||||
DisableSorting: false,
|
DisableSorting: false,
|
||||||
PadLevelText: true,
|
PadLevelText: true,
|
||||||
})
|
})
|
||||||
log.SetLevel(log.DebugLevel)
|
|
||||||
|
logLevelEnv := config.App.LogLevel
|
||||||
|
logLevel, err := log.ParseLevel(logLevelEnv)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("Nivel de log inválido '%s', usando INFO por defecto", logLevelEnv)
|
||||||
|
logLevel = log.InfoLevel
|
||||||
|
}
|
||||||
|
|
||||||
|
log.SetLevel(logLevel)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@@ -10,6 +11,7 @@ import (
|
|||||||
type appConfig struct {
|
type appConfig struct {
|
||||||
SourceDbUrl string
|
SourceDbUrl string
|
||||||
TargetDbUrl string
|
TargetDbUrl string
|
||||||
|
LogLevel string
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadEnv() {
|
func loadEnv() {
|
||||||
@@ -32,9 +34,15 @@ func getAppConfig() appConfig {
|
|||||||
log.Fatal("TARGET_DB_URL environment variable not set")
|
log.Fatal("TARGET_DB_URL environment variable not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logLevel := strings.ToUpper(os.Getenv("LOG_LEVEL"))
|
||||||
|
if logLevel == "" {
|
||||||
|
logLevel = "INFO"
|
||||||
|
}
|
||||||
|
|
||||||
return appConfig{
|
return appConfig{
|
||||||
SourceDbUrl: sourceDbUrl,
|
SourceDbUrl: sourceDbUrl,
|
||||||
TargetDbUrl: targetDbUrl,
|
TargetDbUrl: targetDbUrl,
|
||||||
|
LogLevel: logLevel,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user