Skip to content

Commit dd52580

Browse files
author
Peter Lisy
authored
feat: Added logfmt CLI (#2)
1 parent f42c0eb commit dd52580

File tree

5 files changed

+317
-4
lines changed

5 files changed

+317
-4
lines changed

cmd/logfmt/logfmt.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import (
1313
gcli "github.com/getoutreach/gobox/pkg/cli"
1414
"github.com/sirupsen/logrus"
1515
"github.com/urfave/cli/v2"
16+
1617
// Place any extra imports for your startup code here
1718
///Block(imports)
19+
"github.com/getoutreach/logfmt/internal/runner"
1820
///EndBlock(imports)
1921
)
2022

@@ -50,12 +52,24 @@ func main() {
5052
Version: oapp.Version,
5153
Name: "logfmt",
5254
///Block(app)
53-
55+
Usage: `make test | logfmt -filter <filter> -format <format>`,
56+
Action: func(c *cli.Context) error {
57+
r := runner.New(log, c.String("filter"), c.String("format"))
58+
r.Run()
59+
return nil
60+
},
5461
///EndBlock(app)
5562
}
5663
app.Flags = []cli.Flag{
5764
///Block(flags)
58-
65+
&cli.StringFlag{
66+
Name: "filter",
67+
Usage: "filter the log. Use jq syntax",
68+
},
69+
&cli.StringFlag{
70+
Name: "format",
71+
Usage: "format the output. Use golang templates syntax",
72+
},
5973
///EndBlock(flags)
6074
}
6175
app.Commands = []*cli.Command{

cmd/logfmt/logfmt_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2022 Outreach Corporation. All Rights Reserved.
2+
3+
package main_test
4+
5+
import (
6+
"bytes"
7+
"encoding/json"
8+
"strings"
9+
"testing"
10+
"time"
11+
12+
"gotest.tools/v3/icmd"
13+
)
14+
15+
type line struct {
16+
Level string `json:"level"`
17+
Message string `json:"message"`
18+
Timestamp string `json:"@timestamp"`
19+
}
20+
21+
func TestLogfmtNoJSONText(t *testing.T) {
22+
txt := "Line 1.\nLine 2.\n"
23+
24+
runLogfmt(t, txt, txt)
25+
}
26+
27+
func TestLogfmtStructured(t *testing.T) {
28+
var buff bytes.Buffer
29+
enc := json.NewEncoder(&buff)
30+
31+
enc.Encode(line{
32+
Level: "info",
33+
Message: "Hello, World!",
34+
Timestamp: (time.Time{}.Add(1 * time.Second)).Format(time.RFC3339Nano),
35+
})
36+
37+
runLogfmt(t,
38+
buff.String(),
39+
`time="0001-01-01T00:00:01Z" level=info msg="Hello, World!"`)
40+
}
41+
42+
func TestLogfmtStructuredFormat(t *testing.T) {
43+
var buff bytes.Buffer
44+
enc := json.NewEncoder(&buff)
45+
46+
enc.Encode(line{Level: "info", Message: "msg1"})
47+
enc.Encode(line{Level: "info", Message: "msg2"})
48+
49+
runLogfmt(t,
50+
buff.String(),
51+
"msg1\nmsg2\n",
52+
"--format", "{{ .message }}",
53+
)
54+
}
55+
56+
func TestLogfmtStructuredFilter(t *testing.T) {
57+
var buff bytes.Buffer
58+
enc := json.NewEncoder(&buff)
59+
60+
enc.Encode(line{Level: "info", Message: "info"})
61+
enc.Encode(line{Level: "warn", Message: "warn"})
62+
enc.Encode(line{Level: "error", Message: "error"})
63+
64+
runLogfmt(t,
65+
buff.String(),
66+
"info\nerror\n",
67+
"--format", "{{ .message }}",
68+
"--filter", `select(.level != "warn")`,
69+
)
70+
}
71+
72+
func runLogfmt(t *testing.T, input, output string, args ...string) {
73+
logs := strings.NewReader(input)
74+
75+
args = append([]string{"run", `logfmt.go`}, args...)
76+
77+
result := icmd.RunCmd(icmd.Command("go", args...), icmd.WithStdin(logs))
78+
result.Assert(t, icmd.Expected{
79+
ExitCode: 0,
80+
Err: output,
81+
})
82+
}

go.mod

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ module github.com/getoutreach/logfmt
33
go 1.17
44

55
require (
6+
github.com/Masterminds/sprig/v3 v3.2.2
67
github.com/getoutreach/gobox v1.41.5
8+
github.com/itchyny/gojq v0.12.8
79
github.com/sirupsen/logrus v1.8.1
810
github.com/urfave/cli/v2 v2.6.0
11+
gotest.tools/v3 v3.1.0
912
)
1013

1114
require (
15+
github.com/Masterminds/goutils v1.1.1 // indirect
16+
github.com/Masterminds/semver/v3 v3.1.1 // indirect
1217
github.com/alecthomas/chroma v0.10.0 // indirect
1318
github.com/aymerick/douceur v0.2.0 // indirect
1419
github.com/beorn7/perks v1.0.1 // indirect
@@ -24,12 +29,17 @@ require (
2429
github.com/facebookgo/muster v0.0.0-20150708232844-fd3d7953fd52 // indirect
2530
github.com/fatih/color v1.13.0 // indirect
2631
github.com/golang/protobuf v1.5.2 // indirect
32+
github.com/google/go-cmp v0.5.7 // indirect
2733
github.com/google/go-github/v43 v43.0.0 // indirect
2834
github.com/google/go-querystring v1.1.0 // indirect
35+
github.com/google/uuid v1.3.0 // indirect
2936
github.com/gorilla/css v1.0.0 // indirect
3037
github.com/honeycombio/beeline-go v1.4.1 // indirect
3138
github.com/honeycombio/libhoney-go v1.15.8 // indirect
39+
github.com/huandu/xstrings v1.3.1 // indirect
40+
github.com/imdario/mergo v0.3.12 // indirect
3241
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
42+
github.com/itchyny/timefmt-go v0.1.3 // indirect
3343
github.com/klauspost/compress v1.15.1 // indirect
3444
github.com/kr/text v0.2.0 // indirect
3545
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
@@ -40,6 +50,8 @@ require (
4050
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
4151
github.com/microcosm-cc/bluemonday v1.0.17 // indirect
4252
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
53+
github.com/mitchellh/copystructure v1.0.0 // indirect
54+
github.com/mitchellh/reflectwalk v1.0.0 // indirect
4355
github.com/muesli/reflow v0.3.0 // indirect
4456
github.com/muesli/termenv v0.9.0 // indirect
4557
github.com/olekukonko/tablewriter v0.0.5 // indirect
@@ -51,6 +63,8 @@ require (
5163
github.com/rivo/uniseg v0.2.0 // indirect
5264
github.com/russross/blackfriday/v2 v2.1.0 // indirect
5365
github.com/schollz/progressbar/v3 v3.8.6 // indirect
66+
github.com/shopspring/decimal v1.2.0 // indirect
67+
github.com/spf13/cast v1.3.1 // indirect
5468
github.com/ulikunitz/xz v0.5.10 // indirect
5569
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
5670
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
@@ -59,7 +73,7 @@ require (
5973
golang.org/x/crypto v0.0.0-20220408190544-5352b0902921 // indirect
6074
golang.org/x/net v0.0.0-20220407224826-aac1ed45d8e3 // indirect
6175
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a // indirect
62-
golang.org/x/sys v0.0.0-20220406163625-3f8b81556e12 // indirect
76+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
6377
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
6478
google.golang.org/appengine v1.6.7 // indirect
6579
google.golang.org/protobuf v1.28.0 // indirect

go.sum

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)