Skip to content

Commit

Permalink
feat: Added logfmt CLI (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Lisy committed Jun 28, 2022
1 parent f42c0eb commit dd52580
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 4 deletions.
18 changes: 16 additions & 2 deletions cmd/logfmt/logfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
gcli "github.com/getoutreach/gobox/pkg/cli"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

// Place any extra imports for your startup code here
///Block(imports)
"github.com/getoutreach/logfmt/internal/runner"
///EndBlock(imports)
)

Expand Down Expand Up @@ -50,12 +52,24 @@ func main() {
Version: oapp.Version,
Name: "logfmt",
///Block(app)

Usage: `make test | logfmt -filter <filter> -format <format>`,
Action: func(c *cli.Context) error {
r := runner.New(log, c.String("filter"), c.String("format"))
r.Run()
return nil
},
///EndBlock(app)
}
app.Flags = []cli.Flag{
///Block(flags)

&cli.StringFlag{
Name: "filter",
Usage: "filter the log. Use jq syntax",
},
&cli.StringFlag{
Name: "format",
Usage: "format the output. Use golang templates syntax",
},
///EndBlock(flags)
}
app.Commands = []*cli.Command{
Expand Down
82 changes: 82 additions & 0 deletions cmd/logfmt/logfmt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2022 Outreach Corporation. All Rights Reserved.

package main_test

import (
"bytes"
"encoding/json"
"strings"
"testing"
"time"

"gotest.tools/v3/icmd"
)

type line struct {
Level string `json:"level"`
Message string `json:"message"`
Timestamp string `json:"@timestamp"`
}

func TestLogfmtNoJSONText(t *testing.T) {
txt := "Line 1.\nLine 2.\n"

runLogfmt(t, txt, txt)
}

func TestLogfmtStructured(t *testing.T) {
var buff bytes.Buffer
enc := json.NewEncoder(&buff)

enc.Encode(line{
Level: "info",
Message: "Hello, World!",
Timestamp: (time.Time{}.Add(1 * time.Second)).Format(time.RFC3339Nano),
})

runLogfmt(t,
buff.String(),
`time="0001-01-01T00:00:01Z" level=info msg="Hello, World!"`)
}

func TestLogfmtStructuredFormat(t *testing.T) {
var buff bytes.Buffer
enc := json.NewEncoder(&buff)

enc.Encode(line{Level: "info", Message: "msg1"})
enc.Encode(line{Level: "info", Message: "msg2"})

runLogfmt(t,
buff.String(),
"msg1\nmsg2\n",
"--format", "{{ .message }}",
)
}

func TestLogfmtStructuredFilter(t *testing.T) {
var buff bytes.Buffer
enc := json.NewEncoder(&buff)

enc.Encode(line{Level: "info", Message: "info"})
enc.Encode(line{Level: "warn", Message: "warn"})
enc.Encode(line{Level: "error", Message: "error"})

runLogfmt(t,
buff.String(),
"info\nerror\n",
"--format", "{{ .message }}",
"--filter", `select(.level != "warn")`,
)
}

func runLogfmt(t *testing.T, input, output string, args ...string) {
logs := strings.NewReader(input)

args = append([]string{"run", `logfmt.go`}, args...)

result := icmd.RunCmd(icmd.Command("go", args...), icmd.WithStdin(logs))
result.Assert(t, icmd.Expected{
ExitCode: 0,
Err: output,
})
}
16 changes: 15 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ module github.com/getoutreach/logfmt
go 1.17

require (
github.com/Masterminds/sprig/v3 v3.2.2
github.com/getoutreach/gobox v1.41.5
github.com/itchyny/gojq v0.12.8
github.com/sirupsen/logrus v1.8.1
github.com/urfave/cli/v2 v2.6.0
gotest.tools/v3 v3.1.0
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand All @@ -24,12 +29,17 @@ require (
github.com/facebookgo/muster v0.0.0-20150708232844-fd3d7953fd52 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/google/go-github/v43 v43.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/honeycombio/beeline-go v1.4.1 // indirect
github.com/honeycombio/libhoney-go v1.15.8 // indirect
github.com/huandu/xstrings v1.3.1 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
github.com/itchyny/timefmt-go v0.1.3 // indirect
github.com/klauspost/compress v1.15.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand All @@ -40,6 +50,8 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/microcosm-cc/bluemonday v1.0.17 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.9.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
Expand All @@ -51,6 +63,8 @@ require (
github.com/rivo/uniseg v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/schollz/progressbar/v3 v3.8.6 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
Expand All @@ -59,7 +73,7 @@ require (
golang.org/x/crypto v0.0.0-20220408190544-5352b0902921 // indirect
golang.org/x/net v0.0.0-20220407224826-aac1ed45d8e3 // indirect
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a // indirect
golang.org/x/sys v0.0.0-20220406163625-3f8b81556e12 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
Expand Down
28 changes: 27 additions & 1 deletion go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dd52580

Please sign in to comment.