elastic / ecs-logging-nodejs

https://www.elastic.co/guide/en/ecs-logging/nodejs/master/intro.html
Apache License 2.0
68 stars 39 forks source link

Using pino logger with ECS format does not log custom attributes #126

Closed ibadb closed 2 years ago

ibadb commented 2 years ago

I'm using the following code to return a pino logger instance. I want to log a custom 'name' attribute but I don't seem to get it to work. No errors. It just does not log the 'name' attribute. Everything else is working as expected. Is this the correct way to create a logger instance?

return pino(
      ecs(),
      pino.destination({
          dest: dest, 
          mkdir: true,
          sync: false
      }),
      { name: 'My Project' } // it does not work
 );

But if I change the code and remove the ECS function, it starts working as expected:

return pino(
      { name: 'My Project' },
      pino.destination({
          dest: dest, 
          mkdir: true,
          sync: false
      })
 );
trentm commented 2 years ago

Hi @ibadb, thanks for the question.

The pino Logger constructor doesn't accept a third argument, so your first example can't work.

To set options.name along with the pino options created by @elastic/ecs-pino-format, you will need to merge the two sets of options together and pass them to pino(). Something like this should work:

const pino = require('pino')
const ecsPinoFormat = require('@elastic/ecs-pino-format')
const log = pino({ name: 'myName', ...ecsPinoFormat() })
log.info('hi')

Running that results in:

% node play-pino.js
{"log.level":"info","@timestamp":"2022-06-02T21:18:51.563Z","process":{"pid":59472},"host":{"hostname":"pink.local"},"log":{"logger":"myName"},"ecs":{"version":"1.6.0"},"message":"hi"}

Please let me know if I've misunderstood your question.