Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make flog an importable package #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ vendor/

# Build & binary
dist/
flog

# Editors
.idea/
.vscode/
.vscode/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ $ flog -f rfc3164 -l
- RFC5424
- Common log fomat
- JSON
- logfmt

## Supported Outputs

Expand All @@ -105,4 +106,4 @@ $ flog -f rfc3164 -l

## License

[MIT](LICENSE)
[MIT](LICENSE)
2 changes: 1 addition & 1 deletion array.go → flog/array.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flog

func containString(arr []string, str string) bool {
for _, s := range arr {
Expand Down
2 changes: 1 addition & 1 deletion array_test.go → flog/array_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flog

import (
"testing"
Expand Down
4 changes: 3 additions & 1 deletion flog.go → flog/flog.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flog

import (
"compress/gzip"
Expand Down Expand Up @@ -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 ""
}
Expand Down
4 changes: 3 additions & 1 deletion flog_test.go → flog/flog_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flog

import (
"fmt"
Expand All @@ -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"
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion flog_unix.go → flog/flog_unix.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build !windows

package main
package flog

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion flog_windows.go → flog/flog_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flog

import (
"errors"
Expand Down
20 changes: 19 additions & 1 deletion log.go → flog/log.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flog

import (
"fmt"
Expand All @@ -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
Expand Down Expand Up @@ -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(),
)
}
2 changes: 1 addition & 1 deletion log_test.go → flog/log_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flog

import (
"fmt"
Expand Down
3 changes: 2 additions & 1 deletion option.go → flog/option.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flog

import (
"errors"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion option_test.go → flog/option_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flog

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion random.go → flog/random.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flog

import (
"math/rand"
Expand Down
2 changes: 1 addition & 1 deletion random_test.go → flog/random_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flog

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion time.go → flog/time.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flog

// Custom predefined layouts
const (
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}