console.log信息在哪里显示Google Cloud功能

Mar*_*son 5 javascript google-cloud-endpoints google-cloud-platform

运行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);
  }
};
Run Code Online (Sandbox Code Playgroud)

Tux*_*ude 9

查看日志

您可以使用以下任一方法查看 Cloud Function 日志:

// 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;
    });
}
Run Code Online (Sandbox Code Playgroud)

要使用 gcloud 工具查看日志,请使用 logs read 命令:

gcloud functions logs read
Run Code Online (Sandbox Code Playgroud)

要查看特定函数的日志,请提供函数名称作为参数:

gcloud functions logs read <FUNCTION_NAME>
Run Code Online (Sandbox Code Playgroud)

您甚至可以查看特定执行的日志:

gcloud functions logs read <FUNCTION_NAME> --execution-id EXECUTION_ID
Run Code Online (Sandbox Code Playgroud)

有关完整的日志查看选项,请查看读取日志的帮助:

gcloud functions logs read -h
Run Code Online (Sandbox Code Playgroud)

写日志

您可以使用console.log()console.error()

  • console.log()命令具有INFO日志级别。
  • console.error()命令具有ERROR日志级别。
  • 内部系统消息具有DEBUG日志级别。

此处提供有关查看 Cloud Function 日志的更多信息。