-
Notifications
You must be signed in to change notification settings - Fork 4
/
syslog_writer.go
57 lines (51 loc) · 978 Bytes
/
syslog_writer.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
package main
import (
"errors"
"log/syslog"
)
type syslogWriter struct {
scheme string
addr string
w *syslog.Writer
buf []byte
}
func (s *syslogWriter) Open(name string) error {
w, err := syslog.Dial(s.scheme, s.addr, syslog.LOG_LOCAL0|syslog.LOG_NOTICE, name)
if err != nil {
return err
}
s.w = w
return nil
}
func (s *syslogWriter) Close() error {
if s.w != nil {
err := s.w.Close()
s.w = nil
return err
}
return nil
}
func (s *syslogWriter) Write(prefix string, b []byte) error {
if s.w == nil {
return errors.New("not connected")
}
var err error
switch prefix {
case "error":
err = s.w.Err(string(b))
case "info":
err = s.w.Info(string(b))
case "debug":
err = s.w.Debug(string(b))
default:
s.buf = append(s.buf[:0], '[')
s.buf = append(s.buf, prefix...)
s.buf = append(s.buf, ']', ' ')
s.buf = append(s.buf, b...)
_, err = s.w.Write(s.buf)
}
return err
}
func (s *syslogWriter) Flush() error {
return nil
}