Skip to content

Commit

Permalink
Common log format (#22)
Browse files Browse the repository at this point in the history
* Added common logfile format support

* Added protocol to Apache and CommonLogfile formats

* Generate safe urls in resource uri

* CommonLogfileFormat -> CommonLogFormat

common_logfile_format -> common_log

* Updated ExampleNewLog with common_log format

* gofmt time.go
  • Loading branch information
emepetres authored and mingrammer committed May 29, 2019
1 parent 7ffa7dc commit 8708f2b
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ There are useful options. (`flog --help`)

```console
Options:
-f, --format string choose log format. ("apache_common"|"apache_combined"|"apache_error"|"rfc3164"|"rfc5424") (default "apache_common")
-f, --format string choose log format. ("apache_common"|"apache_combined"|"apache_error"|"rfc3164"|"rfc5424"|"common_log") (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 Down Expand Up @@ -91,7 +91,7 @@ flog -f rfc3164 -l
* [X] Completely random log generator
* [ ] Contextual random log generator
* [ ] Statistical random log generator
* [x] Support `apache common`, `apache combined`, `apache error`, `rfc3164` and `rfc5424` log formats
* [x] Support `apache common`, `apache combined`, `apache error`, `rfc3164`, `rfc5424` and `common log format` log formats
* [ ] Support some other syslog formats
* [x] Support stdout, file and gzip type of logging

Expand Down
2 changes: 2 additions & 0 deletions flog.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ func NewLog(format string, t time.Time) string {
return NewRFC3164Log(t)
case "rfc5424":
return NewRFC5424Log(t)
case "common_log":
return NewCommonLogFormat(t)
default:
return ""
}
Expand Down
6 changes: 4 additions & 2 deletions flog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ func ExampleNewLog() {
fmt.Println(NewLog("apache_error", created))
fmt.Println(NewLog("rfc3164", created))
fmt.Println(NewLog("rfc5424", created))
fmt.Println(NewLog("common_log", created))
fmt.Println(NewLog("unknown", created))
// Output:
// 222.83.191.222 - - [22/04/2018:09:30:00 +0000] "DELETE /innovate/next-generation" 302 24570
// 174.144.199.149 - mosciski7760 [22/04/2018:09:30:00 +0000] "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"
// 222.83.191.222 - - [22/04/2018:09:30:00 +0000] "DELETE /innovate/next-generation HTTP/1.0" 302 24570
// 174.144.199.149 - mosciski7760 [22/04/2018:09:30:00 +0000] "GET /networks/revolutionary HTTP/1.0" 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"
// [Sun Apr 22 09:30:00 2018] [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 ipsam[5455]: Try to reboot the SMS bandwidth, maybe it will synthesize the mobile transmitter!
// <118>2 2018-04-22T09:30:00.000Z legacyb2b.com natus 8605 ID854 - Try to synthesize the JBOD matrix, maybe it will calculate the 1080p application!
// 34.205.100.144 - - [22/Apr/2018:09:30:00 +0000] "DELETE /empower/revolutionary/rich HTTP/1.0" 416 19950
//
}

Expand Down
20 changes: 18 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (

const (
// ApacheCommonLog : {host} {user-identifier} {auth-user-id} [{datetime}] "{method} {request} HTTP/1.0" {response-code} {bytes}
ApacheCommonLog = "%s - %s [%s] \"%s %s\" %d %d"
ApacheCommonLog = "%s - %s [%s] \"%s %s HTTP/1.0\" %d %d"
// ApacheCombinedLog : {host} {user-identifier} {auth-user-id} [{datetime}] "{method} {request} HTTP/1.0" {response-code} {bytes} "{referrer}" "{agent}"
ApacheCombinedLog = "%s - %s [%s] \"%s %s\" %d %d \"%s\" \"%s\""
ApacheCombinedLog = "%s - %s [%s] \"%s %s HTTP/1.0\" %d %d \"%s\" \"%s\""
// ApacheErrorLog : [{timestamp}] [{module}:{severity}] [pid {pid}:tid {thread-id}] [client: %{client}] %{message}
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"
// CommonLogFormat : {host} {user-identifier} {auth-user-id} [{datetime}] "{method} {request} HTTP/1.0" {response-code} {bytes}
CommonLogFormat = "%s - %s [%s] \"%s %s HTTP/1.0\" %d %d"
)

// NewApacheCommonLog creates a log string with apache common log format
Expand Down Expand Up @@ -93,3 +95,17 @@ func NewRFC5424Log(t time.Time) string {
gofakeit.HackerPhrase(),
)
}

// NewCommonLogFormat creates a log string with common log format
func NewCommonLogFormat(t time.Time) string {
return fmt.Sprintf(
CommonLogFormat,
gofakeit.IPv4Address(),
RandAuthUserID(),
t.Format(CommonLog),
gofakeit.HTTPMethod(),
RandResourceURI(),
gofakeit.StatusCode(),
gofakeit.Number(0, 30000),
)
}
15 changes: 13 additions & 2 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ExampleNewApacheCommonLog() {

created := time.Now()
fmt.Println(NewApacheCommonLog(created))
// Output: 222.83.191.222 - - [22/04/2018:09:30:00 +0000] "DELETE /innovate/next-generation" 302 24570
// Output: 222.83.191.222 - - [22/04/2018:09:30:00 +0000] "DELETE /innovate/next-generation HTTP/1.0" 302 24570
}

func ExampleNewApacheCombinedLog() {
Expand All @@ -29,7 +29,7 @@ func ExampleNewApacheCombinedLog() {

created := time.Now()
fmt.Println(NewApacheCombinedLog(created))
// Output: 222.83.191.222 - - [22/04/2018:09:30:00 +0000] "DELETE /innovate/next-generation" 302 81317 "https://www.forwardholistic.biz/mission-critical/synergize/morph/sticky" "Mozilla/5.0 (Windows NT 5.01) AppleWebKit/5320 (KHTML, like Gecko) Chrome/40.0.875.0 Mobile Safari/5320"
// Output: 222.83.191.222 - - [22/04/2018:09:30:00 +0000] "DELETE /innovate/next-generation HTTP/1.0" 302 81317 "https://www.forwardholistic.biz/mission-critical/synergize/morph/sticky" "Mozilla/5.0 (Windows NT 5.01) AppleWebKit/5320 (KHTML, like Gecko) Chrome/40.0.875.0 Mobile Safari/5320"
}

func ExampleNewApacheErrorLog() {
Expand Down Expand Up @@ -64,3 +64,14 @@ func ExampleNewRFC5424Log() {
fmt.Println(NewRFC5424Log(created))
// Output: <24>3 2018-04-22T09:30:00.000Z futurefunctionalities.biz nisi 9030 ID160 - If we back up the program, we can get to the SSL sensor through the redundant SAS program!
}

func ExampleNewCommonLogFormat() {
rand.Seed(11)

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

created := time.Now()
fmt.Println(NewCommonLogFormat(created))
// Output: 222.83.191.222 - - [22/Apr/2018:09:30:00 +0000] "DELETE /innovate/next-generation HTTP/1.0" 302 24570
}
2 changes: 1 addition & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Options:
-l, --loop loop output forever until killed.
`

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

// Option defines log generator options
Expand Down
3 changes: 2 additions & 1 deletion random.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"math/rand"
net_url "net/url"
"strings"

"github.com/brianvoe/gofakeit"
Expand All @@ -13,7 +14,7 @@ func RandResourceURI() string {
num := gofakeit.Number(1, 4)
slug := make([]string, num)
for i := 0; i < num; i++ {
slug[i] = gofakeit.BS()
slug[i] = net_url.QueryEscape(gofakeit.BS())
}
url += "/" + strings.ToLower(strings.Join(slug, "/"))
return url
Expand Down
1 change: 1 addition & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ const (
ApacheError = "Mon Jan 02 15:04:05 2006"
RFC3164 = "Jan 02 15:04:05"
RFC5424 = "2006-01-02T15:04:05.000Z"
CommonLog = "02/Jan/2006:15:04:05 -0700"
)

0 comments on commit 8708f2b

Please sign in to comment.