pinojs / pino-mongodb

:evergreen_tree: Insert JSON from stdin into MongoDB
MIT License
54 stars 18 forks source link

How to customize parseLine #125

Closed hbinduni closed 1 year ago

hbinduni commented 1 year ago

Hi, i try to implement like this:

const db = {
  level: 'info',
  transport: {
    target: 'pino-mongodb',
    options: {
      uri: DB_URL,
      parseLine: async (s) => {
        const line = JSON.parse(s);
        const {level, time, pid, hostname, msg} = line;

        return {
          metadata: {_id: nanoid(), level, pid, hostname},
          timestamp: time,
          message: msg,
        };
      },
    },
  },
};

const pretty = {
  level: 'info',
  transport: {
    target: 'pino-pretty',
    options: {
      colorize: true,
      translateTime: 'SYS:isoDateTime',
      ignore: 'pid,hostname',
    },
  },
};

export const logger = pino(db);

but having this error:

DOMException [DataCloneError]: function parseLine(_x) {
          return _parseLine.apply(this, arguments);
        } could not be cloned.

how to fixed this issue? also how to apply pino-pretty alongside with pino-mongodb? i want to show in console using pino-pretty but want to save the log in mongodb too.

thank you

mcollina commented 1 year ago

It's written in the README: https://github.com/platformatic/platformatic/issues/348.

hbinduni commented 1 year ago

for reference, i can do multiple transport using code like this:

import pino from 'pino';

const db = {
  level: 'info',
  target: './mongo-transport.js',
  options: {
    collection: 'logs',
    database: 'db1',
  },
};

const pretty = {
  level: 'info',
  target: 'pino-pretty',
  options: {
    colorize: true,
    translateTime: 'SYS:isoDateTime',
    ignore: 'pid,hostname',
  },
};

const transports = pino.transport({
  targets: [db, pretty],
});

export const logger = pino(transports);