/
lager_flag.go
56 lines (46 loc) · 1.53 KB
/
lager_flag.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 guardiancmd
import (
"fmt"
"os"
"code.cloudfoundry.org/lager/v3"
)
const (
LogLevelDebug = "debug"
LogLevelInfo = "info"
LogLevelError = "error"
LogLevelFatal = "fatal"
FormatUnixEpoch = "unix-epoch"
FormatRFC3339 = "rfc3339"
)
type LagerFlag struct {
//lint:ignore SA5008 github.com/jesse-vdk/go-flag requires duplicate struct tags for 'choice'
LogLevel string `long:"log-level" default:"info" choice:"debug" choice:"info" choice:"error" choice:"fatal" description:"Minimum level of logs to see."`
//lint:ignore SA5008 github.com/jesse-vdk/go-flag requires duplicate struct tags for 'choice'
TimeFormat string `long:"time-format" default:"unix-epoch" choice:"unix-epoch" choice:"rfc3339" description:"format of log timestamps."`
}
func (f LagerFlag) Logger(component string) (lager.Logger, *lager.ReconfigurableSink) {
var minLagerLogLevel lager.LogLevel
switch f.LogLevel {
case LogLevelDebug:
minLagerLogLevel = lager.DEBUG
case LogLevelInfo:
minLagerLogLevel = lager.INFO
case LogLevelError:
minLagerLogLevel = lager.ERROR
case LogLevelFatal:
minLagerLogLevel = lager.FATAL
default:
panic(fmt.Sprintf("unknown log level: %s", f.LogLevel))
}
var internalSink lager.Sink
switch f.TimeFormat {
case FormatRFC3339:
internalSink = lager.NewPrettySink(os.Stdout, lager.DEBUG)
default:
internalSink = lager.NewWriterSink(os.Stdout, lager.DEBUG)
}
logger := lager.NewLogger(component)
sink := lager.NewReconfigurableSink(internalSink, minLagerLogLevel)
logger.RegisterSink(sink)
return logger, sink
}