mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-05-30 10:25:40 +02:00
Logging
This commit is contained in:
parent
bd865fd55d
commit
ab955d4d1c
15 changed files with 161 additions and 65 deletions
cmd
24
cmd/app.go
24
cmd/app.go
|
@ -3,6 +3,7 @@ package cmd
|
|||
|
||||
import (
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/urfave/cli/v2/altsrc"
|
||||
"heckel.io/ntfy/log"
|
||||
"os"
|
||||
)
|
||||
|
@ -16,7 +17,7 @@ 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"},
|
||||
altsrc.NewStringFlag(&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
|
||||
|
@ -32,22 +33,15 @@ func New() *cli.App {
|
|||
ErrWriter: os.Stderr,
|
||||
Commands: commands,
|
||||
Flags: flagsDefault,
|
||||
Before: initLogFunc(nil),
|
||||
Before: initLogFunc,
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
func initLogFunc(c *cli.Context) error {
|
||||
if c.Bool("debug") {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
} else {
|
||||
log.SetLevel(log.ToLevel(c.String("log-level")))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue