首页 > 解决方案 > 在 Google Cloud Function 中写入控制台时,不会写入任何内容

问题描述

我在 Google Cloud Function 中有一系列工作要做。如果我在本地运行脚本,它会在云函数中生成输出console但不在云函数中。

// this is logged
console.error('An error');
for (var i=0; i<chunks.length; i++) {
  const work = chunks[i].map(c => createOrUpdateTable(c, timestamp));
  await Promise.all(work)
    // this is not logged
    .catch(e => console.error(e.message));
}

我试图把catch函数和很多其他的东西放在里面,但行为相同。如何让错误出现在日志中?

标签: exceptiongoogle-cloud-functions

解决方案


根据官方文档,您可以从 Cloud Function 向 Stackdriver Error Reporting 发出错误:

// These WILL be reported to Stackdriver Error Reporting
console.error(new Error('I failed you'));
console.error('I failed you', new Error('I failed you too'));
throw new Error('I failed you'); // Will cause a cold start if not caught

// These will NOT be reported to Stackdriver Error Reporting
console.info(new Error('I failed you')); // Logging an Error object at the info level
console.error('I failed you'); // Logging something other than an Error object
throw 1; // Throwing something other than an Error object
callback('I failed you');
res.status(500).send('I failed you');

报告错误


推荐阅读