From f11b9b1d9384f5f3f8ffbd1bfa81777c8969b643 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Mon, 10 Jul 2023 14:55:25 +0200 Subject: [PATCH] Adds logger config --- README.md | 4 +- cmd/birthdaybot/main.go | 7 +++- example-config.yml | 4 ++ model/config.go | 82 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 91 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f6698a7..de3302f 100644 --- a/README.md +++ b/README.md @@ -33,10 +33,10 @@ $ make run - [X] Create the bot scaffold - [X] Define how to read the birthdays info - [X] Add a logger instance to the config -- [ ] Add config validation on server creation +- [X] Add config validation on server creation - [X] Add options to the server - [X] Pass logger to the server through an option -- [ ] Configure logger through config (levels and such) +- [X] Configure logger through config (levels and such) - [ ] Reduce logger verbosity (through levels) - [ ] Create a configurable template to fill with each notification - [ ] Create different message systems to use with the bot diff --git a/cmd/birthdaybot/main.go b/cmd/birthdaybot/main.go index 63345cf..dbbe9cc 100644 --- a/cmd/birthdaybot/main.go +++ b/cmd/birthdaybot/main.go @@ -13,8 +13,11 @@ import ( ) func loggerFromConfig(config *model.Config) *log.Logger { - // ToDo: apply config to logger - return log.New(os.Stderr) + logger := log.New(os.Stderr) + logger.SetReportCaller(config.Logger.ReportCaller) + logger.SetLevel(log.ParseLevel(config.Logger.Level)) + + return logger } func main() { diff --git a/example-config.yml b/example-config.yml index a5dbf80..b9eb1d7 100644 --- a/example-config.yml +++ b/example-config.yml @@ -1,6 +1,10 @@ --- birthday_file: birthdays.csv +logger: + level: debug + report_caller: true + telegram_notifications: bot_token: "123456789:YOUR_BOT_TOKEN" channel_id: "-100012345" diff --git a/model/config.go b/model/config.go index b43191b..d4b9d09 100644 --- a/model/config.go +++ b/model/config.go @@ -1,23 +1,99 @@ package model import ( + "errors" + "fmt" "io/ioutil" + "strings" "gopkg.in/yaml.v3" ) +var ( + ErrConfigBirthdayFileEmpty = errors.New("birthday file cannot be empty") + ErrLoggerConfigBadLevel = errors.New("logger level needs to be one of debug, info, warn, error, fatal or left empty") + ErrTelegramNotificationsConfigEmptyBotToken = errors.New("bot token cannot be empty") + ErrTelegramNotificationsConfigEmptyChannelID = errors.New("channel ID cannot be empty") +) + type Config struct { BirthdayFile string `yaml:"birthday_file"` + Logger *LoggerConfig `yaml:"logger"` TelegramNotifications *TelegramNotificationsConfig `yaml:"telegram_notifications"` } +// ToDo: to be implemented +func (c *Config) IsValid() error { + if c.BirthdayFile == "" { + return ErrConfigBirthdayFileEmpty + } + + if err := c.Logger.IsValid(); err != nil { + return fmt.Errorf("invalid logger config: %w", err) + } + + if c.TelegramNotifications != nil { + if err := c.TelegramNotifications.IsValid(); err != nil { + return fmt.Errorf("invalid telegram notifications config: %w", err) + } + } + + return nil +} + +func (c *Config) SetDefaults() { + if c.Logger == nil { + c.Logger = &LoggerConfig{} + } + + c.Logger.SetDefaults() + + if c.TelegramNotifications != nil { + c.TelegramNotifications.SetDefaults() + } +} + +type LoggerConfig struct { + Level string `yaml:"level"` + ReportCaller bool `yaml:"report_caller"` +} + +func (lc *LoggerConfig) SetDefaults() {} + +func (lc *LoggerConfig) IsValid() error { + level := strings.TrimSpace(strings.ToLower(lc.Level)) + + found := false + for _, ll := range []string{"debug", "info", "warn", "error", "fatal", ""} { + if level == ll { + found = true + break + } + } + + if !found { + return ErrLoggerConfigBadLevel + } + + return nil +} + type TelegramNotificationsConfig struct { BotToken string `yaml:"bot_token"` ChannelID string `yaml:"channel_id"` } -// ToDo: to be implemented -func (c *Config) IsValid() error { +func (tnc *TelegramNotificationsConfig) SetDefaults() {} + +func (tnc *TelegramNotificationsConfig) IsValid() error { + if tnc.BotToken == "" { + return ErrTelegramNotificationsConfigEmptyBotToken + } + + if tnc.ChannelID == "" { + return ErrTelegramNotificationsConfigEmptyChannelID + } + return nil } @@ -32,5 +108,7 @@ func ReadConfig(path string) (*Config, error) { return nil, err } + config.SetDefaults() + return config, nil }