-
Notifications
You must be signed in to change notification settings - Fork 8
/
lager.go
52 lines (42 loc) · 1.12 KB
/
lager.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
package flag
import (
"fmt"
"io"
"os"
"code.cloudfoundry.org/lager/v3"
)
const (
LogLevelDebug = "debug"
LogLevelInfo = "info"
LogLevelError = "error"
LogLevelFatal = "fatal"
)
type Lager struct {
LogLevel string `long:"log-level" default:"info" choice:"debug" choice:"info" choice:"error" choice:"fatal" description:"Minimum level of logs to see."`
writerSink io.Writer
}
func (f *Lager) SetWriterSink(writer io.Writer) {
f.writerSink = writer
}
func (f Lager) 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))
}
logger := lager.NewLogger(component)
if f.writerSink == nil {
f.writerSink = os.Stdout
}
sink := lager.NewReconfigurableSink(lager.NewPrettySink(f.writerSink, lager.DEBUG), minLagerLogLevel)
logger.RegisterSink(sink)
return logger, sink
}