Skip to content

Commit

Permalink
fix: preserve newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
dcilke committed Apr 27, 2023
1 parent 8a8caea commit be2a6db
Show file tree
Hide file tree
Showing 22 changed files with 136 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (

require (
github.com/dcilke/gu v0.1.0
github.com/dcilke/heron v0.1.0
github.com/dcilke/heron v0.2.0
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/dcilke/golden v0.1.0 h1:5TRihP8pdy/h5I++ypFGKXT6CrI1y1Jn+2Nw/7qc9Ko=
github.com/dcilke/golden v0.1.0/go.mod h1:KxQFVmzRGf7A9jPbCMXIDr9kQxwnZZ7i07wWyihuhNU=
github.com/dcilke/gu v0.1.0 h1:oxkaeVJ+AzC992otWFqt2gku4U/VyzO62ZscWOUbQ9M=
github.com/dcilke/gu v0.1.0/go.mod h1:8lZQPS+FeUGA7Lc3YAaG+cEYA0VAUYBKBGACsCkpgzI=
github.com/dcilke/heron v0.1.0 h1:/t9SOiESJbBrpxkkRhcOQTEdqRtW/vcGW9w6K66lxcc=
github.com/dcilke/heron v0.1.0/go.mod h1:f9h5pFjBLvwc8tJfrbejvI9yOm3KEx2f6HOXLf/NWRw=
github.com/dcilke/heron v0.2.0 h1:PNiaekqB4wspS85/f5CJzUBhmxVsBglFwx91Ve174Bk=
github.com/dcilke/heron v0.2.0/go.mod h1:f9h5pFjBLvwc8tJfrbejvI9yOm3KEx2f6HOXLf/NWRw=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
Expand Down
2 changes: 1 addition & 1 deletion integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/require"
)

var filecases = []string{"strings", "ndjson", "pretty", "array"}
var filecases = []string{"strings", "ndjson", "pretty", "array", "mixed"}
var levels = []string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}

