-
Notifications
You must be signed in to change notification settings - Fork 28
/
syslog_unix.go
102 lines (91 loc) · 2.23 KB
/
syslog_unix.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
//go:build linux || darwin || freebsd || openbsd || solaris
// +build linux darwin freebsd openbsd solaris
package logger
import (
"fmt"
"log/syslog"
"os"
)
type syslogBackend struct {
writer [numSeverity]*syslog.Writer
buf [numSeverity]chan []byte
}
var SyslogPriorityMap = map[string]syslog.Priority{
"local0": syslog.LOG_LOCAL0,
"local1": syslog.LOG_LOCAL1,
"local2": syslog.LOG_LOCAL2,
"local3": syslog.LOG_LOCAL3,
"local4": syslog.LOG_LOCAL4,
"local5": syslog.LOG_LOCAL5,
"local6": syslog.LOG_LOCAL6,
"local7": syslog.LOG_LOCAL7,
}
var pmap = []syslog.Priority{syslog.LOG_EMERG, syslog.LOG_ERR, syslog.LOG_WARNING, syslog.LOG_INFO, syslog.LOG_DEBUG}
func NewSyslogBackend(priorityStr string, tag string) (*syslogBackend, error) {
priority, ok := SyslogPriorityMap[priorityStr]
if !ok {
return nil, fmt.Errorf("unknown syslog priority: %s", priorityStr)
}
var err error
var b syslogBackend
for i := 0; i < numSeverity; i++ {
b.writer[i], err = syslog.New(priority|pmap[i], tag)
if err != nil {
return nil, err
}
b.buf[i] = make(chan []byte, 1<<16)
}
b.log()
return &b, nil
}
func DialSyslogBackend(network, raddr string, priority syslog.Priority, tag string) (*syslogBackend, error) {
var err error
var b syslogBackend
for i := 0; i < numSeverity; i++ {
b.writer[i], err = syslog.Dial(network, raddr, priority|pmap[i], tag+severityName[i])
if err != nil {
return nil, err
}
b.buf[i] = make(chan []byte, 1<<16)
}
b.log()
return &b, nil
}
func (self *syslogBackend) Log(s Severity, msg []byte) {
msg1 := make([]byte, len(msg))
copy(msg1, msg)
switch s {
case FATAL:
self.tryPutInBuf(FATAL, msg1)
case ERROR:
self.tryPutInBuf(ERROR, msg1)
case WARNING:
self.tryPutInBuf(WARNING, msg1)
case INFO:
self.tryPutInBuf(INFO, msg1)
case DEBUG:
self.tryPutInBuf(DEBUG, msg1)
}
}
func (self *syslogBackend) Close() {
for i := 0; i < numSeverity; i++ {
self.writer[i].Close()
}
}
func (self *syslogBackend) tryPutInBuf(s Severity, msg []byte) {
select {
case self.buf[s] <- msg:
default:
os.Stderr.Write(msg)
}
}
func (self *syslogBackend) log() {
for i := 0; i < numSeverity; i++ {
go func(index int) {
for {
msg := <-self.buf[index]
self.writer[index].Write(msg[27:])
}
}(i)
}
}