Add logger to the server

This commit is contained in:
Miguel de la Cruz 2023-06-30 10:38:29 +02:00
parent acac38d1f6
commit af92353a89
3 changed files with 26 additions and 2 deletions

View file

@ -9,7 +9,7 @@ information of the subject so you can reach them easily.
- [ ] Create the bot scaffold - [ ] Create the bot scaffold
- [X] Define how to read the birthdays info - [X] Define how to read the birthdays info
- [ ] Add a logger instance to the config - [X] Add a logger instance to the config
- [ ] Add config validation on server creation - [ ] Add config validation on server creation
- [ ] 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

@ -2,7 +2,10 @@ package main
import ( import (
"flag" "flag"
"os"
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
"git.ctrlz.es/mgdelacroix/birthdaybot/server"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
) )
@ -10,5 +13,15 @@ func main() {
configFlag := flag.String("config", "birthdaybot.yml", "path to the configuration file") configFlag := flag.String("config", "birthdaybot.yml", "path to the configuration file")
flag.Parse() flag.Parse()
log.Info("Starting, with config file", "config", *configFlag) config, err := model.ReadConfig(*configFlag)
if err != nil {
log.Error("error reading config", "configPath", *configFlag, "error", err)
os.Exit(1)
}
_, err = server.New(config)
if err != nil {
log.Error("error creating server", "error", err)
os.Exit(1)
}
} }

View file

@ -2,23 +2,34 @@ package server
import ( import (
"fmt" "fmt"
"os"
"git.ctrlz.es/mgdelacroix/birthdaybot/model" "git.ctrlz.es/mgdelacroix/birthdaybot/model"
"git.ctrlz.es/mgdelacroix/birthdaybot/parser" "git.ctrlz.es/mgdelacroix/birthdaybot/parser"
"github.com/charmbracelet/log"
) )
type Server struct { type Server struct {
logger *log.Logger
config *model.Config config *model.Config
birthdays []*model.Birthday birthdays []*model.Birthday
} }
func New(config *model.Config) (*Server, error) { func New(config *model.Config) (*Server, error) {
logger := log.New(os.Stderr)
logger.Info("Parsing CSV file", "birthdayFile", config.BirthdayFile)
birthdays, err := parser.ParseCSV(config.BirthdayFile) birthdays, err := parser.ParseCSV(config.BirthdayFile)
if err != nil { if err != nil {
logger.Error("cannot parse CSV file", "birthdayFile", config.BirthdayFile, "error", err)
return nil, fmt.Errorf("cannot parse CSV file %s: %w", config.BirthdayFile, err) return nil, fmt.Errorf("cannot parse CSV file %s: %w", config.BirthdayFile, err)
} }
logger.Info("creating server")
server := &Server{ server := &Server{
logger: logger,
config: config, config: config,
birthdays: birthdays, birthdays: birthdays,
} }