mcollina / pino-roll

A Pino transport that automatically rolls your log files
MIT License
45 stars 11 forks source link

[Bug]: Passing a function to file option crashes #76

Closed lucassilvas1 closed 6 months ago

lucassilvas1 commented 6 months ago

Description

Passing a function that returns a string to the file property of options throws an error.

To reproduce

const path = require("path");
const pino = require("pino");

const logger = pino(
  pino.transport({
    target: "pino-roll",
    options: {
      file: dummyGetLogFilePath,
      mkdir: true,
      extension: ".log",
    },
  })
);

function dummyGetLogFilePath() {
  return path.join("E:/foo/", "filename");
}

Current behavior

Throws an error, I assume because somewhere pino creates a worker thread that tries to clone the transport options for pino-roll and functions can't be cloned:

node:internal/worker:266
    this[kPort].postMessage({
                ^
DOMException [DataCloneError]: function dummyGetLogPath() {
  return path.join("E:/foo/", "filename");
} could not be cloned.
    at new DOMException (node:internal/per_context/domexception:53:5)
    at new Worker (node:internal/worker:266:17)
    at createWorker (C:\Users\Lucas\pino-roll-issue\node_modules\thread-stream\index.js:55:18)
    at new ThreadStream (C:\Users\Lucas\pino-roll-issue\node_modules\thread-stream\index.js:230:19)
    at buildStream (C:\Users\Lucas\pino-roll-issue\node_modules\pino\lib\transport.js:21:18)
    at Function.transport (C:\Users\Lucas\pino-roll-issue\node_modules\pino\lib\transport.js:114:10)
    at Object.<anonymous> (C:\Users\Lucas\pino-roll-issue\index.js:63:8)
    at Module._compile (node:internal/modules/cjs/loader:1369:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
    at Module.load (node:internal/modules/cjs/loader:1206:32)

Expected behavior

The function passed is called to get the name of the log files.

mcollina commented 6 months ago

This is expected. You'd need to wrap this into your own transport to execute code in a different thread.