pinojs / pino

🌲 super fast, all natural json logger
http://getpino.io
MIT License
14.21k stars 875 forks source link

the log format are different in the two files of a same project #1659

Closed gzhhong closed 1 year ago

gzhhong commented 1 year ago

Hello,

I use pino (v8.8.0) in my nextjs (v13.0.4) project.

in the project_home/utils/Logger.js, I created the logger very simple:

const logger = require('pino')()

export default logger;

in project_home/utils/SitemapUtils.js, I used the logger by:

import logger from './Logger';

const moduleLogger = logger.child({component: "sitemaputils", function: "getSitemap"});

moduleLogger.info(`>> getSitemap ${baseUrl} and locale: ${locale}`);

The format of the logs printed here is:

{"level":30,"time":1677322914102,"pid":34946,"hostname":"james-MacBook-Pro.local","component":"sitemaputils","function":"getSitemap","msg":">> getSitemap http://127.0.0.1:8080 and locale: fr-FR"}

So everything works fine.

In the project_home/middleware.js, I use the logger as:

import logger from './utils/Logger';

export function middleware(req, ev) {
    const reqUrl = new URL(req.url);
    // create logger for this module
    const moduleLogger = logger.child({component: "middleware"});

    moduleLogger.info(`>> request ${req.url}, pathname = ${reqUrl.pathname}`);
...

But the format of the logs looks like:

{ component: 'middleware' } '>> request http://localhost:8080/fr-FR/explorer, pathname = /fr-FR/explorer'

Can anyone help me on this issue?

Thanks,

James

mcollina commented 1 year ago

This is expected. Next.js middleware are supposed to run in the Vercel Edge network, i.e. Cloudflare workers. This environment is not Node.js. Therefore you are running the browser build of Pino.

gzhhong commented 1 year ago

@mcollina , Hello mcollina, thank you for the answer. My nextjs application is deployed on my private cloud. It is containerized in docker (based on an image alpine-node-chromium) and running in openshift. At beginning I think nextjs will use the Node to run its server side logic, like the getServerSideProps. And all of the other components running in browser, but I never deeply think where the middleware will run in this case. Can you give me more detail about how the next.js will run its different components?

mcollina commented 1 year ago

You should be asking them ;).

My understanding is that the middlewares are bundled like it's a browser and not Node.js, and therefore the logs are different (unfortunately, there are too many limitations in the browser).

gzhhong commented 1 year ago

OK, thanks for the help.

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.