Strapi logging integration in Puppeteer fails to display console messages inside page.evaluate. Although external logs work, inner logs go missing. What is causing this?
async function runProcess(loggerFunc) {
const browserInstance = await require('puppeteer').launch({ headless: true });
const pageHandle = await browserInstance.newPage();
if (loggerFunc) pageHandle.on('console', message => loggerFunc('Log:', message.text()));
await pageHandle.goto('https://example.com', { waitUntil: 'networkidle0' });
await pageHandle.evaluate(() => { console.info('Operation complete'); });
await browserInstance.close();
}
maybe the page closes too fast. i had issues where the console event didnt flush before browser.ctrl was closed. try a slight delay after the evaluate so the listener actually catches the log.
i think the listener might not be fully attached when page.evaluate runs, so its logs slip by. u could try using page.exposeFunction to catch console msgs instead
In my experience, the issue stems from the asynchronous nature of page.evaluate combined with the timing of event registration. I once encountered a similar problem where console events within page.evaluate were not captured because the event listener was not fully active at the time the console statement executed. A potential solution I discovered was to ensure the event listener is attached well before navigating or running evaluate. This can be achieved by adding a delay or attaching multiple listeners, including device-specific ones, to capture different types of console logs reliably.
I encountered a similar problem where inner console messages were not captured by Strapi logging. After some digging, I found that the console event might not be fully synchronized with the execution context of page.evaluate. In my setup, delaying the evaluate call until the listener was confirmed active resolved the issue. Also, I discovered that using a wrapper function to manually route the logging from the browser context to Node can help ensure no messages are missed. This approach provided a more consistent and reliable capture of all console outputs.