Skip to content

Commit

Permalink
Avoid letting logs with '\r's corrupt the output
Browse files Browse the repository at this point in the history
\n doesn't really mess up the output, but it made sense to do the same thing for fields that we do
for the message itself.  This does make things like stacktrace keys harder to read, so we might want
to make this configurable.

Fixes #2.

Before:
```
$ (for i in `seq 1 3`; do echo '{"ts":1,"level":"info","msg":"foo\r\n","key":"foo\n","another":"foo\rbar"}'; done) | jlog
↩ key:foo 31 19:00:01 foo
barother:foo
↩ key:↑ another:↑0:01 foo
↩ key:↑ another:↑0:01 foo
  3 lines read; no parse errors.
```

After:
```
$ (for i in `seq 1 3`; do echo '{"ts":1,"level":"info","msg":"foo\r\n","key":"foo\n","another":"foo\rbar"}'; done) | go run ./cmd/jlog
INFO  Dec 31 19:00:01 foo←↩ key:foo↩ another:foo←bar
INFO  Dec 31 19:00:01 foo←↩ key:↑ another:↑
INFO  Dec 31 19:00:01 foo←↩ key:↑ another:↑
  3 lines read; no parse errors.
```
  • Loading branch information
jrockway committed Jun 16, 2022
1 parent 5e1edc6 commit 50ad842
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/parse/default_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,14 @@ func (f *DefaultOutputFormatter) FormatTime(s *State, t time.Time, w *bytes.Buff
s.lastTime = t
}

func cleanupNewlines(msg string) string {
msg = strings.ReplaceAll(msg, "\n", "↩")
msg = strings.ReplaceAll(msg, "\r", "←")
return msg
}

func (f *DefaultOutputFormatter) FormatMessage(s *State, msg string, highlight bool, w *bytes.Buffer) {
msg = strings.Replace(msg, "\n", "↩", -1)
msg = cleanupNewlines(msg)
if highlight {
msg = f.Aurora.Inverse(msg).String()
}
Expand Down Expand Up @@ -137,6 +143,7 @@ func (f *DefaultOutputFormatter) FormatField(s *State, k string, v interface{},
var value []byte
switch x := v.(type) {
case string:
x = cleanupNewlines(x)
value = []byte(x)
default:
var err error
Expand Down

0 comments on commit 50ad842

Please sign in to comment.