-
Notifications
You must be signed in to change notification settings - Fork 1
/
syslog.go
121 lines (104 loc) · 2.5 KB
/
syslog.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
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
115
116
117
118
119
120
121
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"
import (
"errors"
"os"
"strconv"
)
var (
devNull = &devnull{}
)
// Syslog describes a sysloger
type Syslog struct {
facility Priority
hostname string
pid string
appname string
channels []Channel
output *Sender
minSev int
}
// New create a new Syslog
func New(output *Sender, minPriority Priority, appname string) (syslog *Syslog, err error) {
hostname, err := os.Hostname()
if err != nil {
hostname = "-"
}
hostname = validHost(hostname)
if appname == "" {
err = errors.New("syslog.New needs a non empty appname")
return
}
appname = validApp(appname)
syslog = &Syslog{
facility: minPriority.Facility(),
hostname: hostname,
pid: strconv.Itoa(os.Getpid()),
appname: appname,
output: output,
minSev: int(minPriority.Severity()),
}
if syslog.pid == "" {
syslog.pid = "-"
}
return syslog, nil
}
// TestMode prefill hostname and pid.
// this is only for test purpose
func (syslog *Syslog) TestMode() {
syslog.hostname = "localhost"
syslog.pid = "1234"
}
// Channel expose a custom channel for a priority
func (syslog *Syslog) Channel(sev Priority) Channel {
if syslog.channels == nil {
syslog.channels = []Channel{
devNull, devNull, devNull, devNull,
devNull, devNull, devNull, devNull,
}
for sev := 0; sev <= syslog.minSev; sev++ {
syslog.channels[sev] = &trueChannel{msgChannel{
priority: syslog.facility | Priority(sev),
hostname: syslog.hostname,
pid: syslog.pid,
appname: syslog.appname,
msgid: "-",
output: syslog.output,
}}
}
}
return syslog.channels[sev.Severity()]
}
// SubSyslog create a syslog for a subAppName
// this allow to postfix to remplace the AppName with AppName/SubAppName
func (syslog *Syslog) SubSyslog(subAppName string) (sub *Syslog) {
var appname string
switch syslog.appname {
case "-":
appname = subAppName
default:
appname = syslog.appname + "/" + subAppName
}
sub = &Syslog{
facility: syslog.facility,
hostname: syslog.hostname,
pid: syslog.pid,
appname: appname,
output: syslog.output,
channels: []Channel{
devNull, devNull, devNull, devNull,
devNull, devNull, devNull, devNull,
},
minSev: syslog.minSev,
}
for sev := 0; sev <= syslog.minSev; sev++ {
sub.channels[sev] = &trueChannel{msgChannel{
priority: syslog.facility | Priority(sev),
hostname: syslog.hostname,
pid: syslog.pid,
appname: appname,
msgid: "-",
output: syslog.output,
}}
}
return
}