pinojs / pino-socket

🌲 A transport for sending pino logs to network sockets
43 stars 14 forks source link

onSocketClose option cannot be used with ThreadStream (multistream) #77

Closed ggrossetie closed 2 years ago

ggrossetie commented 2 years ago

Given the following code:

const pino = require('pino')

const tcpStream = pino.transport({
  level: "info",
  target: "pino-socket",
  options: {
    address: "localhost",
    port: "13370",
    mode: "tcp",
    reconnect: true,
    onSocketClose: () => { console.log('') }
  }
})

tcpStream.on('error', () => { /* ignore */ })
const pinoStreams = [
  {
    level: "debug",
    stream: pino.transport({
      target: "pino/file"
    })
  },
  {
    stream: tcpStream
  }
]

const logger = pino({
  level: "info",
}, pino.multistream(pinoStreams))

logger.info("Keep alive!")

The following exception is thrown:

    this[kPort].postMessage({
                ^

DOMException [DataCloneError]: () => { console.log('') } could not be cloned.
    at new Worker (node:internal/worker:235:17)
    at createWorker (/path/to/project/node_modules/thread-stream/index.js:50:18)
    at new ThreadStream (/path/to/project/node_modules/thread-stream/index.js:209:19)
    at buildStream (/path/to/project/node_modules/pino/lib/transport.js:21:18)
    at Function.transport (/path/to/project/node_modules/pino/lib/transport.js:110:10)
    at Object.<anonymous> (/path/to/project/index.js:3:25)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
mcollina commented 2 years ago

That's expected. Functions cannot be serialized across worker thread boundaries. You'll need to use the programmatic API and create your own transport that reuse this one if you want to use that.

mcollina commented 2 years ago

That's expected. Functions cannot be serialized across worker thread boundaries. You'll need to use the programmatic API and create your own transport that reuse this one if you want to use that.

ggrossetie commented 2 years ago

I think we should add a note in the documentation.

You'll need to use the programmatic API and create your own transport that reuse this one if you want to use that.

Do we already have some documentation on how to proceed?

ggrossetie commented 2 years ago

@mcollina Maybe we should emit an event socketClose instead of defining a callback. It's more portable, what do you think?

mcollina commented 2 years ago

go for it, keep the callback too