bingobot/internal/config/config.go
Piper Pentagram 9f678b6be7 Remove source info from log messages
slog.HandlerOptions.AddSource is a cool feature, but since we moved the
logger to a separate package it no longer works. It now always shows
source info for the log package, rather than the code that called it.

This change removes it.
2024-11-13 17:02:47 -08:00

55 lines
1.1 KiB
Go

package config
import (
"fmt"
"github.com/spf13/viper"
)
type AppConfig struct {
LogFile string `yaml:"log_file"`
LogDir string `yaml:"log_dir"`
LogMaxSizeMB int `yaml:"log_max_size_mb"`
LogMaxBackups int `yaml:"log_max_backups"`
LogMaxAgeDays int `yaml:"log_max_age_days"`
LogCompression bool `yaml:"log_compression"`
}
var config *AppConfig
func Get() *AppConfig {
return config
}
func Init() error {
setDefaults()
// automatically populate config with any matching environment vars
viper.SetEnvPrefix("BB")
viper.AutomaticEnv()
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
return fmt.Errorf("error reading configs: %s", err)
}
err = viper.Unmarshal(&config)
if err != nil {
return fmt.Errorf("error unmarshaling configs: %s", err)
}
return nil
}
func setDefaults() {
viper.SetDefault("LogFile", "bingobot.log")
viper.SetDefault("LogDir", "log")
viper.SetDefault("LogMaxSizeMB", 500)
viper.SetDefault("LogMaxBackups", 3)
viper.SetDefault("LogMaxAgeDays", 365)
viper.SetDefault("LogCompression", false)
}