问题描述
当我运行Google Cloud功能时,如何查看console.log的打印内容?有云控制台吗?
How can I see the console.log prints when I'm running a Google Cloud function? Is there a cloud console?
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);
}
};
推荐答案
查看日志
您可以使用以下任一方式查看Cloud Function日志:
Viewing Logs
You can view the Cloud Function logs using either:
- The Stackdriver logging UI in the Cloud Console
- Using logging API
// 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 :
- Using gcloud:
gcloud functions logs read
要查看特定功能的日志,请提供功能名称为 一个论点:
To view the logs for a specific function, provide the function name as an argument:
gcloud functions logs read <FUNCTION_NAME>
您甚至可以查看特定执行的日志:
You can even view the logs for a specific execution:
gcloud functions logs read <FUNCTION_NAME> --execution-id EXECUTION_ID
有关所有日志查看选项,请查看日志帮助 阅读:
For the full range of log viewing options, view the help for logs read:
gcloud functions logs read -h
书写日志
您可以使用console.log()
或console.error()
.
Writing Logs
You can use console.log()
or console.error()
.
-
console.log()
命令具有INFO
日志级别. -
console.error()
命令具有ERROR
日志级别. - 内部系统消息的日志级别为
DEBUG
.
console.log()
commands have theINFO
log level.console.error()
commands have theERROR
log level.- Internal system messages have the
DEBUG
log level.
有关查看Cloud Function日志的更多信息,请此处.
More info about viewing Cloud Function logs is available here.
这篇关于Google Cloud功能的console.log信息显示在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!