Replies: 1 comment
-
I was able to output the response time and HTTP status by patching the server.js file generated when running npm run build. However, this patch seems fragile and not a good solution... // .next/standalone/node_modules/next/dist/server/lib/start-server.js
async function requestListener(req, res) {
const startTime = process.hrtime.bigint();
res.on('finish', ()=>{
const endTime = process.hrtime.bigint();
const responseTime = Number(endTime - startTime) / 1000000;
console.log({
statusCode: res.statusCode,
method: req.method,
url: req.url,
responseTime: responseTime
});
});
// snip... |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
I am using Next.js (v14) with App Router (Self-Hosted). To pass structured logs of each request to Datadog, I utilize middleware.ts. Here's an example of the code I am currently running in middleware.ts:
However, while middleware allows me to obtain request information, I still lack information on the response. Without this, it's unclear whether the request was successful or failed. I want to obtain the HTTP status code of the response.
Additionally, I am unable to capture error logs from exceptions that occur in server components (such as ServerActions). The error.tsx component catches them, but being a client component, it obscures the details of the error, passing only a digest. I wish to inspect the details in Datadog's error logs, but am unable to do so.
I am seeking advice on good logging practices with Next.js.
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions