sainsburys-tech / next-logger

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

`all` preset should only use `console` patch because next.js 12 simply directly calls the console methods. #16

Closed devinrhode2 closed 2 years ago

devinrhode2 commented 2 years ago

Original title:

next-only patch is no longer utilized in next v12

In next v12, it seems that the next-only patch now no longer does anything.

I noticed only logs from the console patch were coming through, and then I tried this:

    nextLogger[nextLoggerMethod] = () => {
      throw new Error('a')
    }

set my "start": "...script..." to just --require next-logger/presets/next-only and... nothing happened.

Although I've glanced at next.js canary branch today and see that there are calls made to this logger.

UPDATE: Next.js DOES still call these methods, obviously these are using the internal logger:

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Loaded env from /Users/me/repos/myapp/.env.production.local
info  - Loaded env from /Users/me/repos/myapp/.env.local

The patch is happening before these logs, and the logs are coming through.. but an error should be throw instead of these logs coming through

CleanShot 2022-04-14 at 21 40 20

node v16.14.2

UPDATE: Turns out this does not happen when next-logger is installed normally. I am using cd next-logger && yarn link && cd ../myapp && yarn link "next-logger" CleanShot 2022-04-14 at 21 42 26

Sad day for yarn link :)

devinrhode2 commented 2 years ago

actually, this does mean something.

The next methods just directly call console: https://github.com/vercel/next.js/blob/canary/packages/next/build/output/log.ts

In my environment, I basically don't really want to use the next patch. It's simpler to just use the console patch.

Also means I can reclaim my usage of yarn link and, nobody gets confused on how this next patching works, or gets confused with double tracking logs.. etc..

devinrhode2 commented 2 years ago

And, I'm reversing my decision. I think it's cleaner to patch the nextLogger methods, despite the quirky nature of the monkey patch.

Next passes in chalk.red('error') - into console.error and filtering that out, especially if you get encoded color data, is really annoying. Better to just avoid letting next call chalk in the first place.

devinrhode2 commented 2 years ago

Also, yalc link does not have same issue as yarn link. Go figure! yalc FTW!