API resolved without sending a response with intermediate logger using `req.log.with`
nikosantis opened this issue · 1 comments
I am using Next.js 13.3.0 along with version 0.17.0 of the next-axiom library to handle API routing in my application. I have created an intermediate logger using req.log.with
to customize the logs in my API endpoint handlers. However, I am experiencing an issue where responses are not sent correctly and warnings are generated in the console.
The issue seems to be related to the use of req.log.with, as when I use it, responses are not sent correctly and requests are left hanging, resulting in the following warning in the console:
"API resolved without sending a response for /api/[endpoint], this may result in stalled requests."
This issue only occurs when I use req.log.with
, as if I only use req.log.info for logging, no warnings are generated and responses are sent correctly.
I have found a workaround by adding the following config object to my API route:
export const config = {
api: {
externalResolver: true,
},
}
This seems to resolve the issue, but I'm not sure if it's the proper implementation or not, as there is no information available in the documentation of next-axiom
.
Here are the versions I am using:
Next.js: 13.3.0
next-axiom: 0.17.0
Steps to reproduce the issue:
Create an intermediate logger using req.log.with
in an API endpoint handler in Next.js.
Make a request to that endpoint from a client.
Observe that the response is not sent correctly and warnings are generated in the console.
eg:
import type { NextApiResponse } from 'next'
import { AxiomAPIRequest, withAxiom } from 'next-axiom'
async function handler(req: AxiomAPIRequest, res: NextApiResponse) {
const logger = req.log.with({
timestamp: new Date(Date.now()).toISOString(),
env: process.env.NODE_ENV,
node_version: process.version,
})
logger.info('test', {
data: { url: req.url, method: req.method },
})
res.status(200).json({ message: 'Message sent' })
}
export default withAxiom(handler)
I was trying to create a function createLogger
:
eg:
import { Logger } from 'next-axiom'
import pckJson from 'package.json'
function createLogger(logger: Logger) {
const log = logger.with({
timestamp: new Date(Date.now()).toISOString(),
env: process.env.NODE_ENV,
node_version: process.version,
project_version: pckJson.version,
next_version: pckJson.dependencies.next,
})
return log
}
export { createLogger }
Hey @nikosantis, sorry for getting back so late -- from a quick look I'm surprised that with
would introduce these problems, I'll try to reproduce using your code, thanks for bringing this issue to our attention 🙏