Adds logger config

This commit is contained in:
Miguel de la Cruz 2023-07-10 14:55:25 +02:00
parent e43cb05755
commit f11b9b1d93
4 changed files with 91 additions and 6 deletions

View file

@ -33,10 +33,10 @@ $ make run
- [X] Create the bot scaffold - [X] Create the bot scaffold
- [X] Define how to read the birthdays info - [X] Define how to read the birthdays info
- [X] Add a logger instance to the config - [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] Add options to the server
- [X] Pass logger to the server through an option - [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) - [ ] Reduce logger verbosity (through levels)
- [ ] Create a configurable template to fill with each notification - [ ] Create a configurable template to fill with each notification
- [ ] Create different message systems to use with the bot - [ ] Create different message systems to use with the bot

View file

@ -13,8 +13,11 @@ import (
) )
func loggerFromConfig(config *model.Config) *log.Logger { func loggerFromConfig(config *model.Config) *log.Logger {
// ToDo: apply config to logger logger := log.New(os.Stderr)
return log.New(os.Stderr) logger.SetReportCaller(config.Logger.ReportCaller)
logger.SetLevel(log.ParseLevel(config.Logger.Level))
return logger
} }
func main() { func main() {

View file

@ -1,6 +1,10 @@
--- ---
birthday_file: birthdays.csv birthday_file: birthdays.csv
logger:
level: debug
report_caller: true
telegram_notifications: telegram_notifications:
bot_token: "123456789:YOUR_BOT_TOKEN" bot_token: "123456789:YOUR_BOT_TOKEN"
channel_id: "-100012345" channel_id: "-100012345"

View file

@ -1,23 +1,99 @@
package model package model
import ( import (
"errors"
"fmt"
"io/ioutil" "io/ioutil"
"strings"
"gopkg.in/yaml.v3" "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 { type Config struct {
BirthdayFile string `yaml:"birthday_file"` BirthdayFile string `yaml:"birthday_file"`
Logger *LoggerConfig `yaml:"logger"`
TelegramNotifications *TelegramNotificationsConfig `yaml:"telegram_notifications"` 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 { type TelegramNotificationsConfig struct {
BotToken string `yaml:"bot_token"` BotToken string `yaml:"bot_token"`
ChannelID string `yaml:"channel_id"` ChannelID string `yaml:"channel_id"`
} }
// ToDo: to be implemented func (tnc *TelegramNotificationsConfig) SetDefaults() {}
func (c *Config) IsValid() error {
func (tnc *TelegramNotificationsConfig) IsValid() error {
if tnc.BotToken == "" {
return ErrTelegramNotificationsConfigEmptyBotToken
}
if tnc.ChannelID == "" {
return ErrTelegramNotificationsConfigEmptyChannelID
}
return nil return nil
} }
@ -32,5 +108,7 @@ func ReadConfig(path string) (*Config, error) {
return nil, err return nil, err
} }
config.SetDefaults()
return config, nil return config, nil
} }