pinojs/pino-elasticsearch

Both sending log to elastic and printing in stdout (console)

mr7r25 opened this issue · 4 comments

I Want to send my logs to elasicsearch node and also be able to see my logs in my console. Is there anyway to configure pino-elasticsearch to beahve like that?

@mcollina I believe there is a module for that. Pino multi stream maybe?

yes

Example of how to log to console and ES:

import os from 'os';

// Modify this to add labels to every log statement.
export default function getBaseLabels(): Record<string, any> {
  return {
    pid: process.pid,
    hostname: os.hostname,
    service: '<your-service>',
  };
}

const pinoElastic = require('pino-elasticsearch');
const pinoMultiStream = require('pino-multi-stream').multistream;

const streamToElastic = pinoElastic({
  // Dynamic index name (the logs- prefix uses Elastic's standard index mapping for logs).
  index: (logTime: string) => `logs-${logTime.substring(0, 10)}`,
  consistency: 'one',
  node: 'http://localhost:9200',
  'es-version': 7,
  'flush-bytes': 1000,
  'flush-interval': 30000,
});

// No Pretty Print when we are pushing logs to ES.
// You can "| pino-pretty" to get the same effect.
return pino({
  base: getBaseLabels(),
}, pinoMultiStream([
  { stream: process.stdout },
  { stream: streamToElastic },
]));