1

I want to use strapi's log in puppeteer.

The strapi configuration is complete, and it is used well in the strapi internal code.

But even though I passed the log function to puppeteer, it does not work.
The output is printed to the console, but the message inside puppeteer's evaluate is not visible.

What is the problem?

The main functions and strapi settings are attached below.

and minimal reproducible example is https://github.com/bgyooPtr/test-puppeteer

// puppeteer code
export const test = async (log?) => {
  const cluster = getCluster();

  const jobData = {
    url: 'https://google.com',
  };

  const data = await cluster.execute(jobData,
    async ({ page, data }) => {
      const { url } = data;
      page.setDefaultNavigationTimeout(0);
      log && page.on('console', (msg) => log('[Log]:', msg.text()));
      await page.goto(url, { waitUntil: 'load' });

      const results = await page.evaluate(async () => {
        const tic = Date.now();
        try {
        } catch (err) {
          console.error('Detail fetch error:', err);
        }

        // const data = [];
        // ...somethings

        console.log(`Elapsed time: ${Date.now() - tic}ms`);

        return data;
      }); // evaluate

      return results;
    }); // cluster.execute
  return data;
};
// call puppeteer function in cron tab
await test(strapi.log.debug.bind(strapi.log));
// result in console
strapi  |  debug: [Log]:
strapi  |  debug: [Log]:
strapi  |  debug: [Log]:

setting

// ./config/middleware.ts
export default [
  {
    name: 'strapi::logger',
    config: {
      level: 'debug',
      exposeInContext: true,
      requests: true,
    },
  },
...
// ./config/logger.ts
import { winston, formats } from '@strapi/logger';
const { prettyPrint } = formats;

export default {
  transports: [
    new winston.transports.Console({
      level: 'debug',
      format: winston.format.combine(
        winston.format.errors({ stack: true }),
        prettyPrint({ timestamps: 'YYYY-MM-DD hh:mm:ss.SSS' })
      ),
    }),
  ],
};
// puppeteer cluster settings
import { Cluster } from 'puppeteer-cluster';

let cluster: Cluster<any> | null = null;

export async function initCluster() {
  if (!cluster) {
    cluster = await Cluster.launch({
      concurrency: Cluster.CONCURRENCY_PAGE,
      maxConcurrency: 5,
      puppeteerOptions: {
        headless: true,
        executablePath: '/usr/bin/chromium-browser',
        args: [
          '--no-sandbox',
          '--disable-setuid-sandbox',
          '--disable-dev-shm-usage',
          '--disable-gpu',
        ],
        ignoreHTTPSErrors: true,
      },
      timeout: 60 * 1000,
      monitor: true,
    });

    cluster.on('taskerror', (err, data) => {
      console.error(`Error in task: ${err.message}`, data);
    });
  }
  return cluster;
}

export function getCluster(): Cluster<any> {
  if (!cluster) {
    throw new Error('Cluster is not initialized. Call initCluster() first.');
  }
  return cluster;
}
2
  • 1
    Avoid page.setDefaultNavigationTimeout(0);, it can hang your script forever. Better to set a sensible, 2-3 minute timeout. If a nav doesn't complete in that time, it never will, and it's best to throw so you can fix the problem. As for your main question, Puppeteer's page logs just log whatever the site is logging. If the site is logging empty strings, that would explain your logs. The code looks OK at a glance otherwise. A complete, runnable minimal reproducible example with a package.json would make it easier to step in and help here. Commented Jan 13 at 7:00
  • Thanks but it should be in the question itself. Commented Jan 13 at 15:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.