winston logger not working in middleware #50509
Replies: 7 comments 3 replies
-
I've observed this, too. @axelvaindal, did you find any workaround or a replacement logger? |
Beta Was this translation helpful? Give feedback.
-
Pino seems to be working for me. |
Beta Was this translation helpful? Give feedback.
-
Is there any update on this? I'd love to improve logging in my middleware |
Beta Was this translation helpful? Give feedback.
-
Would love to see this working also ... or provide something out of the box with some more configuration options ? e.g. structured logging ? |
Beta Was this translation helpful? Give feedback.
-
still doesn't work because of edge runtime 😢 |
Beta Was this translation helpful? Give feedback.
-
I haven't tried this yet, going to try this weekend but there's a workaround suggested on Winston repo: winstonjs/winston#2327 (comment) with a caveat. They suggested adding the following block to "overrides": {
"@colors/colors": "1.6.0"
}, |
Beta Was this translation helpful? Give feedback.
-
Thought it might be useful for some people - A workaround (Until they sort the issue with edge runtime) to get Winston logger working in NextJS13+ is to set some headers on the response in the middleware, and then call your logger method in the root Layout file (src/app/layout.tsx). Using Pathname as an example, but you should use the built in { params } prop to get that. Here's how you might go about it: Middleware.ts:
It was fine for my use case at least anyway :) |
Beta Was this translation helpful? Give feedback.
-
Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: x64 Version: Darwin Kernel Version 22.4.0: Mon Mar 6 21:00:17 PST 2023; root:xnu-8796.101.5~3/RELEASE_X86_64 Binaries: Node: 16.14.2 npm: 8.5.0 Yarn: 1.22.18 pnpm: N/A Relevant packages: next: 13.4.4 eslint-config-next: 13.4.4 react: 18.2.0 react-dom: 18.2.0 typescript: 5.0.4
Which area(s) of Next.js are affected? (leave empty if unsure)
No response
Link to the code that reproduces this issue or a replay of the bug
https://codesanboxisnotworkingproperly.com
To Reproduce
How to reproduce:
npm install --save winston
middleware.js
Describe the Bug
Winston relies on colors which relies dependencies that only works on
runtime: nodejs
.It seems
middleware.js
relies on Edge Runtime (if not mistaken), and thus is missing some core nodejs dependencies that are required by dependencies of winston (notably @colors/colors).Importhing these dependencies and trying to run them breaks.
See: DABH/colors.js#41
Expected Behavior
A way to opt-out of edge runtime in middleware to allow dependencies to work inside
middleware.js
.Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Beta Was this translation helpful? Give feedback.
All reactions