Skip to content

Commit

Permalink
Capture newlines in log stdlib
Browse files Browse the repository at this point in the history
Capture newlines in the stdlib adapter messages. Avoids cutting off
multi-line log messages.

See: prometheus/node_exporter#1886

Signed-off-by: Ben Kochie <superq@gmail.com>
  • Loading branch information
SuperQ committed Nov 29, 2020
1 parent a055839 commit 2457ebf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion log/stdlib.go
Expand Up @@ -131,7 +131,7 @@ const (
logRegexpDate = `(?P<date>[0-9]{4}/[0-9]{2}/[0-9]{2})?[ ]?`
logRegexpTime = `(?P<time>[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?)?[ ]?`
logRegexpFile = `(?P<file>.+?:[0-9]+)?`
logRegexpMsg = `(: )?(?P<msg>.*)`
logRegexpMsg = `(: )?(?P<msg>(?s:.*))`
)

var (
Expand Down
20 changes: 13 additions & 7 deletions log/stdlib_test.go
Expand Up @@ -31,13 +31,13 @@ func TestStdlibAdapterUsage(t *testing.T) {
time := now.Format("15:04:05")

for flag, want := range map[int]string{
0: "msg=hello\n",
log.Ldate: "ts=" + date + " msg=hello\n",
log.Ltime: "ts=" + time + " msg=hello\n",
log.Ldate | log.Ltime: "ts=\"" + date + " " + time + "\" msg=hello\n",
log.Lshortfile: "caller=stdlib_test.go:44 msg=hello\n",
log.Lshortfile | log.Ldate: "ts=" + date + " caller=stdlib_test.go:44 msg=hello\n",
log.Lshortfile | log.Ldate | log.Ltime: "ts=\"" + date + " " + time + "\" caller=stdlib_test.go:44 msg=hello\n",
0: "msg=\"hello\\n\"\n",
log.Ldate: "ts=" + date + " msg=\"hello\\n\"\n",
log.Ltime: "ts=" + time + " msg=\"hello\\n\"\n",
log.Ldate | log.Ltime: "ts=\"" + date + " " + time + "\" msg=\"hello\\n\"\n",
log.Lshortfile: "caller=stdlib_test.go:44 msg=\"hello\\n\"\n",
log.Lshortfile | log.Ldate: "ts=" + date + " caller=stdlib_test.go:44 msg=\"hello\\n\"\n",
log.Lshortfile | log.Ldate | log.Ltime: "ts=\"" + date + " " + time + "\" caller=stdlib_test.go:44 msg=\"hello\\n\"\n",
} {
buf.Reset()
stdlog.SetFlags(flag)
Expand Down Expand Up @@ -130,6 +130,12 @@ func TestStdlibAdapterSubexps(t *testing.T) {
"file": "",
"msg": "hello world",
},
"hello\nworld": {
"date": "",
"time": "",
"file": "",
"msg": "hello\nworld",
},
"2009/01/23: hello world": {
"date": "2009/01/23",
"time": "",
Expand Down

0 comments on commit 2457ebf

Please sign in to comment.