-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin_writer_hook.go
46 lines (40 loc) · 939 Bytes
/
builtin_writer_hook.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
package main
import (
"os"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/writer"
)
func main() {
fdNormal, err := os.OpenFile("/tmp/service.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
// normal log will be written into service.log
logrus.AddHook(
&writer.Hook{
Writer: fdNormal,
LogLevels: []logrus.Level{
logrus.InfoLevel,
logrus.DebugLevel,
},
},
)
fdError, err := os.OpenFile("/tmp/service-err.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
// error log will be written into service-err.log
logrus.AddHook(
&writer.Hook{
Writer: fdError,
LogLevels: []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
},
},
)
logrus.Info("this log message will be found in service.log")
logrus.Error("this log message will be found in service-err.log")
}