sainsburys-tech / next-logger

JSON logging patcher for Next.js
MIT License
157 stars 14 forks source link

Usage in TypeScript? #80

Open cklat opened 5 days ago

cklat commented 5 days ago

Hi,

I came across this library due to all the limitation NextJS has with logging and trying to use it in my TypeScript. Unfortunately, not successful.

What I have tried is to follow the example for the custom logging in the documentation and use it like I would normally use the Pino Logger like so:

//next-logger.config.ts

import pino from 'pino';

const logger = (defaultConfig: any) =>
    pino({
        ...defaultConfig,
        formatters: {
            level: (label) => {
                return { level: label };
            },
        },
    }, pino.destination(`${__dirname}/logs/logfile.log`));

export default {
    logger,
};

//random-usage-file.ts

import logger from './next-logger.config';

logger.info('Some debug log');

However, I'm getting the following: Property 'info' does not exist on type '{ logger: (defaultConfig: any) => Logger<never, boolean>; }'

Any ideas how to get this to work?

dir commented 5 days ago

From my personal experience messing around with it a bit, this package seems to be very TypeScript-unfriendly.

atkinchris commented 3 days ago

We exclusively use this with TypeScript projects internally, so I'm disappointed to see you're facing issues. Because of when we need to patch Next.js in the applications lifecycle, this doesn't lend itself well to being compiled with TypeScript or referenced back into TypeScript projects.

Generally, we'd import the Pino config into this next-logger.config file, rather than it being the source of truth for Pino's config. You may need to reference the compiled output directly - as this happens at runtime, and isn't compiled.