-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for messages in a nested object. (#31)
- Loading branch information
1 parent
83211ab
commit 852b8f6
Showing
14 changed files
with
215 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Nested Fields | ||
|
||
By default `jl` will ignore nested fields unless you explicitly include them: | ||
|
||
$ echo '{"msg":"Booting...", "level": "INFO", "meta": {"app": "backend", "server": 6}}' | jl | ||
INFO: Booting... | ||
|
||
$ echo '{"msg":"Booting...", "level": "INFO", "meta": {"app": "backend", "server": 6}}' | jl -f meta.app | ||
INFO: Booting... [meta.app=backend] | ||
|
||
$ echo '{"msg":"Booting...", "level": "INFO", "meta": {"app": "backend", "server": 6}}' | jl -f meta | ||
INFO: Booting... [meta.app=backend meta.server=6] | ||
|
||
|
||
Some logging formats have their message in a nested fields, like | ||
[tokio/tracing](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/format/struct.Json.html#example-output) | ||
for example, have the log message in the `.fields.message` field: | ||
|
||
$ myprogram --nested-message | ||
{"timestamp":"2022-02-15T18:47:10.821422Z","level":"INFO","fields":{"message":"shaving yaks","yaks":7},"target":"fmt_json","spans":{"yaks":7,"name":"shaving_yaks"}} | ||
{"timestamp":"2022-02-15T18:47:10.821495Z","level":"TRACE","fields":{"message":"hello! Im gonna shave a yak","excitement":"yay!"},"target":"fmt_json","spans":{"yaks":7,"name":"shaving_yaks"}} | ||
|
||
`jl` will also look for the json path `.*.message`, and when it finds `.fields.message` it will output that message and automatically include the `fields` object: | ||
|
||
$ myprogram --nested-message | jl | ||
[2022-02-15 18:47:10] INFO: shaving yaks [fields.yaks=7 target=fmt_json] | ||
[2022-02-15 18:47:10] TRACE: hello! Im gonna shave a yak [fields.excitement=yay! target=fmt_json] | ||
|
||
You can also explicitly include other nested objects as well: | ||
|
||
$ myprogram --nested-message | jl -f spans | ||
[2022-02-15 18:47:10] INFO: shaving yaks [fields.yaks=7 spans.name=shaving_yaks spans.yaks=7 target=fmt_json] | ||
[2022-02-15 18:47:10] TRACE: hello! Im gonna shave a yak [fields.excitement=yay! spans.name=shaving_yaks spans.yaks=7 target=fmt_json] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package processors | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/koenbollen/jl/stream" | ||
"github.com/koenbollen/jl/structure" | ||
"github.com/tidwall/gjson" | ||
) | ||
|
||
type NestedProcessor struct { | ||
} | ||
|
||
func (p *NestedProcessor) Detect(line *stream.Line, entry *structure.Entry) bool { | ||
result := gjson.GetBytes(line.JSON, "*.message") | ||
return result.Exists() && result.String() != "" && entry.Message == result.String() | ||
} | ||
|
||
func (p *NestedProcessor) Process(line *stream.Line, entry *structure.Entry) error { | ||
result := gjson.GetBytes(line.JSON, "*.message") | ||
path := result.Path(string(line.JSON)) | ||
if field, _, found := strings.Cut(path, ".message"); found { | ||
entry.IncludeFields = append(entry.IncludeFields, field) | ||
entry.ExcludeFields = append(entry.ExcludeFields, path) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package processors | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/koenbollen/jl/djson" | ||
"github.com/koenbollen/jl/stream" | ||
"github.com/koenbollen/jl/structure" | ||
) | ||
|
||
func TestNested(t *testing.T) { | ||
|
||
in := `{ | ||
"level": "INFO", | ||
"nested": { | ||
"message": "Hi", | ||
"user": 56 | ||
} | ||
}` | ||
|
||
raw := &bytes.Buffer{} | ||
_ = json.Compact(raw, []byte(in)) | ||
|
||
line := &stream.Line{Raw: raw.Bytes(), JSON: raw.Bytes()} | ||
entry := &structure.Entry{Message: "Hi"} | ||
djson.Unmarshal(line.JSON, entry) | ||
|
||
p := &NestedProcessor{} | ||
|
||
detected := p.Detect(line, entry) | ||
if got, want := detected, true; got != want { | ||
t.Errorf("Detect() = %v, want %v", got, want) | ||
} | ||
|
||
err := p.Process(line, entry) | ||
if err != nil { | ||
t.Errorf("Process() = %v, want nil", err) | ||
} | ||
|
||
if got, want := entry.Message, "Hi"; got != want { | ||
t.Errorf("entry.Message = %v, want %v", got, want) | ||
} | ||
if got, want := entry.IncludeFields[0], "nested"; got != want { | ||
t.Errorf("entry.IncludeFields = %v, want %v", got, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,6 @@ type Processor interface { | |
} | ||
|
||
var All = []Processor{ | ||
&NestedProcessor{}, | ||
&JournaldProcessor{}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.