pinojs / pino-pretty

🌲Basic prettifier for Pino log lines
MIT License
1.25k stars 147 forks source link

DOMException [DataCloneError]: e=>e.toString() could not be cloned #457

Closed neolectron closed 1 year ago

neolectron commented 1 year ago

Hello, I'm trying to use pino-pretty as a transport module for pino.

Everything is up to date with latest npm (pino + pino-pretty). I'm using esbuild.

I'm doing this :

import createLogger from 'pino';

const defaultLogger = createLogger({
  transport: {
    target: 'pino-pretty',
    options: {
      customPrettifiers: {
        module: (moduleName) => moduleName.toString(),
      },
    },
  },
});

export const coreLogger = defaultLogger.child({ module: 'core' });

Which give me the error below :

node:internal/per_context/domexception:53
    ErrorCaptureStackTrace(this);
    ^
DOMException [DataCloneError]: e=>e.toString() could not be cloned.
    at new DOMException (node:internal/per_context/domexception:53:5)
    at new Worker (node:internal/worker:266:17)
    at createWorker (/home/neolectron/Projects/discord-bot/node_modules/.pnpm/thread-stream@2.4.0/node_modules/thread-stream/index.js:52:18)
    at new ThreadStream (/home/neolectron/Projects/discord-bot/node_modules/.pnpm/thread-stream@2.4.0/node_modules/thread-stream/index.js:227:19)
    at buildStream (/home/neolectron/Projects/discord-bot/node_modules/.pnpm/pino@8.15.1/node_modules/pino/lib/transport.js:21:18)
    at transport (/home/neolectron/Projects/discord-bot/node_modules/.pnpm/pino@8.15.1/node_modules/pino/lib/transport.js:114:10)
    at normalizeArgs (/home/neolectron/Projects/discord-bot/node_modules/.pnpm/pino@8.15.1/node_modules/pino/lib/tools.js:312:16)
    at pino (/home/neolectron/Projects/discord-bot/node_modules/.pnpm/pino@8.15.1/node_modules/pino/pino.js:87:28)
    at <anonymous> (/home/neolectron/Projects/discord-bot/src/core/logger.ts:40:27)
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)

Node.js v20.6.1

I'm guessing this is because pino is using StructuredClone internally and the function cannot be cloned.

The intention I had behind was just to have the module key between brackets.

I haven't found anything in the documentation nor on google that tells me not to do that.

Any idea ? Thanks for your support.

mcollina commented 1 year ago

You cannot pass a function as an option through a worker thread boundary. You have to create a worker.js that use pino-pretty directly.

neolectron commented 1 year ago

Thank you for your confirmation, I didn't found any ressources saying that so I wasn't sure.

I just found out it what's described there, thanks for the reply.