1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2025-05-29 18:05:36 +02:00

WIP: Logging

This commit is contained in:
Philipp Heckel 2022-05-29 22:14:14 -04:00
parent 1f38a4a531
commit dc0e699fb5
8 changed files with 184 additions and 71 deletions

View file

@ -3,6 +3,7 @@ package cmd
import (
"github.com/urfave/cli/v2"
"heckel.io/ntfy/log"
"os"
)
@ -13,6 +14,11 @@ const (
var commands = make([]*cli.Command, 0)
var flagsDefault = []cli.Flag{
&cli.BoolFlag{Name: "debug", Aliases: []string{"d"}, EnvVars: []string{"NTFY_DEBUG"}, Usage: "enable debug logging"},
&cli.StringFlag{Name: "log-level", Aliases: []string{"log_level"}, Value: log.InfoLevel.String(), EnvVars: []string{"NTFY_LOG_LEVEL"}, Usage: "set log level"},
}
// New creates a new CLI application
func New() *cli.App {
return &cli.App{
@ -25,5 +31,23 @@ func New() *cli.App {
Writer: os.Stdout,
ErrWriter: os.Stderr,
Commands: commands,
Flags: flagsDefault,
Before: initLogFunc(nil),
}
}
func initLogFunc(next cli.BeforeFunc) cli.BeforeFunc {
return func(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.ToLevel(c.String("log-level")))
}
if next != nil {
if err := next(c); err != nil {
return err
}
}
return nil
}
}