nestjs-pino icon indicating copy to clipboard operation
nestjs-pino copied to clipboard

[BUG] DataCloneError when messageFormat is functions

Open zaro opened this issue 2 years ago • 2 comments

[x] I've read the docs of nestjs-pino

[x] I've read the docs of pino

[x] I couldn't find the same open issue of nestjs-pino

What is the current behavior?

Basically the same as https://github.com/iamolegga/nestjs-pino/issues/353. I have the following configuration for nest-pino:

    LoggerModule.forRoot({
      //forRoutes: [],
      pinoHttp: {
        formatters: {
          level(level) {
            return { level };
          },
        },
        // level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
        level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
        transport:
          process.env.NODE_ENV !== 'production'
            ? {
                target: 'pino-pretty',
                options: {
                  translateTime: true,
                  colorize: true,
                  ignore: 'pid,hostname',
                  messageFormat: (log, messageKey) => {
                    if ('req' in log) {
                      return `[${log.context}] ${log[messageKey]} ${log.req.url}`;
                    }
                    return `[${log.context}] ${log[messageKey]}`;
                  },
                },
              }
            : undefined,
      },
    }),

With this config nestjs fails to start with the following error:

∴ nest start
[Nest] 626822  - 02/14/2022, 6:58:19 PM     LOG [NestFactory] Starting Nest application...
[Nest] 626822  - 02/14/2022, 6:58:19 PM   ERROR [ExceptionHandler] (log, messageKey) => {
                                    if ('req' in log) {
                                ...<omitted>... } could not be cloned.
DataCloneError: (log, messageKey) => {
                                    if ('req' in log) {
                                ...<omitted>... } could not be cloned.
    at new Worker (node:internal/worker:235:17)

What is the expected behavior?

nest should start and log messages.

Please provide minimal example repo. Without it this issue will be closed

https://github.com/zaro/nestjs-pino-bug

Please mention other relevant information such as Node.js version and Operating System.

node v16.13.1 , Fedora 35

zaro avatar Feb 14 '22 17:02 zaro

Easy way with warning

    LoggerModule.forRoot({
      pinoHttp: {
        prettyPrint: {
          colorize: true,
          timestampKey: 'time',
          messageFormat,
          translateTime: 'HH:MM:ss.l',
          ignore: 'context,pid,hostname,responseTime',
        },
      },
    }),

But it will be also better solution without warning use piano transport as separate file:

// ./pino-pretty-transport.js in root of project, yes same level with src

const pinoPretty = require('pino-pretty');

module.exports = function pinoPrettyTransport(opts) {
  return pinoPretty({
    ...opts,
    messageFormat(log, messageKey, levelLabel) {
      if (log.req)
        return `${log.req.method} ${log.req.url} - ${log.responseTime}ms`;
      return `${log.msg}`;
    },
  });
};

and

    LoggerModule.forRoot({
      pinoHttp: {
        transport: {
          target: require.resolve('../pino-pretty-transport'),
          options: {
            colorize: true,
            timestampKey: 'time',
            translateTime: 'HH:MM:ss.l',
            ignore: 'context,pid,req,res,hostname,responseTime',
          },
        },
      },
    }),

extg avatar Feb 17 '22 20:02 extg

Easy way with warning

    LoggerModule.forRoot({
      pinoHttp: {
        prettyPrint: {
          colorize: true,
          timestampKey: 'time',
          messageFormat,
          translateTime: 'HH:MM:ss.l',
          ignore: 'context,pid,hostname,responseTime',
        },
      },
    }),

But it will be also better solution without warning use piano transport as separate file:

// ./pino-pretty-transport.js in root of project, yes same level with src

const pinoPretty = require('pino-pretty');

module.exports = function pinoPrettyTransport(opts) {
  return pinoPretty({
    ...opts,
    messageFormat(log, messageKey, levelLabel) {
      if (log.req)
        return `${log.req.method} ${log.req.url} - ${log.responseTime}ms`;
      return `${log.msg}`;
    },
  });
};

and

    LoggerModule.forRoot({
      pinoHttp: {
        transport: {
          target: require.resolve('../pino-pretty-transport'),
          options: {
            colorize: true,
            timestampKey: 'time',
            translateTime: 'HH:MM:ss.l',
            ignore: 'context,pid,req,res,hostname,responseTime',
          },
        },
      },
    }),

i have the same error, although, in my project i'm using typescript, is there a way to configure pino-pretty with typescript in nestjs?

do-ald533 avatar Mar 16 '22 13:03 do-ald533

OK, so this is a peculiarity of pino-pretty, right? Closing then

iamolegga avatar Jan 05 '23 19:01 iamolegga