pinojs / pino-http

🌲 high-speed HTTP logger for Node.js
MIT License
539 stars 117 forks source link

TypeScript doesn't resolve the correct type of the logger inside controller (`req.log`) #347

Open svex99 opened 2 months ago

svex99 commented 2 months ago

Problem

I have a logger with a custom level that is used as base to create the logger passed to Express.

import pino from 'pino';
import pinoHTTP from "pino-http";

export const logger = pino<"newLevel">({
    customLevels: {
        newLevel: 100,
    },
})

const httpLogger = pinoHTTP({
    logger: logger,
});

export default httpLogger;

However when I access the log property from the request it doesn't point to the right type of the logger being used (Logger<"newLevel">), instead it points to Logger<never> (from the base pino.d.ts AFAIK).

This causes TypeScript to complain when called the method req.log.newLevel(...) inside a controller.

app.get('/test', (req, res) => {
    req.log.trace('trace log') // TypeScript doesn't complain!
    req.log.newLevel('newLevel log')
    //      \__ Property 'newLevel' does not exist on type 'Logger<never>'.ts(2339)
    res.status(200)
})

Question

How could I point TypeScript to look at the correct type of the logger inside the controllers when using custom levels?

Thanks in advance!