适用于Google Cloud功能的console.log信息展示位置

时间:2017-06-14 23:40:16

标签: javascript google-cloud-platform google-cloud-endpoints

当我运行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);
  }
};

1 个答案:

答案 0 :(得分:2)

Viewing Logs

您可以使用以下任一方式查看云功能日志:

// 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

Writing Logs

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

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

有关查看云功能日志的更多信息,请here