-
Notifications
You must be signed in to change notification settings - Fork 75
/
log_level.go
46 lines (41 loc) · 977 Bytes
/
log_level.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package cli_helpers
import (
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"os"
)
func SetupLogLevelOptions(app *cli.App) {
newFlags := []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "debug mode",
EnvVar: "DEBUG",
},
cli.StringFlag{
Name: "log-level, l",
Value: "info",
Usage: "Log level (options: debug, info, warn, error, fatal, panic)",
},
}
app.Flags = append(app.Flags, newFlags...)
appBefore := app.Before
// logs
app.Before = func(c *cli.Context) error {
log.SetOutput(os.Stderr)
level, err := log.ParseLevel(c.String("log-level"))
if err != nil {
log.Fatalf(err.Error())
}
log.SetLevel(level)
// If a log level wasn't specified and we are running in debug mode,
// enforce log-level=debug.
if !c.IsSet("log-level") && !c.IsSet("l") && c.Bool("debug") {
log.SetLevel(log.DebugLevel)
go watchForGoroutinesDump()
}
if appBefore != nil {
return appBefore(c)
}
return nil
}
}