Skip to content

Commit da05565

Browse files
authored
fix: ensure a 'message' field when no message in pino log statement (#64)
1 parent d0331a7 commit da05565

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

loggers/pino/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
- Set the "message" to the empty string for logger calls that provide no
6+
message, e.g. `log.info({foo: 'bar'})`. In this case pino will not add a
7+
message field, which breaks ecs-logging spec.
8+
59
- Fix handling when the [`base`](https://getpino.io/#/docs/api?id=base-object)
610
option is used to the pino constructor.
711

loggers/pino/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ function createEcsPinoOptions (opts) {
213213
}
214214
}
215215

216+
// If no message (https://getpino.io/#/docs/api?id=message-string) is
217+
// given in the log statement, then pino will not emit a message field.
218+
// However, the ecs-logging spec requires a message field, so we set
219+
// a fallback empty string.
220+
ecsObj.message = ''
221+
216222
return ecsObj
217223
}
218224
}

loggers/pino/test/basic.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,21 @@ test('ecsPinoFormat cases', suite => {
131131
ecs: { version: ecsVersion },
132132
message: 'hi'
133133
}
134+
},
135+
{
136+
name: 'no message in log call should result in empty string message',
137+
pinoOpts: ecsFormat(),
138+
loggingFn: (log) => {
139+
log.info({ foo: 'bar' })
140+
},
141+
rec: {
142+
'log.level': 'info',
143+
ecs: { version: ecsVersion },
144+
process: { pid: process.pid },
145+
host: { hostname: os.hostname },
146+
foo: 'bar',
147+
message: ''
148+
}
134149
}
135150
]
136151

0 commit comments

Comments
 (0)