-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmessage.go
76 lines (70 loc) · 1.85 KB
/
message.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
// Pacakge rfc5424 is a library for parsing and serializing RFC-5424 structured
// syslog messages.
//
// Example usage:
//
// m := rfc5424.Message{
// Priority: rfc5424.Daemon | rfc5424.Info,
// Timestamp: time.Now(),
// Hostname: "myhostname",
// AppName: "someapp",
// Message: []byte("Hello, World!"),
// }
// m.AddDatum("foo@1234", "Revision", "1.2.3.4")
// m.WriteTo(os.Stdout)
//
// Produces output like:
//
// 107 <7>1 2016-02-28T09:57:10.804642398-05:00 myhostname someapp - - [foo@1234 Revision="1.2.3.4"] Hello, World!
//
// You can also use the library to parse syslog messages:
//
// m := rfc5424.Message{}
// _, err := m.ReadFrom(os.Stdin)
// fmt.Printf("%s\n", m.Message)
package rfc5424
import "time"
// Message represents a log message as defined by RFC-5424
// (https://tools.ietf.org/html/rfc5424)
type Message struct {
Priority Priority
Timestamp time.Time
Hostname string
AppName string
ProcessID string
MessageID string
StructuredData []StructuredData
Message []byte
}
// SDParam represents parameters for structured data
type SDParam struct {
Name string
Value string
}
// StructuredData represents structured data within a log message
type StructuredData struct {
ID string
Parameters []SDParam
}
// AddDatum adds structured data to a log message
func (m *Message) AddDatum(ID string, Name string, Value string) {
if m.StructuredData == nil {
m.StructuredData = []StructuredData{}
}
for i, sd := range m.StructuredData {
if sd.ID == ID {
sd.Parameters = append(sd.Parameters, SDParam{Name: Name, Value: Value})
m.StructuredData[i] = sd
return
}
}
m.StructuredData = append(m.StructuredData, StructuredData{
ID: ID,
Parameters: []SDParam{
SDParam{
Name: Name,
Value: Value,
},
},
})
}