Made the notification level flag global for all notification types.

This commit is contained in:
Fabrizio Steiner 2018-03-02 13:08:40 +01:00
parent 4ac08dfb30
commit e1ead2f46f
5 changed files with 26 additions and 24 deletions

View file

@ -191,6 +191,10 @@ The types of notifications to send are passed via the comma-separated option `--
* `email` to send notifications via e-mail * `email` to send notifications via e-mail
* `slack` to send notifications through a Slack webhook * `slack` to send notifications through a Slack webhook
### Settings
* `--notifications-level` (env. `WATCHTOWER_NOTIFICATIONS_LEVEL`): Controls the log level which is used for the notifications. If omitted, the default log level is `info`. Possible values are: `panic`, `fatal`, `error`, `warn`, `info` or `debug`.
### Notifications via E-Mail ### Notifications via E-Mail
To receive notifications by email, the following command-line options, or their corresponding environment variables, can be set: To receive notifications by email, the following command-line options, or their corresponding environment variables, can be set:

12
main.go
View file

@ -96,6 +96,12 @@ func main() {
Usage: "notification types to send (valid: email, slack)", Usage: "notification types to send (valid: email, slack)",
EnvVar: "WATCHTOWER_NOTIFICATIONS", EnvVar: "WATCHTOWER_NOTIFICATIONS",
}, },
cli.StringFlag{
Name: "notifications-level",
Usage: "The log level used for sending notifications. Possible values: \"panic\", \"fatal\", \"error\", \"warn\", \"info\" or \"debug\"",
EnvVar: "WATCHTOWER_NOTIFICATIONS_LEVEL",
Value: "info",
},
cli.StringFlag{ cli.StringFlag{
Name: "notification-email-from", Name: "notification-email-from",
Usage: "Address to send notification e-mails from", Usage: "Address to send notification e-mails from",
@ -147,12 +153,6 @@ func main() {
EnvVar: "WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER", EnvVar: "WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER",
Value: "watchtower", Value: "watchtower",
}, },
cli.StringFlag{
Name: "notification-slack-level",
Usage: "The log level used for sending notifications through Slack. Default if omitted is \"info\". Possible values: \"panic\",\"fatal\",\"error\",\"warn\",\"info\" or \"debug\"",
EnvVar: "WATCHTOWER_NOTIFICATION_SLACK_LEVEL",
Value: "info",
},
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {

View file

@ -28,9 +28,10 @@ type emailTypeNotifier struct {
Port int Port int
tlsSkipVerify bool tlsSkipVerify bool
entries []*log.Entry entries []*log.Entry
logLevels []log.Level
} }
func newEmailNotifier(c *cli.Context) typeNotifier { func newEmailNotifier(c *cli.Context, acceptedLogLevels []log.Level) typeNotifier {
n := &emailTypeNotifier{ n := &emailTypeNotifier{
From: c.GlobalString("notification-email-from"), From: c.GlobalString("notification-email-from"),
To: c.GlobalString("notification-email-to"), To: c.GlobalString("notification-email-to"),
@ -39,6 +40,7 @@ func newEmailNotifier(c *cli.Context) typeNotifier {
Password: c.GlobalString("notification-email-server-password"), Password: c.GlobalString("notification-email-server-password"),
Port: c.GlobalInt("notification-email-server-port"), Port: c.GlobalInt("notification-email-server-port"),
tlsSkipVerify: c.GlobalBool("notification-email-server-tls-skip-verify"), tlsSkipVerify: c.GlobalBool("notification-email-server-tls-skip-verify"),
logLevels: acceptedLogLevels,
} }
log.AddHook(n) log.AddHook(n)
@ -112,14 +114,7 @@ func (e *emailTypeNotifier) SendNotification() {
} }
func (e *emailTypeNotifier) Levels() []log.Level { func (e *emailTypeNotifier) Levels() []log.Level {
// TODO: Make this configurable. return e.logLevels
return []log.Level{
log.PanicLevel,
log.FatalLevel,
log.ErrorLevel,
log.WarnLevel,
log.InfoLevel,
}
} }
func (e *emailTypeNotifier) Fire(entry *log.Entry) error { func (e *emailTypeNotifier) Fire(entry *log.Entry) error {

View file

@ -1,6 +1,7 @@
package notifications package notifications
import ( import (
"github.com/johntdyer/slackrus"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -19,15 +20,22 @@ type Notifier struct {
func NewNotifier(c *cli.Context) *Notifier { func NewNotifier(c *cli.Context) *Notifier {
n := &Notifier{} n := &Notifier{}
logLevel, err := log.ParseLevel(c.GlobalString("notifications-level"))
if err != nil {
log.Fatalf("Notifications invalid log level: %s", err.Error())
}
acceptedLogLevels := slackrus.LevelThreshold(logLevel)
// Parse types and create notifiers. // Parse types and create notifiers.
types := c.GlobalStringSlice("notifications") types := c.GlobalStringSlice("notifications")
for _, t := range types { for _, t := range types {
var tn typeNotifier var tn typeNotifier
switch t { switch t {
case emailType: case emailType:
tn = newEmailNotifier(c) tn = newEmailNotifier(c, acceptedLogLevels)
case slackType: case slackType:
tn = newSlackNotifier(c) tn = newSlackNotifier(c, acceptedLogLevels)
default: default:
log.Fatalf("Unknown notification type %q", t) log.Fatalf("Unknown notification type %q", t)
} }

View file

@ -14,17 +14,12 @@ type slackTypeNotifier struct {
slackrus.SlackrusHook slackrus.SlackrusHook
} }
func newSlackNotifier(c *cli.Context) typeNotifier { func newSlackNotifier(c *cli.Context, acceptedLogLevels []log.Level) typeNotifier {
logLevel, err := log.ParseLevel(c.GlobalString("notification-slack-level"))
if err != nil {
log.Fatalf("Slack notifications: %s", err.Error())
}
n := &slackTypeNotifier{ n := &slackTypeNotifier{
SlackrusHook: slackrus.SlackrusHook{ SlackrusHook: slackrus.SlackrusHook{
HookURL: c.GlobalString("notification-slack-hook-url"), HookURL: c.GlobalString("notification-slack-hook-url"),
Username: c.GlobalString("notification-slack-identifier"), Username: c.GlobalString("notification-slack-identifier"),
AcceptedLevels: slackrus.LevelThreshold(logLevel), AcceptedLevels: acceptedLogLevels,
}, },
} }