当我运行Google Cloud功能时,如何查看console.log?有云控制台吗?
exports.helloWorld = function helloWorld(req, res) {
// Example input: {"message": "Hello!"}
if (req.body.message === undefined) {
// This is an error case, as "message" is required.
res.status(400).send('No message defined!');
} else {
// Everything is okay.
console.log(req.body.message);
res.status(200).send('Success: ' + req.body.message);
}
};
答案 0 :(得分:2)
您可以使用以下任一方式查看云功能日志:
// By default, the client will authenticate using the service account file // specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use // the project specified by the GCLOUD_PROJECT environment variable. See // https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication const Logging = require('@google-cloud/logging'); function getLogEntries () { // Instantiates a client const logging = Logging(); const options = { pageSize: 10, filter: 'resource.type="cloud_function"' }; // Retrieve the latest Cloud Function log entries // See https://googlecloudplatform.github.io/gcloud-node/#/docs/logging return logging.getEntries(options) .then(([entries]) => { console.log('Entries:'); entries.forEach((entry) => console.log(entry)); return entries; }); }
要使用gcloud工具查看日志,请使用logs read命令:
gcloud beta functions logs read
要查看特定功能的日志,请提供函数名称 一个论点:
gcloud beta functions logs read <FUNCTION_NAME>
您甚至可以查看特定执行的日志:
gcloud beta functions logs read <FUNCTION_NAME> --execution-id EXECUTION_ID
对于所有日志查看选项,请查看日志帮助 读:
gcloud beta functions logs read -h
您可以使用console.log()
或console.error()
。
console.log()
命令具有INFO
日志级别。console.error()
命令具有ERROR
日志级别。DEBUG
。有关查看云功能日志的更多信息,请here。