diff --git a/README.md b/README.md index 4b88f89..20794dc 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ $ flog -f rfc3164 -l - RFC5424 - Common log fomat - JSON +- logfmt ## Supported Outputs @@ -105,4 +106,4 @@ $ flog -f rfc3164 -l ## License -[MIT](LICENSE) \ No newline at end of file +[MIT](LICENSE) diff --git a/flog.go b/flog.go index 896026b..c0013da 100644 --- a/flog.go +++ b/flog.go @@ -130,6 +130,8 @@ func NewLog(format string, t time.Time) string { return NewCommonLogFormat(t) case "json": return NewJSONLogFormat(t) + case "logfmt": + return NewLogFmtLogFormat(t) default: return "" } diff --git a/flog_test.go b/flog_test.go index dd860ca..504720a 100644 --- a/flog_test.go +++ b/flog_test.go @@ -25,6 +25,7 @@ func ExampleNewLog() { fmt.Println(NewLog("common_log", created)) fmt.Println(NewLog("unknown", created)) fmt.Println(NewLog("json", created)) + fmt.Println(NewLog("logfmt", created)) // Output: // 222.83.191.222 - - [22/Apr/2018:09:30:00 +0000] "DELETE /innovate/next-generation HTTP/1.1" 406 7610 // 144.199.149.125 - waelchi7603 [22/Apr/2018:09:30:00 +0000] "PUT /revolutionary HTTP/1.1" 301 8089 "https://www.futureaggregate.io/users" "Mozilla/5.0 (Macintosh; PPC Mac OS X 10_6_5 rv:4.0; en-US) AppleWebKit/536.38.2 (KHTML, like Gecko) Version/6.0 Safari/536.38.2" @@ -34,6 +35,7 @@ func ExampleNewLog() { // 195.44.200.155 - kihn6187 [22/Apr/2018:09:30:00 +0000] "GET /revolutionary/e-markets/holistic/syndicate HTTP/2.0" 404 14503 // // {"host":"13.108.182.26", "user-identifier":"bailey7205", "datetime":"22/Apr/2018:09:30:00 +0000", "method": "GET", "request": "/out-of-the-box/architectures/embrace", "protocol":"HTTP/1.0", "status":200, "bytes":5921, "referer": "http://www.dynamicexperiences.io/robust"} + // host="169.19.25.250" user=rutherford8856 timestamp=2018-04-22T09:30:00.000Z method=HEAD request="/e-enable/virtual/partnerships" protocol=HTTP/2.0 status=400 bytes=16428 referer="http://www.investorstrategic.biz/strategic/vertical/scalable/ubiquitous" } func TestNewSplitFileName(t *testing.T) { diff --git a/log.go b/log.go index 304012d..92f1644 100644 --- a/log.go +++ b/log.go @@ -23,6 +23,8 @@ const ( CommonLogFormat = "%s - %s [%s] \"%s %s %s\" %d %d" // JSONLogFormat : {"host": "{host}", "user-identifier": "{user-identifier}", "datetime": "{datetime}", "method": "{method}", "request": "{request}", "protocol": "{protocol}", "status", {status}, "bytes": {bytes}, "referer": "{referer}"} JSONLogFormat = `{"host":"%s", "user-identifier":"%s", "datetime":"%s", "method": "%s", "request": "%s", "protocol":"%s", "status":%d, "bytes":%d, "referer": "%s"}` + // LogFmtLogFormat : host={host} user={user-identifier} timestamp={datetime} method={method} request="{request}" protocol={protocol} status={status} bytes={bytes} referer="{referer}" + LogFmtLogFormat = `host="%s" user=%s timestamp=%s method=%s request="%s" protocol=%s status=%d bytes=%d referer="%s"` ) // NewApacheCommonLog creates a log string with apache common log format @@ -131,3 +133,19 @@ func NewJSONLogFormat(t time.Time) string { gofakeit.URL(), ) } + +// NewLogFmtLogFormat creates a log string with logfmt log format +func NewLogFmtLogFormat(t time.Time) string { + return fmt.Sprintf( + LogFmtLogFormat, + gofakeit.IPv4Address(), + RandAuthUserID(), + t.Format(RFC5424), + gofakeit.HTTPMethod(), + RandResourceURI(), + RandHTTPVersion(), + gofakeit.StatusCode(), + gofakeit.Number(0, 30000), + gofakeit.URL(), + ) +} diff --git a/option.go b/option.go index 7396211..b0d60a2 100644 --- a/option.go +++ b/option.go @@ -26,6 +26,7 @@ Options: - rfc3164 - rfc5424 - json + - logfmt -o, --output string output filename. Path-like is allowed. (default "generated.log") -t, --type string log output type. available types: - stdout (default)