Skip to content

Commit

Permalink
Re-work & reduce logging
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2142 committed May 28, 2024
1 parent 2487e7e commit ecdc875
Show file tree
Hide file tree
Showing 17 changed files with 338 additions and 133 deletions.
90 changes: 47 additions & 43 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package config
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/0x2142/frigate-notify/models"
"github.com/0x2142/frigate-notify/util"
"github.com/kkyr/fig"
"github.com/rs/zerolog/log"
)

type Config struct {
Expand Down Expand Up @@ -144,13 +144,15 @@ func LoadConfig(configFile string) {
}

// Load Config file
log.Print("Loading config file: ", configFile)
log.Debug().Msgf("Loading config file: %v", configFile)

err := fig.Load(&ConfigData, fig.File(filepath.Base(configFile)), fig.Dirs(filepath.Dir(configFile)), fig.UseEnv("FN"))
if err != nil {
log.Fatal("Failed to load config file! Error: ", err)
log.Fatal().
Err(err).
Msg("Failed to load config file!")
}
log.Print("Config file loaded.")
log.Info().Msg("Config file loaded.")

// Send config file to validation before completing
validateConfig()
Expand All @@ -159,7 +161,7 @@ func LoadConfig(configFile string) {
// validateConfig checks config file structure & loads info into associated packages
func validateConfig() {
var configErrors []string
log.Println("Validating config file...")
log.Debug().Msg("Validating config file...")

if (ConfigData.Frigate.WebAPI.Enabled && ConfigData.Frigate.MQTT.Enabled) || (!ConfigData.Frigate.WebAPI.Enabled && !ConfigData.Frigate.MQTT.Enabled) {
configErrors = append(configErrors, "Please configure only one polling method: Frigate Web API or MQTT")
Expand All @@ -172,31 +174,33 @@ func validateConfig() {

// Warn on test mode being enabled
if ConfigData.Frigate.WebAPI.Enabled && ConfigData.Frigate.WebAPI.TestMode {
log.Print("~~~~~~~~~~~~~~~~~~~")
log.Print("WARNING: Test Mode is enabled.")
log.Print("This is intended for development only & will only query Frigate for the last event.")
log.Print("Do not enable this in production! App will not accurately check for events.")
log.Print("~~~~~~~~~~~~~~~~~~~")
log.Warn().Msg("~~~~~~~~~~~~~~~~~~~")
log.Warn().Msg("WARNING: Test Mode is enabled.")
log.Warn().Msg("This is intended for development only & will only query Frigate for the last event.")
log.Warn().Msg("Do not enable this in production! App will not accurately check for events.")
log.Warn().Msg("~~~~~~~~~~~~~~~~~~~")
}

// Check if Frigate server URL contains protocol, assume HTTP if not specified
if !strings.Contains(ConfigData.Frigate.Server, "http://") && !strings.Contains(ConfigData.Frigate.Server, "https://") {
log.Println("No protocol specified on Frigate Server. Assuming http://. If this is incorrect, please adjust the config file.")
log.Warn().Msg("No protocol specified on Frigate Server. Assuming http://. If this is incorrect, please adjust the config file.")
ConfigData.Frigate.Server = fmt.Sprintf("http://%s", ConfigData.Frigate.Server)
}

// Test connectivity to Frigate
log.Print("Checking connection to Frigate server...")
log.Debug().Msg("Checking connection to Frigate server...")
statsAPI := fmt.Sprintf("%s/api/stats", ConfigData.Frigate.Server)
response, err := util.HTTPGet(statsAPI, ConfigData.Frigate.Insecure)
if err != nil {
log.Fatalf("Cannot reach Frigate server at %v, error: %v", ConfigData.Frigate.Server, err)
log.Fatal().
Err(err).
Msgf("Cannot reach Frigate server at %v", ConfigData.Frigate.Server)
}
var stats models.FrigateStats
json.Unmarshal([]byte(response), &stats)
log.Printf("Successfully connected to %v", ConfigData.Frigate.Server)
log.Info().Msgf("Successfully connected to %v", ConfigData.Frigate.Server)
if stats.Service.Version != "" {
log.Printf("Frigate server is running version %v", stats.Service.Version)
log.Debug().Msgf("Frigate server is running version %v", stats.Service.Version)
}

// Check Public / External URL if set
Expand All @@ -208,15 +212,15 @@ func validateConfig() {

// Check for camera exclusions
if len(ConfigData.Frigate.Cameras.Exclude) > 0 {
log.Println("Cameras to exclude from alerting:")
log.Debug().Msg("Cameras to exclude from alerting:")
for _, c := range ConfigData.Frigate.Cameras.Exclude {
log.Println(" -", c)
log.Debug().Msgf(" - %v", c)
}
}

// Check MQTT Config
if ConfigData.Frigate.MQTT.Enabled {
log.Println("MQTT Enabled.")
log.Debug().Msg("MQTT Enabled.")
if ConfigData.Frigate.MQTT.Server == "" {
configErrors = append(configErrors, "No MQTT server address specified!")
}
Expand All @@ -232,56 +236,56 @@ func validateConfig() {
if strings.ToLower(ConfigData.Alerts.Zones.Unzoned) != "allow" && strings.ToLower(ConfigData.Alerts.Zones.Unzoned) != "drop" {
configErrors = append(configErrors, "Option for unzoned events must be 'allow' or 'drop'")
} else {
log.Println("Events outside a zone:", strings.ToLower(ConfigData.Alerts.Zones.Unzoned))
log.Debug().Msgf("Events outside a zone: %v", strings.ToLower(ConfigData.Alerts.Zones.Unzoned))
}

if len(ConfigData.Alerts.Zones.Allow) > 0 {
log.Println("Zones to generate alerts for:")
log.Debug().Msg("Zones to generate alerts for:")
for _, c := range ConfigData.Alerts.Zones.Allow {
log.Println(" -", c)
log.Debug().Msgf(" - %v", c)
}
} else {
log.Println("All zones included in alerts")
log.Debug().Msg("All zones included in alerts")
}
if len(ConfigData.Alerts.Zones.Block) > 0 {
log.Println("Zones to exclude from alerting:")
log.Debug().Msg("Zones to exclude from alerting:")
for _, c := range ConfigData.Alerts.Zones.Block {
log.Println(" -", c)
log.Debug().Msgf(" - %v", c)
}
} else {
log.Println("No zones excluded")
log.Debug().Msg("No zones excluded")
}

// Check Label filtering config
if len(ConfigData.Alerts.Labels.Allow) > 0 {
log.Println("Labels to generate alerts for:")
log.Debug().Msg("Labels to generate alerts for:")
for _, c := range ConfigData.Alerts.Labels.Allow {
log.Println(" -", c)
log.Debug().Msgf(" - %v", c)
}
} else {
log.Println("All labels included in alerts")
log.Debug().Msg("All labels included in alerts")
}
if len(ConfigData.Alerts.Labels.Block) > 0 {
log.Println("Labels to exclude from alerting:")
log.Debug().Msg("Labels to exclude from alerting:")
for _, c := range ConfigData.Alerts.Labels.Block {
log.Println(" -", c)
log.Debug().Msgf(" - %v", c)
}
} else {
log.Println("No labels excluded")
log.Debug().Msg("No labels excluded")
}

// Check / Load alerting configuration
if ConfigData.Alerts.Discord.Enabled {
log.Print("Discord alerting enabled.")
log.Debug().Msg("Discord alerting enabled.")
if ConfigData.Alerts.Discord.Webhook == "" {
configErrors = append(configErrors, "No Discord webhook specified!")
}
}
if ConfigData.Alerts.Gotify.Enabled {
log.Print("Gotify alerting enabled.")
log.Debug().Msg("Gotify alerting enabled.")
// Check if Gotify server URL contains protocol, assume HTTP if not specified
if !strings.Contains(ConfigData.Alerts.Gotify.Server, "http://") && !strings.Contains(ConfigData.Alerts.Gotify.Server, "https://") {
log.Println("No protocol specified on Gotify Server. Assuming http://. If this is incorrect, please adjust the config file.")
log.Debug().Msg("No protocol specified on Gotify Server. Assuming http://. If this is incorrect, please adjust the config file.")
ConfigData.Alerts.Gotify.Server = fmt.Sprintf("http://%s", ConfigData.Alerts.Gotify.Server)
}
if ConfigData.Alerts.Gotify.Server == "" {
Expand All @@ -292,7 +296,7 @@ func validateConfig() {
}
}
if ConfigData.Alerts.SMTP.Enabled {
log.Print("SMTP alerting enabled.")
log.Debug().Msg("SMTP alerting enabled.")
if ConfigData.Alerts.SMTP.Server == "" {
configErrors = append(configErrors, "No SMTP server specified!")
}
Expand All @@ -307,7 +311,7 @@ func validateConfig() {
}
}
if ConfigData.Alerts.Telegram.Enabled {
log.Print("Telegram alerting enabled.")
log.Debug().Msg("Telegram alerting enabled.")
if ConfigData.Alerts.Telegram.ChatID == 0 {
configErrors = append(configErrors, "No Telegram Chat ID specified!")
}
Expand All @@ -316,7 +320,7 @@ func validateConfig() {
}
}
if ConfigData.Alerts.Pushover.Enabled {
log.Print("Pushover alerting enabled.")
log.Debug().Msg("Pushover alerting enabled.")
if ConfigData.Alerts.Pushover.Token == "" {
configErrors = append(configErrors, "No Pushover API token specified!")
}
Expand All @@ -340,7 +344,7 @@ func validateConfig() {
}
}
if ConfigData.Alerts.Nfty.Enabled {
log.Print("Nfty alerting enabled.")
log.Debug().Msg("Nfty alerting enabled.")
if ConfigData.Alerts.Nfty.Server == "" {
configErrors = append(configErrors, "No Nfty server specified!")
}
Expand All @@ -352,7 +356,7 @@ func validateConfig() {

// Validate monitoring config
if ConfigData.Monitor.Enabled {
log.Println("App monitoring enabled.")
log.Debug().Msg("App monitoring enabled.")
if ConfigData.Monitor.URL == "" {
configErrors = append(configErrors, "App monitor enabled but no URL specified!")
}
Expand All @@ -363,13 +367,13 @@ func validateConfig() {

if len(configErrors) > 0 {
fmt.Println()
log.Println("Config validation failed:")
log.Error().Msg("Config validation failed:")
for _, msg := range configErrors {
log.Println(" -", msg)
log.Error().Msgf(" - %v", msg)
}
fmt.Println()
log.Fatal("Please fix config errors before restarting app.")
log.Fatal().Msg("Please fix config errors before restarting app.")
} else {
log.Println("Config file validated!")
log.Info().Msg("Config file validated!")
}
}
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [v0.3.0](https://github.com/0x2142/frigate-notify/releases/tag/v0.2.8) - Upcoming Release

- Reworked & reduced logging.

## [v0.2.8](https://github.com/0x2142/frigate-notify/releases/tag/v0.2.8) - May 15 2024

- Add support for notifications via [Nfty](https://frigate-notify.0x2142.com/config/#nfty)
Expand Down
Loading

0 comments on commit ecdc875

Please sign in to comment.