-
Notifications
You must be signed in to change notification settings - Fork 23
/
syslog.go
57 lines (47 loc) · 1.03 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
// +build !windows,!nacl,!plan9
package irtt
import (
"log/syslog"
"net/url"
"strings"
)
const syslogSupport = true
const defaultSyslogTag = "irtt"
type syslogHandler struct {
syslogWriter *syslog.Writer
}
func (s *syslogHandler) OnEvent(e *Event) {
if e.IsError() {
s.syslogWriter.Err(e.String())
} else {
s.syslogWriter.Info(e.String())
}
}
func newSyslogHandler(uriStr string) (sh Handler, err error) {
var suri *url.URL
if suri, err = parseSyslogURI(uriStr); err != nil {
return
}
prio := syslog.LOG_DAEMON | syslog.LOG_INFO
var sw *syslog.Writer
if suri.Scheme == "local" {
sw, err = syslog.New(prio, suri.Path)
} else {
sw, err = syslog.Dial(suri.Scheme, suri.Host, prio, suri.Path)
}
sh = &syslogHandler{sw}
return
}
func parseSyslogURI(suri string) (u *url.URL, err error) {
if u, err = url.Parse(suri); err != nil {
return
}
u.Path = strings.Trim(u.Path, "/")
if u.Path == "" {
u.Path = defaultSyslogTag
}
if u.Scheme == "" {
err = Errorf(InvalidSyslogURI, "missing colon after scheme")
}
return
}