Next.js Discord

Discord Forum

How to log all api requests in next.js?

Unanswered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
I have some api routes, and I want to log all requests for routes. The main information for logging:
[date]: [start] /api/user/info [user-agent]
[data]: [end] /api/user/info [user-agent]

What would be the best solution for query logging without custom server? Maybe can you recommend me some libraries?

2 Replies

if you have a centralized fetch function just add a console.log to it.
There is a nextConfig option for logging:{fetches:{fullUrl:true}} https://nextjs.org/docs/app/api-reference/next-config-js/logging But it only works in dev.
Giant Chinchilla
https://nextjs.org/docs/app/api-reference/file-conventions/middleware

Middle where is definitely the best way to do it within the nextjs framework, but sander is right it would probably jsut be eaiser to throw it into a global fetch call or something