pinojs / pino-elasticsearch

🌲 load pino logs into Elasticsearch
MIT License
179 stars 67 forks source link

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

Closed mr7r25 closed 3 years ago

mr7r25 commented 4 years ago

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 commented 4 years ago

cc @delvedor

delvedor commented 4 years ago

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

mcollina commented 4 years ago

yes

rclayton-godaddy commented 3 years ago

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 },
]));