refactor: update logging configuration to use string levels

This commit is contained in:
Bhunter 2025-01-14 11:58:04 +01:00
parent f0187f4052
commit 4c5d5035f7
4 changed files with 26 additions and 30 deletions

View file

@ -139,10 +139,13 @@ func findAvailablePort(startPort int) (int, error) {
}
func runApplication(ctx context.Context, conf *config.ServerCmdConfig) {
lvl, err := zapcore.ParseLevel(conf.Log.Level)
if err != nil {
lvl = zapcore.InfoLevel
}
logging.SetConfig(&logging.Config{
Level: zapcore.Level(conf.Log.Level),
Development: conf.Log.Development,
FilePath: conf.Log.File,
Level: lvl,
FilePath: conf.Log.File,
})
lg := logging.DefaultLogger().Sugar()

View file

@ -13,6 +13,7 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/tgdrive/teldrive/internal/duration"
"go.uber.org/zap/zapcore"
)
type ServerConfig struct {
@ -30,9 +31,8 @@ type CacheConfig struct {
}
type LoggingConfig struct {
Level int `mapstructure:"level"`
Development bool `mapstructure:"development"`
File string `mapstructure:"file"`
Level string `mapstructure:"level"`
File string `mapstructure:"file"`
}
type JWTConfig struct {
@ -44,7 +44,7 @@ type JWTConfig struct {
type DBConfig struct {
DataSource string `mapstructure:"data-source"`
PrepareStmt bool `mapstructure:"prepare-stmt"`
LogLevel int `mapstructure:"log-level"`
LogLevel string `mapstructure:"log-level"`
Pool struct {
Enable bool `mapstructure:"enable"`
MaxOpenConnections int `mapstructure:"max-open-connections"`
@ -196,13 +196,12 @@ func AddCommonFlags(flags *pflag.FlagSet, config *ServerCmdConfig) {
flags.StringP("config", "c", "", "Config file path (default $HOME/.teldrive/config.toml)")
// Log config
flags.IntVarP(&config.Log.Level, "log-level", "", -1, "Logging level")
flags.StringVar(&config.Log.Level, "log-level", zapcore.InfoLevel.String(), "Logging level")
flags.StringVar(&config.Log.File, "log-file", "", "Logging file path")
flags.BoolVar(&config.Log.Development, "log-development", false, "Enable development mode")
// DB config
flags.StringVar(&config.DB.DataSource, "db-data-source", "", "Database connection string")
flags.IntVar(&config.DB.LogLevel, "db-log-level", 1, "Database log level")
flags.StringVar(&config.DB.LogLevel, "db-log-level", zapcore.InfoLevel.String(), "Database log level")
flags.BoolVar(&config.DB.PrepareStmt, "db-prepare-stmt", true, "Enable prepared statements")
flags.BoolVar(&config.DB.Pool.Enable, "db-pool-enable", true, "Enable database pool")
flags.IntVar(&config.DB.Pool.MaxIdleConnections, "db-pool-max-open-connections", 25, "Database max open connections")

View file

@ -14,17 +14,19 @@ import (
)
func NewDatabase(cfg *config.DBConfig, lg *zap.SugaredLogger) (*gorm.DB, error) {
var (
db *gorm.DB
err error
logger = NewLogger(lg, time.Second, true, zapcore.Level(cfg.LogLevel))
)
level, err := zapcore.ParseLevel(cfg.LogLevel)
if err != nil {
level = zapcore.InfoLevel
}
var db *gorm.DB
for i := 0; i <= 5; i++ {
db, err = gorm.Open(postgres.New(postgres.Config{
DSN: cfg.DataSource,
PreferSimpleProtocol: !cfg.PrepareStmt,
}), &gorm.Config{
Logger: logger,
Logger: NewLogger(lg, time.Second, true, level),
NamingStrategy: schema.NamingStrategy{
TablePrefix: "teldrive.",
SingularTable: false,

View file

@ -21,21 +21,18 @@ var (
)
var conf = &Config{
Level: zapcore.InfoLevel,
Development: true,
Level: zapcore.InfoLevel,
}
type Config struct {
Level zapcore.Level
Development bool
FilePath string
Level zapcore.Level
FilePath string
}
func SetConfig(c *Config) {
conf = &Config{
Level: c.Level,
Development: c.Development,
FilePath: c.FilePath,
Level: c.Level,
FilePath: c.FilePath,
}
}
@ -70,12 +67,7 @@ func NewLogger(conf *Config) *zap.Logger {
zapcore.AddSync(lumberjackLogger), zap.NewAtomicLevelAt(conf.Level)))
}
options := []zap.Option{}
if conf.Development {
options = append(options, zap.Development())
}
return zap.New(zapcore.NewTee(cores...), options...)
return zap.New(zapcore.NewTee(cores...))
}
func DefaultLogger() *zap.Logger {