Troubleshooting Strapi logging in Puppeteer scripts

Hey folks, I’m having a weird issue with Strapi logging in my Puppeteer script. I’ve got Strapi set up and it’s working fine in the main app, but when I try to use its log function in Puppeteer, it’s acting up.

The console shows some output, but the messages inside the evaluate function are missing. I’m scratching my head here!

Here’s a quick look at what I’m doing:

async function scrapeData(logger) {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  
  logger && page.on('console', msg => logger('Puppeteer:', msg.text()));
  
  await page.goto('https://example.com');
  
  const data = await page.evaluate(() => {
    console.log('This message is not showing up!');
    return document.title;
  });
  
  await browser.close();
  return data;
}

// Call the function like this
scrapeData(strapi.log.debug.bind(strapi.log));

My Strapi configuration seems fine with the logger set to debug and exposed in context. Any insight on what might be causing this issue? Thanks a bunch!

Hey Laura219, i had a similar headache w/ puppeteer logs. try using page.on(‘console’, msg => logger(‘Puppeteer:’, msg.text())) INSIDE the evaluate function. it worked for me. like this:

const data = await page.evaluate(async () => {
page.on(‘console’, msg => logger(‘Puppeteer:’, msg.text()));
console.log(‘now this should show up!’);
return document.title;
});

hope this helps!

I’ve encountered a similar issue before, and it’s likely related to how Puppeteer handles console logs within the evaluate function. The console.log inside evaluate runs in the browser context, not Node.js, so it won’t be caught by Strapi’s logger.

To capture these logs, you can modify your approach slightly. Try using page.exposeFunction to create a bridge between the browser and Node contexts. Here’s a quick example:

await page.exposeFunction('logToStrapi', (text) => logger('Puppeteer:', text));

const data = await page.evaluate(() => {
  logToStrapi('This message should now show up!');
  return document.title;
});

This method should allow you to see the logs from within the evaluate function. Hope this helps solve your issue!

I’ve dealt with similar logging challenges in Puppeteer scripts before. One workaround I found effective is using page.evaluate with a callback function instead of a regular function. This approach allows you to pass variables from the Node.js context into the browser context, including your logger.

Here’s how you could modify your code:

const data = await page.evaluate((logger) => {
  logger('This message should now appear in Strapi logs');
  return document.title;
}, (msg) => logger('Puppeteer:', msg));

This method lets you use the logger directly within the evaluate function. It’s a bit more straightforward than setting up exposed functions, in my experience. Just remember to handle any potential serialization issues if you’re passing complex objects. Give it a shot and see if it resolves your logging problem!