func TestCLI(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions integration/testdata/golden/TestCLI/mixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
servicea 12:34:25 TRC yup module=http
servicea 12:34:25 DBG yeah module=http
servicea 12:34:26 INF here module=http
serviceb 12:34:26 WRN warning, something is suspicious module=grpc
servicea 12:34:27 ERR hit module=http
serviceb 12:34:27 WRN this shouldn't happen module=grpc
servicea 12:34:28 WRN seriously?!? module=grpc
serviceb 12:34:28 FTL fatal module=http
servicea 12:34:29 PNC panic! module=http
serviceb 12:34:29 DBG wat module=search
servicea 12:34:29 DBG request elapsed=8.268013 method=GET module=search statusCode=200 url={"domain":"localhost","full":"www.example.com","original":"https://original.url","path":"/yo","port":80,"scheme":"http"}
1 change: 1 addition & 0 deletions integration/testdata/golden/TestCLI/strings
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ msg
10
42.42
2015-01-01T00:00:00.000Z

a b c d e f g h i j k l m n o p q r s t u v w x y z
tabs tabs tabs
11 changes: 11 additions & 0 deletions integration/testdata/golden/TestCLI_Level/mixed/debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
servicea
servicea 12:34:25 DBG yeah module=http
servicea
serviceb
servicea
serviceb
servicea
serviceb
servicea
serviceb 12:34:29 DBG wat module=search
servicea 12:34:29 DBG request elapsed=8.268013 method=GET module=search statusCode=200 url={"domain":"localhost","full":"www.example.com","original":"https://original.url","path":"/yo","port":80,"scheme":"http"}
11 changes: 11 additions & 0 deletions integration/testdata/golden/TestCLI_Level/mixed/error
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
servicea
servicea
servicea
serviceb
servicea 12:34:27 ERR hit module=http
serviceb
servicea
serviceb
servicea
serviceb
servicea
11 changes: 11 additions & 0 deletions integration/testdata/golden/TestCLI_Level/mixed/fatal
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
servicea
servicea
servicea
serviceb
servicea
serviceb
servicea
serviceb 12:34:28 FTL fatal module=http
servicea
serviceb
servicea
11 changes: 11 additions & 0 deletions integration/testdata/golden/TestCLI_Level/mixed/info
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
servicea
servicea
servicea 12:34:26 INF here module=http
serviceb
servicea
serviceb
servicea
serviceb
servicea
serviceb
servicea
11 changes: 11 additions & 0 deletions integration/testdata/golden/TestCLI_Level/mixed/panic
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
servicea
servicea
servicea
serviceb
servicea
serviceb
servicea
serviceb
servicea 12:34:29 PNC panic! module=http
serviceb
servicea
11 changes: 11 additions & 0 deletions integration/testdata/golden/TestCLI_Level/mixed/trace
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
servicea 12:34:25 TRC yup module=http
servicea
servicea
serviceb
servicea
serviceb
servicea
serviceb
servicea
serviceb
servicea
11 changes: 11 additions & 0 deletions integration/testdata/golden/TestCLI_Level/mixed/warn
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
servicea
servicea
servicea
serviceb 12:34:26 WRN warning, something is suspicious module=grpc
servicea
serviceb 12:34:27 WRN this shouldn't happen module=grpc
servicea 12:34:28 WRN seriously?!? module=grpc
serviceb
servicea
serviceb
servicea
1 change: 1 addition & 0 deletions integration/testdata/golden/TestCLI_Level/strings/debug
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ msg
10
42.42
2015-01-01T00:00:00.000Z

a b c d e f g h i j k l m n o p q r s t u v w x y z
tabs tabs tabs
1 change: 1 addition & 0 deletions integration/testdata/golden/TestCLI_Level/strings/error
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ msg
10
42.42
2015-01-01T00:00:00.000Z

a b c d e f g h i j k l m n o p q r s t u v w x y z
tabs tabs tabs
1 change: 1 addition & 0 deletions integration/testdata/golden/TestCLI_Level/strings/fatal
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ msg
10
42.42
2015-01-01T00:00:00.000Z

a b c d e f g h i j k l m n o p q r s t u v w x y z
tabs tabs tabs
1 change: 1 addition & 0 deletions integration/testdata/golden/TestCLI_Level/strings/info
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ msg
10
42.42
2015-01-01T00:00:00.000Z

a b c d e f g h i j k l m n o p q r s t u v w x y z
tabs tabs tabs
1 change: 1 addition & 0 deletions integration/testdata/golden/TestCLI_Level/strings/panic
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ msg
10
42.42
2015-01-01T00:00:00.000Z

a b c d e f g h i j k l m n o p q r s t u v w x y z
tabs tabs tabs
1 change: 1 addition & 0 deletions integration/testdata/golden/TestCLI_Level/strings/trace
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ msg
10
42.42
2015-01-01T00:00:00.000Z

a b c d e f g h i j k l m n o p q r s t u v w x y z
tabs tabs tabs
1 change: 1 addition & 0 deletions integration/testdata/golden/TestCLI_Level/strings/warn
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ msg
10
42.42
2015-01-01T00:00:00.000Z

a b c d e f g h i j k l m n o p q r s t u v w x y z
tabs tabs tabs
11 changes: 11 additions & 0 deletions integration/testdata/golden/TestCLI_Strict/mixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
12:34:25 TRC yup module=http
12:34:25 DBG yeah module=http
12:34:26 INF here module=http
12:34:26 WRN warning, something is suspicious module=grpc
12:34:27 ERR hit module=http
12:34:27 WRN this shouldn't happen module=grpc
12:34:28 WRN seriously?!? module=grpc
12:34:28 FTL fatal module=http
12:34:29 PNC panic! module=http
12:34:29 DBG wat module=search
12:34:29 DBG request elapsed=8.268013 method=GET module=search statusCode=200 url={"domain":"localhost","full":"www.example.com","original":"https://original.url","path":"/yo","port":80,"scheme":"http"}
11 changes: 11 additions & 0 deletions integration/testdata/samples/mixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
servicea {"module":"http","time":"2022-08-03T12:34:25.142900417Z","log":{"level":"trace"},"message":"yup"}
servicea {"module":"http","time":"2022-08-03T12:34:25.605701107Z","log":{"level":"debug"},"message":"yeah"}
servicea {"module":"http","time":"2022-08-03T12:34:26.143015538Z","log":{"level":"info"},"message":"here"}
serviceb {"module":"grpc","time":"2022-08-03T12:34:26.543649596Z","log":{"level":"warn"},"message":"warning, something is suspicious"}
servicea {"module":"http","time":"2022-08-03T12:34:27.142783759Z","log":{"level":"error"},"message":"hit"}
serviceb {"module":"grpc","time":"2022-08-03T12:34:27.428810856Z","log":{"level":"warn"},"message":"this shouldn't happen"}
servicea {"module":"grpc","time":"2022-08-03T12:34:28.119661968Z","log":{"level":"warn"},"message":"seriously?!?"}
serviceb {"module":"http","time":"2022-08-03T12:34:28.142497379Z","log":{"level":"fatal"},"message":"fatal"}
servicea {"module":"http","time":"2022-08-03T12:34:29.142768807Z","log":{"level":"panic"},"message":"panic!"}
serviceb {"module":"search","time":"2022-08-03T12:34:29.157192604Z","log":{"level":"debug"},"message":"wat"}
servicea {"module":"search","method":"GET","url":{"full":"www.example.com","original":"https://original.url","domain":"localhost","path":"/yo","port":80,"scheme":"http"},"statusCode":200,"elapsed":8.268013,"time":"2022-08-03T12:34:29.165763321Z","log":{"level":"debug"},"message":"request"}
17 changes: 14 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/jessevdk/go-flags"
)

const (
newline = "\n"
)

type Cmd struct {
Strict bool `short:"s" long:"strict" description:"strict mode"`
Level []string `short:"l" long:"level" description:"only output lines at this level"`
Expand All @@ -36,19 +40,26 @@ func main() {
w := writer.New(
writer.WithLevelFilters(cmd.Level),
)
// didnl is used to prevent double newlines since we want to ensure each JSON
// objects is on its own line but we want to preserve as much of the output
// as possible
didnl := true
h := heron.New(
heron.WithBufSize(bufSize),
heron.WithJSON(func(a any) {
s, _ := w.WriteAny(a)
if s > 0 {
didnl = true
w.Println()
}
}),
heron.WithBytes(func(b []byte) {
s, _ := w.Print(string(b))
if s > 0 {
w.Println()
sb := string(b)
if sb == newline && didnl {
return
}
didnl = false
_, _ = w.Print(sb)
}),
heron.WithError(func(err error) {
fmt.Fprint(os.Stderr, fmt.Errorf("extractor error: %w", err))
Expand Down

0 comments on commit be2a6db

Please sign in to comment.