forked from influxdata/go-syslog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
96 lines (90 loc) · 2.47 KB
/
example_test.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
package rfc5424
import (
"fmt"
"github.com/davecgh/go-spew/spew"
)
func output(out interface{}) {
spew.Config.DisableCapacities = true
spew.Config.DisablePointerAddresses = true
spew.Dump(out)
}
func Example() {
i := []byte(`<165>4 2018-10-11T22:14:15.003Z mymach.it e - 1 [ex@32473 iut="3"] An application event log entry...`)
p := NewParser()
m, _ := p.Parse(i)
output(m)
// Output:
// (*rfc5424.SyslogMessage)({
// Base: (syslog.Base) {
// Facility: (*uint8)(20),
// Severity: (*uint8)(5),
// Priority: (*uint8)(165),
// Timestamp: (*time.Time)(2018-10-11 22:14:15.003 +0000 UTC),
// Hostname: (*string)((len=9) "mymach.it"),
// Appname: (*string)((len=1) "e"),
// ProcID: (*string)(<nil>),
// MsgID: (*string)((len=1) "1"),
// Message: (*string)((len=33) "An application event log entry...")
// },
// Version: (uint16) 4,
// StructuredData: (*map[string]map[string]string)((len=1) {
// (string) (len=8) "ex@32473": (map[string]string) (len=1) {
// (string) (len=3) "iut": (string) (len=1) "3"
// }
// })
// })
}
func Example_besteffort() {
i := []byte(`<1>1 A - - - - - -`)
p := NewParser(WithBestEffort())
m, e := p.Parse(i)
output(m)
fmt.Println(e)
// Output:
// (*rfc5424.SyslogMessage)({
// Base: (syslog.Base) {
// Facility: (*uint8)(0),
// Severity: (*uint8)(1),
// Priority: (*uint8)(1),
// Timestamp: (*time.Time)(<nil>),
// Hostname: (*string)(<nil>),
// Appname: (*string)(<nil>),
// ProcID: (*string)(<nil>),
// MsgID: (*string)(<nil>),
// Message: (*string)(<nil>)
// },
// Version: (uint16) 1,
// StructuredData: (*map[string]map[string]string)(<nil>)
// })
// expecting a RFC3339MICRO timestamp or a nil value [col 5]
}
func Example_builder() {
msg := &SyslogMessage{}
msg.SetTimestamp("not a RFC3339MICRO timestamp")
fmt.Println("Valid?", msg.Valid())
msg.SetPriority(191)
msg.SetVersion(1)
fmt.Println("Valid?", msg.Valid())
output(msg)
str, _ := msg.String()
fmt.Println(str)
// Output:
// Valid? false
// Valid? true
// (*rfc5424.SyslogMessage)({
// Base: (syslog.Base) {
// Facility: (*uint8)(23),
// Severity: (*uint8)(7),
// Priority: (*uint8)(191),
// Timestamp: (*time.Time)(<nil>),
// Hostname: (*string)(<nil>),
// Appname: (*string)(<nil>),
// ProcID: (*string)(<nil>),
// MsgID: (*string)(<nil>),
// Message: (*string)(<nil>)
// },
// Version: (uint16) 1,
// StructuredData: (*map[string]map[string]string)(<nil>)
// })
// <191>1 - - - - - -
}