Winston: How to log JSON to console and get cloudwatch logs pick up correct metadata?

Created on 16 Feb 2017  路  1Comment  路  Source: winstonjs/winston

winston.info('Hello again distributed logs', { app: 'test', sid: 1 })

pino.info('Hello again distributed logs', { app: 'test', sid: 1 })

Output:

{"app":"test","sid":1,"level":"info","message":"Hello again distributed logs","timestamp":"2017-02-16T03:34:06.186Z"}
{"pid":4269,"hostname":"Macchiato.local","level":30,"time":1487216046197,"msg":"Hello again distributed logs {\"app\":\"test\",\"sid\":1}","v":1}

I have fluentd daemon to pickup container stdout and ship it to cloudwatch.

In the example above, cloudwatchlogs can detect the metadata correctly from pino and I will be able to query the log like this

{$.app = "test"}

However, winston console is not working like that and everything got push to log attribute.

Does anyone know how to fix this. I'm using winston console with json: true and stringify: true

2.x only

Most helpful comment

I concur with what @davidmarkclements said in https://github.com/pinojs/pino/issues/194#issuecomment-280676307

At a guess I'd say cloudwatch is parsing the msg field and then running the query agains the object found in the msg field?

In [email protected] the default format was:

`${message} ${JSON.stringify(meta)`

so if you continue to use 2.x you want to use the { json: true } for your Console transport. If you upgrade to [email protected] JSON is the default transport so this will just work as is out of the box. 馃樃 馃樃 馃樃

>All comments

I concur with what @davidmarkclements said in https://github.com/pinojs/pino/issues/194#issuecomment-280676307

At a guess I'd say cloudwatch is parsing the msg field and then running the query agains the object found in the msg field?

In [email protected] the default format was:

`${message} ${JSON.stringify(meta)`

so if you continue to use 2.x you want to use the { json: true } for your Console transport. If you upgrade to [email protected] JSON is the default transport so this will just work as is out of the box. 馃樃 馃樃 馃樃

Was this page helpful?
0 / 5 - 0 ratings