Skip to content

Commit

Permalink
Fix formatting when timestamp is unix timestamp and not string value
Browse files Browse the repository at this point in the history
  • Loading branch information
maoueh committed Jun 17, 2022
1 parent a818964 commit e2082a6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- [Fix] Fix formatting when `timestamp` is unix timestamp and not string value

## 0.3.0 (April 21th, 2021)

- [Fix] Fix formatting when `caller` is not present
Expand Down
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (p *processor) process() {
}

if err := p.scanner.Err(); err != nil {
debugPrintln("Scanner terminated with error: %s", err)
debugPrintln("Scanner terminated with error: %w", err)
}
}

Expand Down Expand Up @@ -203,7 +203,7 @@ func (p *processor) maybePrettyPrintLine(line string, lineData map[string]interf
func (p *processor) maybePrettyPrintZapLine(line string, lineData map[string]interface{}) (string, error) {
logTimestamp, err := tsFieldToTimestamp(lineData["ts"])
if err != nil {
return "", fmt.Errorf("unable to process field 'ts': %s", err)
return "", fmt.Errorf("unable to process field 'ts': %w", err)
}

var caller *string
Expand Down Expand Up @@ -273,9 +273,10 @@ func (p *processor) maybePrettyPrintZapdriverLine(line string, lineData map[stri
}

var buffer bytes.Buffer
parsedTime, err := time.Parse(time.RFC3339, timeValue.(string))

parsedTime, err := tsFieldToTimestamp(timeValue)
if err != nil {
return "", fmt.Errorf("unable to process field 'time': %s", err)
return "", fmt.Errorf("unable to process field %q: %w", timeField, err)
}

var caller *string
Expand All @@ -290,7 +291,7 @@ func (p *processor) maybePrettyPrintZapdriverLine(line string, lineData map[stri
logger = &loggerStr
}

p.writeHeader(&buffer, &parsedTime, lineData["severity"].(string), caller, logger, lineData["message"].(string))
p.writeHeader(&buffer, parsedTime, lineData["severity"].(string), caller, logger, lineData["message"].(string))

// Delete standard stuff from data fields
delete(lineData, timeField)
Expand Down
11 changes: 11 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ func TestZapDriverNewProduction(t *testing.T) {
"[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\"}",
},
},

{
name: "line_with_timstamp_unix",
lines: []string{
`{"severity":"INFO","timestamp":"2022-06-16T23:11:09.929517378-04:00","message":"m","timestamp":1655435437,"logging.googleapis.com/labels":{}}`,
},
expectedLines: []string{
"[2022-06-16 23:10:37.000 EDT] \x1b[32mINFO\x1b[0m \x1b[34mm\x1b[0m",
},
},

{
name: "single_log_line_missing_fields",
lines: []string{
Expand Down

0 comments on commit e2082a6

Please sign in to comment.