pinojs/pino

Cannot resolve worker_threads

Opened this issue · 4 comments

Cannot support stream for pretty-pino

I am using Node v20.3.0
"pino": "^8.15.0",
"pino-pretty": "^10.2.0",
"next": "13.4.5",

This is my logger.ts

import pino from 'pino';
import pretty from 'pino-pretty';

const levels = {
error: 50,
info: 20,
debug: 10,
};

const stream = pretty({
levelFirst: true,
colorize: true,
ignore: 'time,hostname,pid',
});

export const logger = pino(
{
name: 'frontend',
level: 'info',
customLevels: levels,
useOnlyCustomLevels: true,
formatters: {
level: (label) => {
return { level: label };
},
},
},
stream
);

It works well without the stream:
Screenshot 2023-09-01 at 14 16 28

But once I added, I got:
Screenshot 2023-09-01 at 14 17 11

I also did not found a lot information about the issue, thanks for your help.

Thanks for reporting!

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

Sure!
Thats my code:

import pino from 'pino';
import pretty from 'pino-pretty';

const levels = {
error: 50,
info: 20,
debug: 10,
};

const stream = pretty({
levelFirst: true,
colorize: true,
ignore: 'time,hostname,pid',
});

export const logger = pino(
{
name: 'frontend',
level: 'info',
customLevels: levels,
useOnlyCustomLevels: true,
formatters: {
level: (label) => {
return { level: label };
},
},
},
stream
);
```|
and I called it in this way:

logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.debug('debug message')

I'm pretty sure that code runs perfectly when run with Node.js, so there is something related to Next.js that you are not including. I'd recommend you to set up a repository for us to clone.

Recently ran into this as well.

Because NextJS has a feature called output standalone which analyses your code for modules that you import and then only includes them specifically in the bundle.

As I understand it, transports are imported dynamically so it doesn't know to bundle it unless you define the import for it which has it's own issues because the logger.ts runs on both browser and server the worker_threads module is not available.

Ideally the transport needs to be imported at server time somewhere.

UPDATE:

I actually ended up importing it in /pages/api/log.ts which worked for me