Skip to content

Commit

Permalink
feat: add no pin flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dcilke committed Apr 29, 2023
1 parent 4de0a59 commit d4e9241
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 30 deletions.
8 changes: 7 additions & 1 deletion integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestCLI_Flat(t *testing.T) {
}

func TestCLI_Vert(t *testing.T) {
output, err := hz(fn("nested"), "--plain", "--vert")
output, err := hz(fn("nested"), "--plain", "--vertical")
require.NoError(t, err)
golden.Assert(t, output)
}
Expand All @@ -69,3 +69,9 @@ func TestCLI_Color(t *testing.T) {
require.NoError(t, err)
golden.Assert(t, output)
}

func TestCLI_NoPin(t *testing.T) {
output, err := hz(fn("mixed"), "--plain", "--no-pin")
require.NoError(t, err)
golden.Assert(t, output)
}
13 changes: 7 additions & 6 deletions integration/testdata/golden/TestCLI_Help/--help
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ Usage:
hz [FILE]

Application Options:
-l, --level= only output lines at this level
-s, --strict strict mode
-f, --flat flatten output
-v, --vert vertical output
-p, --plain plain (no color) output
-l, --level= only output lines at this level
-s, --strict strict mode
-f, --flat flatten output
-v, --vertical vertical output
-p, --plain plain (no color) output
-n, --no-pin don't pin any fields

Help Options:
-h, --help Show this help message
-h, --help Show this help message
13 changes: 7 additions & 6 deletions integration/testdata/golden/TestCLI_Help/-h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ Usage:
hz [FILE]

Application Options:
-l, --level= only output lines at this level
-s, --strict strict mode
-f, --flat flatten output
-v, --vert vertical output
-p, --plain plain (no color) output
-l, --level= only output lines at this level
-s, --strict strict mode
-f, --flat flatten output
-v, --vertical vertical output
-p, --plain plain (no color) output
-n, --no-pin don't pin any fields

Help Options:
-h, --help Show this help message
-h, --help Show this help message
11 changes: 11 additions & 0 deletions integration/testdata/golden/TestCLI_NoPin
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
servicea log={"level":"trace"} message=yup module=http time=2022-08-03T12:34:25.142900417Z
servicea log={"level":"debug"} message=yeah module=http time=2022-08-03T12:34:25.605701107Z
servicea log={"level":"info"} message=here module=http time=2022-08-03T12:34:26.143015538Z
serviceb log={"level":"warn"} message="warning, something is suspicious" module=grpc time=2022-08-03T12:34:26.543649596Z
servicea log={"level":"error"} message=hit module=http time=2022-08-03T12:34:27.142783759Z
serviceb log={"level":"warn"} message="this shouldn't happen" module=grpc time=2022-08-03T12:34:27.428810856Z
servicea log={"level":"warn"} message=seriously?!? module=grpc time=2022-08-03T12:34:28.119661968Z
serviceb log={"level":"fatal"} message=fatal module=http time=2022-08-03T12:34:28.142497379Z
servicea log={"level":"panic"} message=panic! module=http time=2022-08-03T12:34:29.142768807Z
serviceb log={"level":"debug"} message=wat module=search time=2022-08-03T12:34:29.157192604Z
servicea elapsed=8.268013 log={"level":"debug"} message=request method=GET module=search statusCode=200 time=2022-08-03T12:34:29.165763321Z url={"domain":"localhost","full":"www.example.com","original":"https://original.url","path":"/yo","port":80,"scheme":"http"}
23 changes: 15 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ const (
)

type Cmd struct {
Level []string `short:"l" long:"level" description:"only output lines at this level"`
Strict bool `short:"s" long:"strict" description:"strict mode"`
Flat bool `short:"f" long:"flat" description:"flatten output"`
Vert bool `short:"v" long:"vert" description:"vertical output"`
Plain bool `short:"p" long:"plain" description:"plain (no color) output"`
Level []string `short:"l" long:"level" description:"only output lines at this level"`
Strict bool `short:"s" long:"strict" description:"strict mode"`
Flat bool `short:"f" long:"flat" description:"flatten output"`
Vertical bool `short:"v" long:"vertical" description:"vertical output"`
Plain bool `short:"p" long:"plain" description:"plain (no color) output"`
NoPin bool `short:"n" long:"no-pin" description:"don't pin any fields"`
}

func main() {
Expand All @@ -40,12 +41,18 @@ func main() {
bufSize = 0
}

w := writer.New(
opts := []writer.Option{
writer.WithLevelFilters(cmd.Level),
writer.WithFlatten(cmd.Flat),
writer.WithVertical(cmd.Vert),
writer.WithVertical(cmd.Vertical),
writer.WithColor(!cmd.Plain),
)
}

if cmd.NoPin {
opts = append(opts, writer.WithPinOrder([]string{}))
}

w := writer.New(opts...)
// 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
Expand Down
34 changes: 25 additions & 9 deletions pkg/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,30 +203,46 @@ func New(options ...Option) Writer {

// Ensure default formatters, if not specified in input
if _, ok := w.formatter[PinTimestamp]; !ok {
w.formatter[PinTimestamp] = formatter.NewTimestamp(w.color, w.formatKey, w.timeFormat)
f := formatter.NewTimestamp(w.color, w.formatKey, w.timeFormat)
w.formatter[PinTimestamp] = f
if gu.Includes(w.pinOrder, PinTimestamp) {
w.excludeKeys = append(w.excludeKeys, f.ExcludeKeys()...)
}
}
if _, ok := w.formatter[PinLevel]; !ok {
w.formatter[PinLevel] = formatter.NewLevel(w.color, w.formatKey)
f := formatter.NewLevel(w.color, w.formatKey)
w.formatter[PinLevel] = f
if gu.Includes(w.pinOrder, PinLevel) {
w.excludeKeys = append(w.excludeKeys, f.ExcludeKeys()...)
}
}
if _, ok := w.formatter[PinMessage]; !ok {
w.formatter[PinMessage] = formatter.NewMessage(w.color, w.formatKey)
f := formatter.NewMessage(w.color, w.formatKey)
w.formatter[PinMessage] = f
if gu.Includes(w.pinOrder, PinMessage) {
w.excludeKeys = append(w.excludeKeys, f.ExcludeKeys()...)
}
}
if _, ok := w.formatter[PinCaller]; !ok {
w.formatter[PinCaller] = formatter.NewCaller(w.color)
f := formatter.NewCaller(w.color)
w.formatter[PinCaller] = f
if gu.Includes(w.pinOrder, PinCaller) {
w.excludeKeys = append(w.excludeKeys, f.ExcludeKeys()...)
}
}
if _, ok := w.formatter[PinError]; !ok {
w.formatter[PinError] = formatter.NewError(w.color, w.formatKey)
f := formatter.NewError(w.color, w.formatKey)
w.formatter[PinError] = f
if gu.Includes(w.pinOrder, PinError) {
w.excludeKeys = append(w.excludeKeys, f.ExcludeKeys()...)
}
}

// Ensure default extractor
if w.fielder == nil {
w.fielder = formatter.Map(w.formatKey)
}

for _, v := range w.formatter {
w.excludeKeys = append(w.excludeKeys, v.ExcludeKeys()...)
}

return w
}

Expand Down

0 comments on commit d4e9241

Please sign in to comment.