forked from aadidenko/evloghook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
74 lines (63 loc) · 1.55 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// +build windows
// Package evloghook to send logs via Windwows Event Log
package evloghook
import (
"golang.org/x/sys/windows/svc/eventlog"
"github.com/sirupsen/logrus"
)
const levels = eventlog.Error | eventlog.Warning | eventlog.Info
// EventLogHook to send logs via Windwows Event Log.
type EventLogHook struct {
name string
}
// NewEventLogHook creates a hook to be added to an instance of logger
func NewEventLogHook(name string) (*EventLogHook, error) {
l, err := eventlog.Open(name)
if err != nil {
return nil, err
}
defer l.Close()
return &EventLogHook{
name: name,
}, nil
}
// Fire is called when a log event is fired.
func (h *EventLogHook) Fire(entry *logrus.Entry) error {
logger, err := eventlog.Open(h.name)
if err != nil {
return err
}
defer logger.Close()
const eventID = 1001
message, err := entry.String()
if err != nil {
return err
}
switch entry.Level {
case logrus.PanicLevel:
return logger.Error(eventID+2, message)
case logrus.FatalLevel:
return logger.Error(eventID+1, message)
case logrus.ErrorLevel:
return logger.Error(eventID, message)
case logrus.WarnLevel:
return logger.Warning(eventID, message)
case logrus.InfoLevel:
return logger.Info(eventID, message)
case logrus.DebugLevel:
return logger.Info(eventID+1, message)
default:
return nil
}
}
// Levels returns the available logging levels.
func (h *EventLogHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
}
}