Skip to content
This repository was archived by the owner on Aug 14, 2022. It is now read-only.

Commit 9050ff4

Browse files
committed
Add log.level and log.json as cli flags
1 parent 956cec2 commit 9050ff4

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

main.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ import (
2424
const (
2525
storeBolt = "bolt"
2626
storeConsul = "consul"
27+
28+
levelDebug = "debug"
29+
levelInfo = "info"
30+
levelWarn = "warn"
31+
levelError = "error"
2732
)
2833

2934
var (
@@ -47,6 +52,8 @@ func main() {
4752
boltPath string
4853
consul *url.URL
4954
listenAddr string
55+
logLevel string
56+
logJSON bool
5057
store string
5158
telegramAdmin int
5259
telegramToken string
@@ -73,6 +80,15 @@ func main() {
7380
Envar("LISTEN_ADDR").
7481
StringVar(&config.listenAddr)
7582

83+
a.Flag("log.json", "Tell the application to log json and not key value pairs").
84+
Envar("LOG_JSON").
85+
BoolVar(&config.logJSON)
86+
87+
a.Flag("log.level", "The log level to use for filtering logs").
88+
Envar("LOG_LEVEL").
89+
Default(levelInfo).
90+
EnumVar(&config.logLevel, levelError, levelWarn, levelInfo, levelDebug)
91+
7692
a.Flag("store", "The store to use").
7793
Required().
7894
Envar("STORE").
@@ -95,8 +111,19 @@ func main() {
95111
os.Exit(2)
96112
}
97113

114+
levelFilter := map[string]level.Option{
115+
levelError: level.AllowError(),
116+
levelWarn: level.AllowWarn(),
117+
levelInfo: level.AllowInfo(),
118+
levelDebug: level.AllowDebug(),
119+
}
120+
98121
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
99-
logger = level.NewFilter(logger, level.AllowAll())
122+
if config.logJSON {
123+
logger = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
124+
}
125+
126+
logger = level.NewFilter(logger, levelFilter[config.logLevel])
100127
logger = log.With(logger,
101128
"ts", log.DefaultTimestampUTC,
102129
"caller", log.DefaultCaller,

0 commit comments

Comments
 (0)