Skip to content

Commit

Permalink
Added serviceContext and logging.googleapis.com/labels as removed…
Browse files Browse the repository at this point in the history
… fields and added `-all` flag

- The `-all` flag can now be used to show filtered out fields.
- The README has been improved to notes the new flag and the default filtered fields for Zapdriver.
- Added a CHANGELOG.md file to list changes that happened recently.
  • Loading branch information
Matthieu Vachon committed Aug 26, 2020
1 parent 5291d02 commit 4391465
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Unreleased

- [Improvement] The flag `-all` (or `--all`) can now be used to show fields that are filtered out by default
- [Improvement] More fields are filtered by defaults for Zapdriver format (`labels`, `serviceContext`, `logging.googleapis.com/labels` & `logging.googleapis.com/sourceLocation` are filtered out by default now).
- [Fix] Stacktrace are now properly printed when your log format is the Zap production standard one.
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ Simply pipe the output of the CLI tool generating Zap JSON log lines to the `zap
zap_instrumented | zap-pretty
```

The tool supports Zap standard production format as well as the Zapdriver standard format (for
consumption by Google Stackdriver).

### Zapdriver

When using the Zapdriver format, those fields are removed by default from the prettified version
to reduce the clutter of the logs:

- `labels`
- `serviceContext`
- `logging.googleapis.com/labels`
- `logging.googleapis.com/sourceLocation`

If you want to see those fields, you can use `--all` flag:

```sh
zap_instrumented | zap-pretty --all
```

### CLI Arguments

- `--all` - Show all fields of the line, even those filtered out by default for the active logger format (default `false`).
- `--version` - Show version information.

### Troubleshoot

#### No Conversion
Expand Down Expand Up @@ -80,10 +104,6 @@ To:
return config.Build()
```

### CLI Arguments

None for now.

## License

MIT License
6 changes: 5 additions & 1 deletion common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

func executeProcessorTest(lines []string) *bytes.Buffer {
func executeProcessorTest(lines []string, options ...processorOption) *bytes.Buffer {
reader := bytes.NewReader([]byte(strings.Join(lines, "\n")))
writer := &bytes.Buffer{}

Expand All @@ -16,6 +16,10 @@ func executeProcessorTest(lines []string) *bytes.Buffer {
output: writer,
}

for _, opt := range options {
opt.apply(processor)
}

processor.process()
return writer
}
Expand Down
38 changes: 32 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,33 @@ func init() {
severityToColor["fatal"] = RedFg
}

type processorOption interface {
apply(p *processor)
}

type processorOptionFunc func(p *processor)

func (f processorOptionFunc) apply(p *processor) {
f(p)
}

func withAllFields() processorOption {
return processorOptionFunc(func(p *processor) {
p.showAllFields = true
})
}

type processor struct {
scanner *bufio.Scanner
output io.Writer
scanner *bufio.Scanner
output io.Writer
showAllFields bool
}

var showAllFlag = flag.Bool("all", false, "Show ")
var versionFlag = flag.Bool("version", false, "Prints version information and exit")

var showAll = false

func main() {
flag.Parse()

Expand All @@ -66,8 +86,9 @@ func main() {
go NewSignaler().forwardAllSignalsToProcessGroup()

processor := &processor{
scanner: bufio.NewScanner(os.Stdin),
output: os.Stdout,
scanner: bufio.NewScanner(os.Stdin),
output: os.Stdout,
showAllFields: *showAllFlag,
}

processor.process()
Expand Down Expand Up @@ -215,8 +236,13 @@ func (p *processor) maybePrettyPrintZapdriverLine(line string, lineData map[stri
delete(lineData, "severity")
delete(lineData, "caller")
delete(lineData, "message")
delete(lineData, "labels")
delete(lineData, "logging.googleapis.com/sourceLocation")

if !p.showAllFields {
delete(lineData, "labels")
delete(lineData, "serviceContext")
delete(lineData, "logging.googleapis.com/labels")
delete(lineData, "logging.googleapis.com/sourceLocation")
}

errorVerbose := ""
if t, ok := lineData["errorVerbose"].(string); ok && t != "" {
Expand Down
14 changes: 13 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type logTest struct {
name string
lines []string
expectedLines []string
options []processorOption
}

func init() {
Expand Down Expand Up @@ -196,13 +197,24 @@ func TestZapDriverNewProduction(t *testing.T) {
" \tFile2a",
},
},

{
name: "line_with_filtered_labels_when_show_all_fields",
lines: []string{
`{"severity":"INFO","timestamp":"2018-12-21T23:06:49.435919-05:00","caller":"c:0","message":"m","folder":"f","labels":{},"logging.googleapis.com/sourceLocation":{"file":"f","line":"1","function":"fn"}}`,
},
expectedLines: []string{
"[2018-12-21 23:06:49.435 EST] \x1b[32mINFO\x1b[0m \x1b[38;5;244m(c:0)\x1b[0m \x1b[34mm\x1b[0m {\"folder\":\"f\",\"labels\":{},\"logging.googleapis.com/sourceLocation\":{\"file\":\"f\",\"function\":\"fn\",\"line\":\"1\"}}",
},
options: []processorOption{withAllFields()},
},
})
}

func runLogTests(t *testing.T, tests []logTest) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
writer := executeProcessorTest(test.lines)
writer := executeProcessorTest(test.lines, test.options...)

outputLines := strings.Split(writer.String(), "\n")
require.Equal(t, test.expectedLines, outputLines)
Expand Down

0 comments on commit 4391465

Please sign in to comment.