This repository was archived by the owner on Jul 6, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.go
160 lines (135 loc) · 3.05 KB
/
format.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package syslogp
import (
"bytes"
"errors"
"fmt"
"io"
"log/syslog"
"strings"
"time"
)
// Right now there is only version 1.
const syslogVersion = 1
// Fields for the RFC5424.
type Fields map[string]interface{}
type rfc5424Formatter struct {
priority syslog.Priority
time time.Time
hostname string
app string
pid int
msgID string
fields Fields
msg string
buf bytes.Buffer
}
// NewRFC5424Formatter returns a formatter for this specific RFC.
// See https://tools.ietf.org/html/rfc5424
func NewRFC5424Formatter(facility syslog.Priority, severity syslog.Priority, time time.Time, hostname string, app string, pid int, msgID string, fields Fields, msg string) (io.Reader, error) {
priority := facility | severity
if priority < 0 || priority > syslog.LOG_LOCAL7|syslog.LOG_DEBUG {
return nil, errors.New("invalid syslog priority")
}
var buf bytes.Buffer
return &rfc5424Formatter{
priority, time, hostname,
app, pid, msgID,
fields, msg,
buf,
}, nil
}
func (f *rfc5424Formatter) Read(p []byte) (n int, err error) {
if f.buf.Len() > 0 {
return 0, io.EOF
}
fields := "-"
var i int
if len(f.fields) > 0 {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("[context@%v ", f.pid))
for key, value := range f.fields {
buf.WriteString(fmt.Sprintf(`%v="%v"`, key, value))
if i != len(f.fields)-1 {
buf.WriteString(" ")
}
i++
}
buf.WriteString("]")
fields = buf.String()
}
// ensure it ends in a \n
nl := ""
if !strings.HasSuffix(f.msg, "\n") {
nl = "\n"
}
// <priority>VERSION ISOTIMESTAMP HOSTNAME APPLICATION PID MESSAGEID STRUCTURED-DATA MSG
_, err = fmt.Fprintf(
&f.buf,
"<%d>%v %v %v %v %v %v %v %v%v",
f.priority,
syslogVersion,
f.time.Format(time.RFC3339),
f.hostname,
f.app,
f.pid,
f.msgID,
fields,
f.msg,
nl,
)
if err != nil {
return 0, err
}
copy(p, f.buf.Bytes())
return f.buf.Len(), nil
}
type rfc3164Formatter struct {
priority syslog.Priority
time time.Time
hostname string
app string
pid int
msg string
buf bytes.Buffer
}
// NewRFC3164Formatter returns a formatter for this specific RFC.
// See https://tools.ietf.org/html/rfc3164
func NewRFC3164Formatter(facility syslog.Priority, severity syslog.Priority, time time.Time, hostname string, app string, pid int, msg string) (io.Reader, error) {
priority := facility | severity
if priority < 0 || priority > syslog.LOG_LOCAL7|syslog.LOG_DEBUG {
return nil, errors.New("invalid syslog priority")
}
var buf bytes.Buffer
return &rfc3164Formatter{
priority, time, hostname,
app, pid, msg,
buf,
}, nil
}
func (f *rfc3164Formatter) Read(p []byte) (n int, err error) {
if f.buf.Len() > 0 {
return 0, io.EOF
}
// ensure it ends in a \n
nl := ""
if !strings.HasSuffix(f.msg, "\n") {
nl = "\n"
}
//<PRI>TIMESTAMP HOSTNAME TAG[PID]: MSG
_, err = fmt.Fprintf(
&f.buf,
"<%d>%s %s %s[%d]: %s%s",
f.priority,
f.time.Format(time.Stamp),
f.hostname,
f.app,
f.pid,
f.msg,
nl,
)
if err != nil {
return 0, err
}
copy(p, f.buf.Bytes())
return f.buf.Len(), nil
}