Skip to content

Commit e9f2038

Browse files
committed
new(rfc3164): option to set the time zone on the Stamp timestamps of the syslog messages
Signed-off-by: Leonardo Di Donato <[email protected]>
1 parent 8ba5784 commit e9f2038

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

rfc3164/machine.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type machine struct {
3535
bestEffort bool
3636
yyyy int
3737
rfc3339 bool
38+
loc *time.Location
3839
}
3940

4041
// NewMachine creates a new FSM able to parse RFC3164 syslog messages.
@@ -58,10 +59,19 @@ func (m *machine) HasBestEffort() bool {
5859
return m.bestEffort
5960
}
6061

62+
// WithYear sets the year for the Stamp timestamp of the RFC 3164 syslog message.
6163
func (m *machine) WithYear(o YearOperator) {
6264
m.yyyy = YearOperation{o}.Operate()
6365
}
6466

67+
// WithTimezone sets the time zone for the Stamp timestamp of the RFC 3164 syslog message.
68+
func (m *machine) WithTimezone(loc *time.Location) {
69+
m.loc = loc
70+
}
71+
72+
// WithRFC3339 enables ability to ALSO match RFC3339 timestamps.
73+
//
74+
// Notice this does not disable the default and correct timestamps - ie., Stamp timestamps.
6575
func (m *machine) WithRFC3339() {
6676
m.rfc3339 = true
6777
}
@@ -1153,6 +1163,9 @@ func (m *machine) Parse(input []byte) (syslog.Message, error) {
11531163
}
11541164
} else {
11551165
output.timestamp = t.AddDate(m.yyyy, 0, 0)
1166+
if m.loc != nil {
1167+
output.timestamp = output.timestamp.In(m.loc)
1168+
}
11561169
output.timestampSet = true
11571170
}
11581171

rfc3164/machine.go.rl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ action set_timestamp {
4444
fgoto fail;
4545
} else {
4646
output.timestamp = t.AddDate(m.yyyy, 0, 0)
47+
if m.loc != nil {
48+
output.timestamp = output.timestamp.In(m.loc)
49+
}
4750
output.timestampSet = true
4851
}
4952
}
@@ -167,6 +170,7 @@ type machine struct {
167170
bestEffort bool
168171
yyyy int
169172
rfc3339 bool
173+
loc *time.Location
170174
}
171175

172176
// NewMachine creates a new FSM able to parse RFC3164 syslog messages.
@@ -196,10 +200,19 @@ func (m *machine) HasBestEffort() bool {
196200
return m.bestEffort
197201
}
198202

203+
// WithYear sets the year for the Stamp timestamp of the RFC 3164 syslog message.
199204
func (m *machine) WithYear(o YearOperator) {
200205
m.yyyy = YearOperation{o}.Operate()
201206
}
202207

208+
// WithTimezone sets the time zone for the Stamp timestamp of the RFC 3164 syslog message.
209+
func (m *machine) WithTimezone(loc *time.Location) {
210+
m.loc = loc
211+
}
212+
213+
// WithRFC3339 enables ability to ALSO match RFC3339 timestamps.
214+
//
215+
// Notice this does not disable the default and correct timestamps - ie., Stamp timestamps.
203216
func (m *machine) WithRFC3339() {
204217
m.rfc3339 = true
205218
}

rfc3164/options.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package rfc3164
22

33
import (
4+
"time"
5+
46
syslog "github.com/influxdata/go-syslog/v2"
57
)
68

@@ -21,12 +23,12 @@ func WithYear(o YearOperator) syslog.MachineOption {
2123
}
2224

2325
// WithTimezone sets the strategy to decide the timezone to apply to the Stamp timestamp of RFC 3164.
24-
// func WithTimezone(loc time.Location) syslog.MachineOption {
25-
// return func(m syslog.Machine) syslog.Machine {
26-
// m.(*machine).WithTimezone(loc)
27-
// return m
28-
// }
29-
// }
26+
func WithTimezone(loc *time.Location) syslog.MachineOption {
27+
return func(m syslog.Machine) syslog.Machine {
28+
m.(*machine).WithTimezone(loc)
29+
return m
30+
}
31+
}
3032

3133
// todo > WithStrictHostname() option - see RFC3164 page 10
3234
// WithStrictHostname tells the parser to match the hostnames strictly as per RFC 3164 recommentations.

0 commit comments

Comments
 (0)