Google Cloud 函数的 console.log 信息在哪里显示 [英] Where does console.log info showup for Google Cloud functions

查看:28
本文介绍了Google Cloud 函数的 console.log 信息在哪里显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行 Google Cloud 函数时,如何查看 console.log 打印?有云控制台吗?

exports.helloWorld = function helloWorld(req, res) {//示例输入:{"message": "Hello!"}如果(req.body.message === 未定义){//这是一个错误案例,因为消息"是必需的.res.status(400).send('没有定义消息!');} 别的 {//一切正常.控制台日志(req.body.message);res.status(200).send('成功:' + req.body.message);}};

解决方案

查看日志

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

<块引用>

//默认情况下,客户端将使用服务帐户文件进行身份验证//由 GOOGLE_APPLICATION_CREDENTIALS 环境变量指定并使用//GCLOUD_PROJECT 环境变量指定的项目.看//https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authenticationconst Logging = require('@google-cloud/logging');函数 getLogEntries () {//实例化一个客户端常量日志 = 日志();常量选项 = {页面大小:10,过滤器:'resource.type="cloud_function"'};//检索最新的 Cloud Function 日志条目//参见 https://googlecloudplatform.github.io/gcloud-node/#/docs/logging返回 logging.getEntries(options).then(([entries]) => {console.log('条目:');entry.forEach((entry) => console.log(entry));返回条目;});}

<块引用>

要使用 gcloud 工具查看日志,请使用日志读取命令:

gcloud 函数日志读取

要查看特定函数的日志,请提供函数名称作为一个论点:

gcloud 函数日志读取 

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

gcloud 函数日志读取 --execution-id EXECUTION_ID

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

gcloud 函数日志读取 -h

写日志

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

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

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

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);
  }
};

解决方案

Viewing Logs

You can view the Cloud Function logs using either:

// 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;
    });
}

To view logs with the gcloud tool, use the logs read command:

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

Writing Logs

You can use console.log() or console.error().

  • console.log() commands have the INFO log level.
  • console.error() commands have the ERROR log level.
  • Internal system messages have the DEBUG log level.

More info about viewing Cloud Function logs is available here.

这篇关于Google Cloud 函数的 console.log 信息在哪里显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