4

From the Apify example docs, I can see that if you use console.log() within handlePageFunction, it is logged directly to the terminal console. The same is true for the Apify utils log. For example:

handlePageFunction: async ({ request, page }) => {
    console.log('This is logged to the terminal console');
    log.info('So is this.')
    ...
}

However when I am defining my own functions inside handlePageFunction this, I cannot get anything logged to the terminal console. I expect the console.log is going to the browser console, but in Apify the browser window appears and disappears quickly. (It's also possible to use apify-cli which wouldn't even show a browser at all.)

handlePageFunction: async ({ request, page }) => {
  ...
  const myFunction = () => {
    console.log('This disappears into the ether.');
    log.info('This causes the script to fail with error, "log is not defined"');
  }
  myFunction();
}

How do I use the Apify utils.log from within a nested javascript function?

Kit Johnson
  • 541
  • 2
  • 7
  • 15

1 Answers1

4

If you use the defined function inside a page.evaluate or any other function that is evaluated in the browser context the console.log gets outputted inside the browser console. If you want to have the browser logs displayed inside the Node.js this should work for simple logging. -

page.on('console', msg => console.log('PAGE LOG:', msg.text));
Petr
  • 66
  • 1
  • 3