Skip to content

Commit

Permalink
Support RFC5424 syslog format
Browse files Browse the repository at this point in the history
  • Loading branch information
mingrammer committed Apr 22, 2019
1 parent 989bed9 commit d09e931
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions flog.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func NewLog(format string, t time.Time) string {
return NewApacheErrorLog(t)
case "rfc3164":
return NewRFC3164Log(t)
case "rfc5424":
return NewRFC5424Log(t)
default:
return ""
}
Expand Down
2 changes: 2 additions & 0 deletions flog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ func ExampleNewLog() {
fmt.Println(NewLog("apache_combined", created))
fmt.Println(NewLog("apache_error", created))
fmt.Println(NewLog("rfc3164", created))
fmt.Println(NewLog("rfc5424", created))
fmt.Println(NewLog("unknown", created))
// Output:
// 222.83.191.222 - Kozey7157 697 [2018-04-22T09:30:00Z] "DELETE /innovate/next-generation" 302 24570
// 174.144.199.149 - Mosciski7760 386 [2018-04-22T09:30:00Z] "GET /networks/revolutionary" 204 70360 "https://www.directeyeballs.org/streamline" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7) AppleWebKit/5312 (KHTML, like Gecko) Chrome/37.0.821.0 Mobile Safari/5312"
// [2018-04-22T09:30:00Z] [tempore:warn] [pid 7401:tid 4039] [client: 184.155.77.136] Try to synthesize the SMS capacitor, maybe it will compress the online program!
// <25>Apr 22 09:30:00 Fay5424 Monitored[5455]: Try to reboot the SMS bandwidth, maybe it will synthesize the mobile transmitter!
// <118>2 2018-04-22T09:30:00.000Z legacyb2b.com toolset 8605 ID854 - Try to synthesize the JBOD matrix, maybe it will calculate the 1080p application!
//
}

Expand Down
18 changes: 18 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const (
ApacheErrorLog = "[%s] [%s:%s] [pid %d:tid %d] [client: %s] %s"
// RFC3164Log : <priority>{timestamp} {hostname} {application}[{pid}]: {message}
RFC3164Log = "<%d>%s %s %s[%d]: %s"
// RFC5424Log : <priority>{version} {iso-timestamp} {hostname} {application} {pid} {message-id} {structured-data} {message}
RFC5424Log = "<%d>%d %s %s %s %d ID%d %s %s"
)

// NewApacheCommonLog creates a log string with apache common log format
Expand Down Expand Up @@ -76,3 +78,19 @@ func NewRFC3164Log(t time.Time) string {
gofakeit.HackerPhrase(),
)
}

// NewRFC5424Log creates a log string with syslog (RFC5424) format
func NewRFC5424Log(t time.Time) string {
return fmt.Sprintf(
RFC5424Log,
gofakeit.Number(0, 191),
gofakeit.Number(1, 3),
t.Format(RFC5424),
gofakeit.DomainName(),
gofakeit.BuzzWord(),
gofakeit.Number(1, 10000),
gofakeit.Number(1, 1000),
"-", // TODO: structured data
gofakeit.HackerPhrase(),
)
}
11 changes: 11 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,14 @@ func ExampleNewRFC3164Log() {
fmt.Println(NewRFC3164Log(created))
// Output: <24>Apr 22 09:30:00 Moen8727 concept[3160]: If we back up the program, we can get to the SSL sensor through the redundant SAS program!
}

func ExampleNewRFC5424Log() {
rand.Seed(11)

monkey.Patch(time.Now, func() time.Time { return stopped })
defer monkey.Unpatch(time.Now)

created := time.Now()
fmt.Println(NewRFC5424Log(created))
// Output: <24>3 2018-04-22T09:30:00.000Z futurefunctionalities.biz info-mediaries 9030 ID160 - If we back up the program, we can get to the SSL sensor through the redundant SAS program!
}
4 changes: 2 additions & 2 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Usage: flog [options]
Version: %s
Options:
-f, --format string choose log format. ("apache_common"|"apache_combined"|"apache_error"|"rfc3164") (default "apache_common")
-f, --format string choose log format. ("apache_common"|"apache_combined"|"apache_error"|"rfc3164"|"rfc5424") (default "apache_common")
-o, --output string output filename. Path-like is allowed. (default "generated.log")
-t, --type string log output type. ("stdout"|"log"|"gz") (default "stdout")
-n, --number integer number of lines to generate.
Expand All @@ -31,7 +31,7 @@ Options:
-l, --loop loop output forever until killed.
`

var validFormats = []string{"apache_common", "apache_combined", "apache_error", "rfc3164"}
var validFormats = []string{"apache_common", "apache_combined", "apache_error", "rfc3164", "rfc5424"}
var validTypes = []string{"stdout", "log", "gz"}

// Option defines log generator options
Expand Down
1 change: 1 addition & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package main
// Custom predefined layouts
const (
RFC3164 = "Jan 02 15:04:05"
RFC5424 = "2006-01-02T15:04:05.000Z"
)

0 comments on commit d09e931

Please sign in to comment.