-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsyslog.go
More file actions
114 lines (97 loc) · 2.28 KB
/
syslog.go
File metadata and controls
114 lines (97 loc) · 2.28 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
syslog "gopkg.in/mcuadros/go-syslog.v2"
"os"
)
type Syslog struct {
Path string
Filter struct {
Facility string
facility int
Severity string
severity int
}
Output struct {
Template string
}
}
var SyslogFacilityMap = map[string]int{
"kern": 0,
"user": 1,
"mail": 2,
"daemon": 3,
"auth": 4,
"syslog": 5,
"lpr": 6,
"news": 7,
"uucp": 8,
"cron": 9,
"authpriv": 10,
"ftp": 11,
"ntp": 12,
"security": 13,
"console": 14,
"solaris-cron": 15,
"local0": 16,
"local1": 17,
"local2": 18,
"local3": 19,
"local4": 20,
"local5": 21,
"local6": 22,
"local7": 23,
}
var SyslogPriorityMap = map[string]int{
"emerg": 0,
"emergency": 0,
"alert": 1,
"crit": 2,
"critical": 2,
"err": 3,
"error": 3,
"warn": 4,
"warning": 4,
"notice": 5,
"info": 6,
"dbg": 7,
"debug": 7,
}
func handleSyslog() {
LoggerStdout.Verbose(fmt.Sprintf(" -> starting syslog daemon (%s)", configuration.Syslog.Path))
// Check if syslog path exists, remove if already existing
_, err := os.Stat(configuration.Syslog.Path)
if err == nil {
os.Remove(configuration.Syslog.Path)
}
channel := make(syslog.LogPartsChannel)
handler := syslog.NewChannelHandler(channel)
server := syslog.NewServer()
server.SetFormat(syslog.Automatic)
server.SetHandler(handler)
server.ListenUnixgram(configuration.Syslog.Path)
server.Boot()
go func(channel syslog.LogPartsChannel) {
for logParts := range channel {
facilityId := uint(logParts["facility"].(int))
severityId := uint(logParts["severity"].(int))
// facility filter
if hasBit(configuration.Syslog.Filter.facility, facilityId) == false {
continue
}
// severity filter
if hasBit(configuration.Syslog.Filter.severity, severityId) == false {
continue
}
//fmt.Println(logParts)
// build message
message := fmt.Sprintf("%s %s", logParts["hostname"], logParts["content"])
// custom template
if configuration.Syslog.Output.Template != "" {
message = fmt.Sprintf(configuration.Syslog.Output.Template, message)
}
LoggerStdout.Println(message)
}
}(channel)
server.Wait()
}