Skip to content

Commit

Permalink
Support ISO-8601 timestamp format.
Browse files Browse the repository at this point in the history
  • Loading branch information
youyo committed Feb 4, 2020
1 parent e8a5546 commit 30ccdae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ require (
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
)

go 1.13
9 changes: 7 additions & 2 deletions postfix-log-parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (

const (
TimeFormat = "Jan 2 15:04:05"
TimeRegexpFormat = `([A-Za-z]{3}\s*[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2})`
TimeFormatISO8601 = "2006-01-02T15:04:05.999999-07:00"
TimeRegexpFormat = `([A-Za-z]{3}\s*[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}|^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+(?:[+-][0-2]\d:[0-5]\d|Z))`
HostRegexpFormat = `([0-9A-Za-z\.]*)`
ProcessRegexpFormat = `(postfix/[a-z]*\[[0-9]{1,5}\])?`
QueueIdRegexpFormat = `([0-9A-Z]*)`
Expand Down Expand Up @@ -45,9 +46,13 @@ func NewPostfixLog() *PostfixLog {
func (p *PostfixLog) Parse(text []byte) (LogFormat, error) {
re := p.Regexp.Copy()
group := re.FindSubmatch(text)
var t time.Time
t, err := time.ParseInLocation(TimeFormat, string(group[1]), time.Local)
if err != nil {
return LogFormat{}, err
t, err = time.ParseInLocation(TimeFormatISO8601, string(group[1]), time.Local)
if err != nil {
return LogFormat{}, err
}
}

logFormat := LogFormat{
Expand Down
22 changes: 22 additions & 0 deletions test/test-iso8601.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
2020-01-30T06:26:05.404714+09:00 mail postfix/smtpd[1830]: connect from example.com[127.0.0.1]
2020-01-30T06:26:05.404714+09:00 mail postfix/smtpd[1830]: C6E0DDB74006: client=example.com[127.0.0.1]
2020-01-30T06:26:05.404714+09:00 mail postfix/cleanup[1894]: C6E0DDB74006: message-id=<[email protected]>
2020-01-30T06:26:05.404714+09:00 mail postfix/qmgr[18719]: C6E0DDB74006: from=<[email protected]>, size=309891, nrcpt=1 (queue active)
2020-01-30T06:26:05.404714+09:00 mail postfix/smtpd[1830]: disconnect from example.com[127.0.0.1]
2020-01-30T06:26:06.404714+09:00 mail postfix/smtpd[1827]: connect from example.com[127.0.0.1]
2020-01-30T06:26:06.404714+09:00 mail postfix/smtpd[1827]: 3D74ADB7400B: client=example.com[127.0.0.1]
2020-01-30T06:26:06.404714+09:00 mail postfix/cleanup[1695]: 3D74ADB7400B: message-id=<[email protected]>
2020-01-30T06:26:06.404714+09:00 mail postfix/smtpd[1827]: disconnect from example.com[127.0.0.1]
2020-01-30T06:26:06.404714+09:00 mail postfix/qmgr[18719]: 3D74ADB7400B: from=<[email protected]>, size=2140, nrcpt=1 (queue active)
2020-01-30T06:26:07.404714+09:00 mail postfix/smtp[1810]: 3D74ADB7400B: to=<[email protected]>, relay=example.to[192.168.0.20]:25, delay=1.7, delays=0.02/0/1.7/0.06, dsn=2.0.0, status=sent (250 [Sniper] OK 1539154772 snipe-queue 10549)
2020-01-30T06:26:07.404714+09:00 mail postfix/smtp[1810]: 3D74ADB7400B: to=<[email protected]>, relay=example.to[192.168.0.20]:25, delay=1.7, delays=0.02/0/1.7/0.06, dsn=2.0.0, status=sent (250 [Sniper] OK 1539154772 snipe-queue 10549)
2020-01-30T06:26:07.404714+09:00 mail postfix/qmgr[18719]: 3D74ADB7400B: removed
2020-01-30T06:26:08.404714+09:00 mail postfix/smtpd[1830]: connect from example.com[127.0.0.1]
2020-01-30T06:26:08.404714+09:00 mail postfix/smtpd[1830]: 6526CDB7400B: client=example.com[127.0.0.1]
2020-01-30T06:26:08.404714+09:00 mail postfix/cleanup[1894]: 6526CDB7400B: message-id=<[email protected]>
2020-01-30T06:26:08.404714+09:00 mail postfix/smtpd[1830]: disconnect from example.com[127.0.0.1]
2020-01-30T06:26:08.404714+09:00 mail postfix/qmgr[18719]: 6526CDB7400B: from=<[email protected]>, size=4118, nrcpt=1 (queue active)
2020-01-30T06:26:08.404714+09:00 mail postfix/smtp[1417]: 6526CDB7400B: to=<[email protected]>, relay=example.ccc[192.168.0.10]:25, delay=0.1, delays=0.02/0/0.03/0.05, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 71111424269E)
2020-01-30T06:26:08.404714+09:00 mail postfix/qmgr[18719]: 6526CDB7400B: removed
2020-01-30T06:26:09.404714+09:00 mail postfix/smtp[1874]: C6E0DDB74006: to=<[email protected]>, relay=example.ddd[192.168.0.30]:25, delay=3.4, delays=0.11/0/0.38/2.9, dsn=2.0.0, status=sent (250 2.0.0 OK 1539154772 az9-v6si5976496plb.190 - gsmtp)
2020-01-30T06:26:09.404714+09:00 mail postfix/qmgr[18719]: C6E0DDB74006: removed

0 comments on commit 30ccdae

Please sign in to comment.