Pino: Functional programming not working

Created on 21 Dec 2018  路  1Comment  路  Source: pinojs/pino

Description

To avoid duplication I would like to give pino().debug, pino().info and pino().error into a function. I constructed the following minimal example (the test succeeds for error and info, i.e. these two throw)

Test code

import {expect} from "chai";
import "mocha";
import * as pino from "pino";

it("cannot give pino log function into other function as parameter", () => {
    const log = (logFn: pino.LogFn, msg: string) => logFn(msg);

    const resultFn = () => log(pino().error, "my message");

    expect(resultFn).to.throw("Cannot read property 'Symbol(pino.write)' of undefined");
});

Note: To make debug fail, one needs to use pino({level: "debug"}).debug because of pino's default log level being info

Questions

  1. Why is this throwing? How do I get it to work (not throw) inside another function?

Stack trace

Calling the resultFn without catching results in the following stack trace.

  1) pino cannot give pino log function into other function as parameter:
     TypeError: Cannot read property 'Symbol(pino.write)' of undefined
      at LOG (my_local_path\node_modules\pino\lib\tools.js:40:16)
      at log (dist\logging\pino.learning.test.js:137:37)
      at resultFn (dist\logging\pino.learning.test.js:138:32)
      at Context.it (dist\logging\pino.learning.test.js:139:9)
      at <anonymous>

Context

Used "pino": "^5.6.2"
under node v8.9.4

Most helpful comment

Found the solution. The called function uses this. I have to bind the pino() to its function.

```
const logger = pino()
const resultFn = () => log(logger.error.bind(logger), "my message");

>All comments

Found the solution. The called function uses this. I have to bind the pino() to its function.

```
const logger = pino()
const resultFn = () => log(logger.error.bind(logger), "my message");

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dunklesToast picture dunklesToast  路  3Comments

callumlocke picture callumlocke  路  4Comments

glensc picture glensc  路  3Comments

yoshuawuyts picture yoshuawuyts  路  5Comments

acro5piano picture acro5piano  路  3Comments