Implemented proper logging and added more log messages

This commit is contained in:
ChaoticByte 2023-10-14 10:41:22 +02:00
parent 5a90f9736b
commit 9534dc3492
7 changed files with 89 additions and 23 deletions

31
main.go
View file

@ -9,6 +9,8 @@ import (
"time"
)
var logger Logger
func main() {
// get cli arguments
args := os.Args
@ -17,8 +19,11 @@ func main() {
os.Exit(1)
}
configFilePath := os.Args[1]
// create logger
logger = NewLogger(2)
// init
println("INFO\tInitializing ...")
logger.info("Initializing ...")
defer logger.info("Exiting ...")
config := NewDataStore(
configFilePath,
NewConfig(),
@ -30,15 +35,17 @@ func main() {
NewPersistentData(config),
false,
0640)
// exit handler
defer println("INFO\tExiting ...")
// check config
logger.LogLevel = config.LogLevel
logger.debug("Checking configuration file ...")
checkConfig(config)
// create mail template from mail template config
logger.debug("Parsing mail template ...")
if config.Template.SubjectTemplate == "" {
logger.debug("Using default template for mail subject")
config.Template.SubjectTemplate = DEFAULT_SUBJECT_TEMPLATE
}
if config.Template.BodyTemplate == "" {
logger.debug("Using default template for mail body")
config.Template.BodyTemplate = DEFAULT_BODY_TEMPLATE
}
mailTemplate := NewTemplateFromTemplateConfig(config.Template)
@ -54,42 +61,48 @@ func main() {
for _, a := range apiEndpoints {
for _, b := range config.EnabledApiEndpoints {
if a.Id == b {
logger.debug("Endpoint '" + b + "' is enabled")
enabledApiEndpoints = append(enabledApiEndpoints, a)
}
}
}
// main loop
logger.debug("Entering main loop ...")
for {
t1 := time.Now().UnixMilli()
newNotices := []WidNotice{}
for _, a := range enabledApiEndpoints {
fmt.Printf("INFO\t%v Querying endpoint '%v' for new notices ...\n", time.Now().Format(time.RFC3339Nano), a.Id)
logger.info("Querying endpoint '" + a.Id + "' for new notices ...")
n, t, err := a.getNotices(persistent.data.(PersistentData).LastPublished[a.Id])
if err != nil {
// retry
logger.warn("Couldn't query notices from API endpoint '" + a.Id + "'. Retrying ...")
logger.warn(err)
n, t, err = a.getNotices(persistent.data.(PersistentData).LastPublished[a.Id])
}
if err != nil {
// ok then...
fmt.Print("ERROR\t", err)
logger.error("Couldn't query notices from API endpoint '" + a.Id + "'")
logger.error(err)
} else {
newNotices = append(newNotices, n...)
persistent.data.(PersistentData).LastPublished[a.Id] = t
persistent.save()
}
}
logger.debug(fmt.Sprintf("Got %v new notices", len(newNotices)))
if len(newNotices) > 0 {
fmt.Printf("INFO\t%v Sending email notifications ...\n", time.Now().Format(time.RFC3339Nano))
logger.info("Sending email notifications ...")
recipientsNotified := 0
for _, r := range config.Recipients {
err := r.filterAndSendNotices(newNotices, mailTemplate, mailAuth, config.SmtpConfiguration)
if err != nil {
fmt.Printf("ERROR\t%v\n", err)
logger.error(err)
} else {
recipientsNotified++
}
}
fmt.Printf("INFO\t%v Email notifications sent to %v of %v recipients.\n", time.Now().Format(time.RFC3339Nano), recipientsNotified, len(config.Recipients))
logger.info(fmt.Sprintf("Email notifications sent to %v of %v recipients", recipientsNotified, len(config.Recipients)))
}
t2 := time.Now().UnixMilli()
dt := int(t2 - t1)