-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogging.go
65 lines (56 loc) · 1.34 KB
/
logging.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
57
58
59
60
61
62
63
64
65
package main
import (
"io"
"io/ioutil"
"os"
"github.com/dcos/dcos-ui-update-service/config"
log "github.com/sirupsen/logrus"
)
type logWriterHook struct {
Writer io.Writer
LogLevels []log.Level
}
func initLogging(config *config.Config) {
setupSplitLogging()
// Set logging level
lvl, err := log.ParseLevel(config.LogLevel())
if err != nil {
log.Fatal(err)
}
log.SetLevel(lvl)
log.Infof("Logging set to: %s", config.LogLevel())
}
func setupSplitLogging() {
log.SetOutput(ioutil.Discard)
log.AddHook(&logWriterHook{ // Send logs with level higher than warning to stderr
Writer: os.Stderr,
LogLevels: []log.Level{
log.PanicLevel,
log.FatalLevel,
log.ErrorLevel,
log.WarnLevel,
},
})
log.AddHook(&logWriterHook{ // Send info and debug logs to stdout
Writer: os.Stdout,
LogLevels: []log.Level{
log.InfoLevel,
log.DebugLevel,
log.TraceLevel,
},
})
}
// Fire will be called when some logging function is called with current hook
// It will format log entry to string and write it to appropriate writer
func (hook *logWriterHook) Fire(entry *log.Entry) error {
line, err := entry.String()
if err != nil {
return err
}
_, err = hook.Writer.Write([]byte(line))
return err
}
// Levels define on which log levels this hook would trigger
func (hook *logWriterHook) Levels() []log.Level {
return hook.LogLevels
}