Skip to content

Commit

Permalink
feat(logparser): add slog, logrus
Browse files Browse the repository at this point in the history
Updates: #243
  • Loading branch information
ernado committed Dec 4, 2023
1 parent cf2ecae commit aef49d0
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 13 deletions.
9 changes: 9 additions & 0 deletions internal/logparser/_golden/genericjson_logrus_01.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"severity_number_str": "Info",
"severity_number": 9,
"severity_text": "info",
"body": "A group of walrus emerges from the ocean",
"timestamp": "2023-12-04T10:54:23Z",
"animal": "walrus",
"size": 10
}
8 changes: 8 additions & 0 deletions internal/logparser/_golden/genericjson_slog_01.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"severity_number_str": "Info",
"severity_number": 9,
"severity_text": "INFO",
"body": "hello world",
"timestamp": "2023-12-04T10:54:23.012381337Z",
"answer": 42
}
6 changes: 6 additions & 0 deletions internal/logparser/_golden/genericjson_slog_02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"severity_number_str": "Info",
"severity_number": 9,
"severity_text": "INFO",
"body": "No time"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"severity_number_str": "Info",
"severity_number": 9,
"severity_text": "info",
"body": "Events",
"timestamp": "2023-12-04T09:11:59.1613184Z",
Expand Down
1 change: 1 addition & 0 deletions internal/logparser/_testdata/genericjson/logrus.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"animal":"walrus","level":"info","msg":"A group of walrus emerges from the ocean","size":10,"time":"2023-12-04T13:54:23+03:00"}
2 changes: 2 additions & 0 deletions internal/logparser/_testdata/genericjson/slog.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"time":"2023-12-04T13:54:23.012381337+03:00","level":"INFO","msg":"hello world","answer":42}
{"level":"INFO","msg":"No time"}
3 changes: 2 additions & 1 deletion internal/logparser/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"time"
"unicode"

"github.com/go-faster/errors"
"github.com/go-faster/jx"
Expand Down Expand Up @@ -250,7 +251,7 @@ func (GenericJSONParser) Parse(data []byte) (*Line, error) {
return nil
}
line.SeverityText = v
line.SeverityNumber = _severityMap[rune(v[0])]
line.SeverityNumber = _severityMap[unicode.ToLower(rune(v[0]))]
case msgField:
if d.Next() != jx.String {
return addJSONMapKey(attrs, string(k), d)
Expand Down
41 changes: 30 additions & 11 deletions internal/logparser/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,47 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/go-faster/jx"
"github.com/go-faster/sdk/gold"
"github.com/stretchr/testify/require"
)

func TestGenericJSONParser_Parse(t *testing.T) {
data, err := os.ReadFile(filepath.Join("_testdata", "genericjson", "zap.jsonl"))
files, err := os.ReadDir(filepath.Join("_testdata", "genericjson"))
require.NoError(t, err, "read testdata")

var parser GenericJSONParser
scanner := bufio.NewScanner(bytes.NewReader(data))
for _, file := range files {
t.Run(file.Name(), func(t *testing.T) {
data, err := os.ReadFile(filepath.Join("_testdata", "genericjson", file.Name()))
require.NoError(t, err, "read testdata")

var i int
for scanner.Scan() {
i++
t.Run(fmt.Sprintf("Line%02d", i), func(t *testing.T) {
line, err := parser.Parse(scanner.Bytes())
require.NoError(t, err, "parse")
var parser GenericJSONParser

scanner := bufio.NewScanner(bytes.NewReader(data))

name := fmt.Sprintf("genericjson_%02d.json", i)
gold.Str(t, line.String(), name)
var i int
for scanner.Scan() {
s := strings.TrimSpace(scanner.Text())
if s == "" {
continue
}
i++
t.Run(fmt.Sprintf("Line%02d", i), func(t *testing.T) {
t.Logf("%s", s)
if !jx.Valid(scanner.Bytes()) {
t.Fatal("invalid")
}
line, err := parser.Parse([]byte(s))
require.NoError(t, err, "parse")
name := fmt.Sprintf("genericjson_%s_%02d.json",
strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())), i,
)
gold.Str(t, line.String(), name)
})
}
})
}
}
Expand Down
5 changes: 4 additions & 1 deletion internal/logparser/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (l Line) Encode(e *jx.Encoder) {
e.Field("severity_number_str", func(e *jx.Encoder) {
e.Str(l.SeverityNumber.String())
})
e.Field("severity_number", func(e *jx.Encoder) {
e.Int64(int64(l.SeverityNumber))
})
}
if l.SeverityText != "" {
e.Field("severity_text", func(e *jx.Encoder) {
Expand All @@ -47,7 +50,7 @@ func (l Line) Encode(e *jx.Encoder) {
e.Str(l.Body)
})
}
if !l.Timestamp.AsTime().IsZero() {
if !l.Timestamp.AsTime().IsZero() && l.Timestamp != 0 {
e.Field("timestamp", func(e *jx.Encoder) {
e.Str(l.Timestamp.AsTime().Format(time.RFC3339Nano))
})
Expand Down

0 comments on commit aef49d0

Please sign in to comment.