-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflags.go
56 lines (45 loc) · 1.35 KB
/
flags.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
47
48
49
50
51
52
53
54
55
56
package log
import (
"flag"
"net/url"
)
// AddFlags adds the flags used by this package to the given FlagSet. That's
// useful if working with a custom FlagSet. The init function of this package
// adds the flags to flag.CommandLine anyway. Thus, it's usually enough to call
// flag.Parse() to make the logging flags take effect.
func AddFlags(fs *flag.FlagSet) error {
fs.Var(
levelFlag("info"),
"log.level",
"Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, dpanic, panic, fatal]",
)
u, err := url.Parse(defaultLogFormatURI)
if err != nil {
return err
}
fs.Var(
logFormatFlag(*u),
"log.format",
`Set the log target and format. Example: "logger:console?disableCaller=false&development=true&outputPaths=stdout&errorOutputPaths=stderr" or "logger:json?disableStacktrace=true"`,
)
return nil
}
type levelFlag string
// String implements flag.Value interface.
func (f levelFlag) String() string {
return string(f)
}
// Set implements flag.Value interface.
func (f levelFlag) Set(level string) error {
return baseLoggerLevel.UnmarshalText([]byte(level))
}
type logFormatFlag url.URL
// String implements flag.Value.
func (f logFormatFlag) String() string {
u := url.URL(f)
return u.String()
}
// Set implements flag.Value.
func (f logFormatFlag) Set(format string) error {
return initGlobalLogger(format)
}