pinojs/pino

TypeError: Cannot read properties of undefined (reading 'Symbol(pino.msgPrefix)')

Closed this issue · 5 comments

Upon trying a log.info passed as a value argument to a function creating a closure that captures that value... I get this strange error, complaining it has no context. Almost as if it was dropped at some point, could it be caused by the closure in question?

const logger = <some_init_pino_logger>

const createCurlLogger = (logLabel, logFunc, opts) => {
  if (!opts.providerLoggingEnabled) {
    return _ => {}
  }

  return request => {
    const { method, url, headers, body } = request
    const headersString = Object.keys(headers)
          .map((key) => `-H "${key}: ${headers[key]}"`)
          .join(' ')

    const bodyString = body ? `-d '${JSON.stringify(body)}'` : ''
    const curlStyle = `curl -X ${method} --location '${url}' ${headersString} ${bodyString}`

    const logObject = {
        label: logLabel,
        request: request,
        curlStyle: curlStyle
    }

    const message = JSON.stringify(logObject)
    logFunc(message) // <- pino logger losing it's context 
  }
}

const log  = createCurlLogger("EXAMPLE_LOG", logger.info, { providerLoggingEnabled: true })
log({ method: "POST", headers: {"Accept-Encoding": "gzip"}, body: { username: "foo", password: "pwd" } })

lol

Thanks for reporting!

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

I found this example to be enough to trigger the issue:

const pinoLogger = require('pino')()

const createCustomLogger = (logLabel, logFunc) => {
    if (!logLabel)
        return _ => {}

    return data => {
        const logObject = {
            label: logLabel,
            body: typeof data === 'object' ? data : {},
            str: typeof data === 'object' ? JSON.stringify(data) : data?.toString(),
        }
        logFunc(logObject)
    }
}

const log = createCustomLogger('EXAMPLE LOGGER', pinoLogger.info)
log({ foo: 'bar' })

Executing it:

[rob@rob-precisiontower3420 pino-issue]$ ls
index.js  node_modules  package.json  package-lock.json
[rob@rob-precisiontower3420 pino-issue]$ node index.js 
/home/rob/repos/pino-issue/node_modules/pino/lib/tools.js:59
      if (typeof this[msgPrefixSym] === 'string' && msg !== undefined && msg !== null) {
                     ^

TypeError: Cannot read properties of undefined (reading 'Symbol(pino.msgPrefix)')
    at LOG (/home/rob/repos/pino-issue/node_modules/pino/lib/tools.js:59:22)
    at /home/rob/repos/pino-issue/index.js:13:9
    at Object.<anonymous> (/home/rob/repos/pino-issue/index.js:18:1)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.15.0

I'm using the latest pino @ version 8.15.1 btw

This is normal and expected, the log functions in pino are not bound to the current object.

Ok got it, thank you!

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.