...
1
2
3 package syslog
4
5 import (
6 "fmt"
7 "log/syslog"
8 "os"
9
10 "github.com/sirupsen/logrus"
11 )
12
13
14 type SyslogHook struct {
15 Writer *syslog.Writer
16 SyslogNetwork string
17 SyslogRaddr string
18 }
19
20
21
22
23 func NewSyslogHook(network, raddr string, priority syslog.Priority, tag string) (*SyslogHook, error) {
24 w, err := syslog.Dial(network, raddr, priority, tag)
25 return &SyslogHook{w, network, raddr}, err
26 }
27
28 func (hook *SyslogHook) Fire(entry *logrus.Entry) error {
29 line, err := entry.String()
30 if err != nil {
31 fmt.Fprintf(os.Stderr, "Unable to read entry, %v", err)
32 return err
33 }
34
35 switch entry.Level {
36 case logrus.PanicLevel:
37 return hook.Writer.Crit(line)
38 case logrus.FatalLevel:
39 return hook.Writer.Crit(line)
40 case logrus.ErrorLevel:
41 return hook.Writer.Err(line)
42 case logrus.WarnLevel:
43 return hook.Writer.Warning(line)
44 case logrus.InfoLevel:
45 return hook.Writer.Info(line)
46 case logrus.DebugLevel, logrus.TraceLevel:
47 return hook.Writer.Debug(line)
48 default:
49 return nil
50 }
51 }
52
53 func (hook *SyslogHook) Levels() []logrus.Level {
54 return logrus.AllLevels
55 }
56
View as plain text