forked from influxdata/go-syslog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog.go
152 lines (126 loc) · 3.74 KB
/
syslog.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
// Package syslog provides generic interfaces and structs for syslog messages and transport.
// Subpackages contains various parsers or scanners for different syslog formats.
package syslog
import (
"io"
"time"
"github.com/influxdata/go-syslog/v3/common"
)
// BestEfforter is an interface that wraps the HasBestEffort method.
type BestEfforter interface {
WithBestEffort()
HasBestEffort() bool
}
// MaxMessager sets the max message size the parser should be able to parse
type MaxMessager interface {
WithMaxMessageLength(length int)
}
// Machine represent a FSM able to parse an entire syslog message and return it in an structured way.
type Machine interface {
Parse(input []byte) (Message, error)
BestEfforter
}
// MachineOption represents the type of option setters for Machine instances.
type MachineOption func(m Machine) Machine
// Parser is an interface that wraps the Parse method.
type Parser interface {
Parse(r io.Reader)
WithListener(ParserListener)
BestEfforter
MaxMessager
}
// ParserOption represent the type of option setters for Parser instances.
type ParserOption func(p Parser) Parser
// ParserListener is a function that receives syslog parsing results, one by one.
type ParserListener func(*Result)
// Result wraps the outcomes obtained parsing a syslog message.
type Result struct {
Message Message
Error error
}
// Message represent a minimal syslog message.
type Message interface {
Valid() bool
FacilityMessage() *string
FacilityLevel() *string
SeverityMessage() *string
SeverityLevel() *string
SeverityShortLevel() *string
ComputeFromPriority(value uint8)
}
// Base represents a base struct for syslog messages.
//
// It contains the fields in common among different formats.
type Base struct {
Facility *uint8
Severity *uint8
Priority *uint8
Timestamp *time.Time
Hostname *string
Appname *string
ProcID *string
MsgID *string
Message *string
}
// Valid tells whether the receiving message is well-formed or not.
//
// A minimally well-formed RFC3164 syslog message contains at least the priority ([1, 191] or 0).
// A minimally well-formed RFC5424 syslog message also contains the version.
func (m *Base) Valid() bool {
// A nil priority or a 0 version means that the message is not valid
return m.Priority != nil && common.ValidPriority(*m.Priority)
}
// ComputeFromPriority set the priority values and computes facility and severity from it.
//
// It does NOT check the input value validity.
func (m *Base) ComputeFromPriority(value uint8) {
m.Priority = &value
facility := uint8(value / 8)
severity := uint8(value % 8)
m.Facility = &facility
m.Severity = &severity
}
// FacilityMessage returns the text message for the current facility value.
func (m *Base) FacilityMessage() *string {
if m.Facility != nil {
msg := common.Facility[*m.Facility]
return &msg
}
return nil
}
// FacilityLevel returns the
func (m *Base) FacilityLevel() *string {
if m.Facility != nil {
if msg, ok := common.FacilityKeywords[*m.Facility]; ok {
return &msg
}
// Fallback to facility message
msg := common.Facility[*m.Facility]
return &msg
}
return nil
}
// SeverityMessage returns the text message for the current severity value.
func (m *Base) SeverityMessage() *string {
if m.Severity != nil {
msg := common.SeverityMessages[*m.Severity]
return &msg
}
return nil
}
// SeverityLevel returns the text level for the current severity value.
func (m *Base) SeverityLevel() *string {
if m.Severity != nil {
msg := common.SeverityLevels[*m.Severity]
return &msg
}
return nil
}
// SeverityShortLevel returns the short text level for the current severity value.
func (m *Base) SeverityShortLevel() *string {
if m.Severity != nil {
msg := common.SeverityLevelsShort[*m.Severity]
return &msg
}
return nil
}