在 Apify 中,如何从嵌套函数中登录到控制台?
In Apify, how do I log to the console from within a nested function?
从 Apify 示例文档中,我可以看到如果您在 handlePageFunction
中使用 console.log()
,它会直接记录到终端控制台。 Apify 实用程序 log
也是如此。例如:
handlePageFunction: async ({ request, page }) => {
console.log('This is logged to the terminal console');
log.info('So is this.')
...
}
然而,当我在 inside handlePageFunction this 中定义自己的函数时,我无法将任何内容记录到终端控制台。我希望 console.log 进入浏览器控制台,但在 Apify 中,浏览器 window 出现并迅速消失。 (也可以使用 apify-cli,它甚至根本不显示浏览器。)
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();
}
如何在嵌套的 javascript 函数中使用 Apify utils.log?
如果您在 page.evaluate
中使用定义的函数或在浏览器上下文中评估的任何其他函数,则 console.log
会在浏览器控制台中输出。如果您想在 Node.js 内显示浏览器日志,这应该适用于简单的日志记录。 -
page.on('console', msg => console.log('PAGE LOG:', msg.text));
从 Apify 示例文档中,我可以看到如果您在 handlePageFunction
中使用 console.log()
,它会直接记录到终端控制台。 Apify 实用程序 log
也是如此。例如:
handlePageFunction: async ({ request, page }) => {
console.log('This is logged to the terminal console');
log.info('So is this.')
...
}
然而,当我在 inside handlePageFunction this 中定义自己的函数时,我无法将任何内容记录到终端控制台。我希望 console.log 进入浏览器控制台,但在 Apify 中,浏览器 window 出现并迅速消失。 (也可以使用 apify-cli,它甚至根本不显示浏览器。)
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();
}
如何在嵌套的 javascript 函数中使用 Apify utils.log?
如果您在 page.evaluate
中使用定义的函数或在浏览器上下文中评估的任何其他函数,则 console.log
会在浏览器控制台中输出。如果您想在 Node.js 内显示浏览器日志,这应该适用于简单的日志记录。 -
page.on('console', msg => console.log('PAGE LOG:', msg.text));