From 6353fc34c2e84512497bf2837fb4a0722989f52d Mon Sep 17 00:00:00 2001 From: Issif Date: Wed, 15 Apr 2020 11:59:28 +0200 Subject: [PATCH] add variable for disabling error logs in stdout --- README.md | 6 ++++-- config.go | 5 +++++ config_example.yaml | 5 +++-- main.go | 8 ++++++-- slack.go | 2 +- 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 80b2850..549805d 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,9 @@ Two methods are available for configuration and can be mixed : SlackWebhookURL: "" #Slack Webhook URL SlackIconURL: "" #Slack Icon (Avatar) URL SlackUsername: "" #Slack Username -Regexp: ".*\\.fr$" #Regexp to match. Can't be empty. It uses Golang regexp format. +Regexp: ".*\\.fr$" #Regexp to match. Can't be empty. It uses Golang regexp format Workers: 20 #Number of workers for consuming feed from CertStream +DisplayErrors: false #Enable/Disable display of errors in logs ``` ### With env vars @@ -35,8 +36,9 @@ Workers: 20 #Number of workers for consuming feed from CertStream - **SLACKWEBHOOKURL**: Slack Webhook URL - **SLACKICONURL**: Slack Icon (Avatar) URL - **SLACKUSERNAME**: Slack Username -- **REGEXP**: Regexp to match, if empty, '.*' is used. Use Golang regexp format. +- **REGEXP**: Regexp to match, if empty, '.*' is used. Use Golang regexp format - **WORKERS**: Number of workers for consuming feed from CertStream +- **DISPLAYERRORS**: Enable/Disable display of errors in logs ## Run diff --git a/config.go b/config.go index 777d759..085e203 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,7 @@ type configuration struct { SlackIconURL string SlackUsername string Regexp string + DisplayErrors string } func getConfig() *configuration { @@ -31,6 +32,7 @@ func getConfig() *configuration { v.SetDefault("SlackUsername", "Cercat") v.SetDefault("Regexp", "") v.SetDefault("Workers", 20) + v.SetDefault("DisplayErrors", "false") if *configFile != "" { d, f := path.Split(*configFile) @@ -51,6 +53,9 @@ func getConfig() *configuration { if c.SlackUsername == "" { c.SlackUsername = "Cercat" } + if c.DisplayErrors == "" { + c.DisplayErrors = "false" + } if c.Regexp == "" { log.Println("[ERROR] : Regexp can't be empty") os.Exit(1) diff --git a/config_example.yaml b/config_example.yaml index f404817..87f2a04 100644 --- a/config_example.yaml +++ b/config_example.yaml @@ -2,5 +2,6 @@ SlackWebhookURL: "" #Slack Webhook URL SlackIconURL: "" #Slack Icon (Avatar) URL SlackUsername: "" #Slack Username -Regexp: ".*\\.fr$" #Regexp to match. Can't be empty. It uses Golang regexp format. -Workers: 20 #Number of workers for consuming stream from CertStream \ No newline at end of file +Regexp: ".*\\.fr$" #Regexp to match. Can't be empty. It uses Golang regexp format +Workers: 20 #Number of workers for consuming stream from CertStream +DisplayErrors: false #Enable/Disable display of errors in logs \ No newline at end of file diff --git a/main.go b/main.go index 104ec28..eb0d3a0 100644 --- a/main.go +++ b/main.go @@ -60,14 +60,18 @@ func main() { defer ws.Close() if err != nil { - log.Println("[INFO] : Error connecting to certstream! Sleeping a few seconds and reconnecting...") + if config.DisplayErrors == "true" { + log.Println("[ERROR] : Error connecting to certstream! Sleeping a few seconds and reconnecting...") + } time.Sleep(1 * time.Second) continue } for { _, msg, err := ws.ReadMessage() if err != nil { - log.Println("[ERROR] : Error reading message") + if config.DisplayErrors == "true" { + log.Println("[ERROR] : Error reading message from CertStream") + } break } msgChan <- msg diff --git a/slack.go b/slack.go index e84e4db..b18bb30 100644 --- a/slack.go +++ b/slack.go @@ -80,7 +80,7 @@ func (s slackPayload) Post() { req.Header.Add("Content-Type", "application/json") client := &http.Client{} _, err := client.Do(req) - if err != nil { + if err != nil && config.DisplayErrors == "true" { log.Println("[ERROR] : Slack Post error") } }