sainsburys-tech / next-logger

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

Is There a Way to Handle process.stdout.write? #30

Open CHC383 opened 1 month ago

CHC383 commented 1 month ago

Starting from Next 14.2.0, Next will output request logs in dev mode (https://github.com/vercel/next.js/pull/62946), which is always enabled and uses process.stdout.write to output the logs.

I tried to create a new patch for process.stdout.write but realized that Pino also uses the same method underneath by default (I believed it is this code). Even though during the testing this recursive calling loop seems to work, but the output is not as expected, i.e. the whole JSON format message is wrapped in the msg field, instead of itself being the output.

One potential hacky workaround is to check the caller and decide whether we need to patch the call or not, but Function.prototype.caller is deprecated due to security risks, so this doesn't seem to be a viable path.

I wonder if there are any other solutions, or it just can't be solved.

devinrhode2 commented 1 month ago

Patch-package next itself would probably work. Maybe a postinstall that does a sort-of search and replace - Biome.js uses a nifty library for doing search-and-replace type commands.

Although it's probably just as easy to get a PR merged into core, which allows for doing these logging things.

Lastly, sentry has a fine logging solution for next, so they will certainly have a solution on their sdk.

Good luck.

atkinchris commented 4 weeks ago

We really need first-class support for JSON logging in Next.js itself. 😅

CHC383 commented 3 weeks ago

We really need first-class support for JSON logging in Next.js itself. 😅

There is a discussion on the Next.js side: https://github.com/vercel/next.js/discussions/63787