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)
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
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>
Used "pino": "^5.6.2"
under node v8.9.4
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");
Most helpful comment
Found the solution. The called function uses
this. I have to bind thepino()to its function.```
const logger = pino()
const resultFn = () => log(logger.error.bind(logger), "my message");