Log requests and responses in a log file on the server
In summary, I am trying to log requests and responses to a file, a simple text file. This would be as simple as:
This is at
server/middleware/log.ts
This should work, however, nothing is getting saved to logs/log.txt. The folder doesn't even get created. Console gets a success message, and no errors occur. This is happening on my local windows development environment.
My goal is to add some form of logging to a file found on the server, within its working directory. I have a custom fetch that is used to connect to an external api. I also have a proxy set up in the nuxt config to point the custom fetch to the correct external api location. I want to record any errors that are received from the api to a file. I can use any logging package, such as pino and winston, however i tried briefly to get it to work and had a few errors from winston so just thought i just need a simple write to file at the moment and nothing major.
If you have any other ideas or suggestions, i'm all ears. Thanks!3 Replies
Hi @Kernel Panic. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
You’re writing from server middleware, which runs for every request before other server routes. It should not send a response, but it can perform side effects like logging (that part is fine) (server middleware). However:
- In dev, Nitro runs in different providers (including edge-like or serverless presets) where a writable filesystem may not persist or be available the way you expect. Prefer using Nitro hooks/plugins or ensure you’re on a Node preset and writing to an absolute path your runtime can write to (Getting started: Server / Nitro).
- For background tasks that shouldn’t block the response (like writing logs), use event.waitUntil so the request isn’t delayed (awaiting promises after response). Minimal example (Node runtime) using server middleware and waitUntil: Notes: - Don’t return anything from server middleware (server middleware). - If you also need to log responses, prefer a Nitro plugin to hook into the request lifecycle once at startup (Nitro plugins run once). If you need to forward client headers/cookies when your server calls other APIs (for correlation or auth), use event.$fetch in your server routes (forwarding context & headers). If this still writes nothing: - Confirm you’re running with a Node preset that allows filesystem writes. - Log absolute paths to verify where you’re writing (console log is fine). - Move the logic into server/plugins to ensure it initializes once and add try/catch with error logging. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
- For background tasks that shouldn’t block the response (like writing logs), use event.waitUntil so the request isn’t delayed (awaiting promises after response). Minimal example (Node runtime) using server middleware and waitUntil: Notes: - Don’t return anything from server middleware (server middleware). - If you also need to log responses, prefer a Nitro plugin to hook into the request lifecycle once at startup (Nitro plugins run once). If you need to forward client headers/cookies when your server calls other APIs (for correlation or auth), use event.$fetch in your server routes (forwarding context & headers). If this still writes nothing: - Confirm you’re running with a Node preset that allows filesystem writes. - Log absolute paths to verify where you’re writing (console log is fine). - Move the logic into server/plugins to ensure it initializes once and add try/catch with error logging. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@246664150514663425> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
I tried creating a nitro plugin with a request hook and write to a file but that too didn't work. No errors, however it doesn't do anything.