diff --git a/.gitignore b/.gitignore index 7c510c6..81ae608 100644 --- a/.gitignore +++ b/.gitignore @@ -3,8 +3,7 @@ vendor/ # Build & binary dist/ -flog # Editors .idea/ -.vscode/ \ No newline at end of file +.vscode/ 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/array.go b/flog/array.go similarity index 91% rename from array.go rename to flog/array.go index 2be5ac7..b3bb5dd 100644 --- a/array.go +++ b/flog/array.go @@ -1,4 +1,4 @@ -package main +package flog func containString(arr []string, str string) bool { for _, s := range arr { diff --git a/array_test.go b/flog/array_test.go similarity index 96% rename from array_test.go rename to flog/array_test.go index 17db80a..8c7c0bd 100644 --- a/array_test.go +++ b/flog/array_test.go @@ -1,4 +1,4 @@ -package main +package flog import ( "testing" diff --git a/flog.go b/flog/flog.go similarity index 98% rename from flog.go rename to flog/flog.go index 896026b..2fb5cbb 100644 --- a/flog.go +++ b/flog/flog.go @@ -1,4 +1,4 @@ -package main +package flog import ( "compress/gzip" @@ -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/flog_test.go similarity index 87% rename from flog_test.go rename to flog/flog_test.go index dd860ca..e4b44de 100644 --- a/flog_test.go +++ b/flog/flog_test.go @@ -1,4 +1,4 @@ -package main +package flog import ( "fmt" @@ -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/flog_unix.go b/flog/flog_unix.go similarity index 97% rename from flog_unix.go rename to flog/flog_unix.go index ff49407..90189af 100644 --- a/flog_unix.go +++ b/flog/flog_unix.go @@ -1,6 +1,6 @@ // +build !windows -package main +package flog import ( "errors" diff --git a/flog_windows.go b/flog/flog_windows.go similarity index 97% rename from flog_windows.go rename to flog/flog_windows.go index 1664e3e..ff173d1 100644 --- a/flog_windows.go +++ b/flog/flog_windows.go @@ -1,4 +1,4 @@ -package main +package flog import ( "errors" diff --git a/log.go b/flog/log.go similarity index 85% rename from log.go rename to flog/log.go index 304012d..c4ca98c 100644 --- a/log.go +++ b/flog/log.go @@ -1,4 +1,4 @@ -package main +package flog import ( "fmt" @@ -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/log_test.go b/flog/log_test.go similarity index 99% rename from log_test.go rename to flog/log_test.go index 77dcfd0..fee63a8 100644 --- a/log_test.go +++ b/flog/log_test.go @@ -1,4 +1,4 @@ -package main +package flog import ( "fmt" diff --git a/option.go b/flog/option.go similarity index 99% rename from option.go rename to flog/option.go index 7396211..2fe9f37 100644 --- a/option.go +++ b/flog/option.go @@ -1,4 +1,4 @@ -package main +package flog import ( "errors" @@ -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) diff --git a/option_test.go b/flog/option_test.go similarity index 99% rename from option_test.go rename to flog/option_test.go index fc09701..2f44a1a 100644 --- a/option_test.go +++ b/flog/option_test.go @@ -1,4 +1,4 @@ -package main +package flog import ( "testing" diff --git a/random.go b/flog/random.go similarity index 98% rename from random.go rename to flog/random.go index 6ed5da1..4d0d651 100644 --- a/random.go +++ b/flog/random.go @@ -1,4 +1,4 @@ -package main +package flog import ( "math/rand" diff --git a/random_test.go b/flog/random_test.go similarity index 91% rename from random_test.go rename to flog/random_test.go index 69f3514..6805a09 100644 --- a/random_test.go +++ b/flog/random_test.go @@ -1,4 +1,4 @@ -package main +package flog import ( "fmt" diff --git a/time.go b/flog/time.go similarity index 94% rename from time.go rename to flog/time.go index 69c9cfa..597e7fc 100644 --- a/time.go +++ b/flog/time.go @@ -1,4 +1,4 @@ -package main +package flog // Custom predefined layouts const ( diff --git a/main.go b/main.go index 164e399..2d68d83 100644 --- a/main.go +++ b/main.go @@ -5,12 +5,13 @@ import ( "time" "github.com/mingrammer/cfmt" + "github.com/mingrammer/flog/flog" ) func main() { rand.Seed(time.Now().UnixNano()) - opts := ParseOptions() - if err := Run(opts); err != nil { + opts := flog.ParseOptions() + if err := flog.Run(opts); err != nil { cfmt.Warningln(err.Error()) } }