Skip to content

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

Description

@svex99

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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions