-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_logger.go
53 lines (44 loc) · 967 Bytes
/
system_logger.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
package log
import (
"github.com/ayufan/golang-kardianos-service"
"github.com/sirupsen/logrus"
)
type ServiceLogHook struct {
service.Logger
Level logrus.Level
}
func (s *ServiceLogHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
}
}
func (s *ServiceLogHook) Fire(entry *logrus.Entry) error {
if entry.Level > s.Level {
return nil
}
msg, err := entry.String()
if err != nil {
return err
}
switch entry.Level {
case logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel:
s.Error(msg)
case logrus.WarnLevel:
s.Warning(msg)
case logrus.InfoLevel:
s.Info(msg)
}
return nil
}
func SetSystemLogger(svc service.Service) {
logger, err := svc.SystemLogger(nil)
if err == nil {
logrus.AddHook(&ServiceLogHook{logger, logrus.GetLevel()})
} else {
logrus.WithError(err).Error("Error while setting up the system logger")
}
}