【问题标题】:Where does console.log info showup for Google Cloud functionsGoogle Cloud 功能的 console.log 信息显示在哪里
【发布时间】:2017-06-14 23:40:15
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    Viewing Logs

    您可以使用以下任一方式查看 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;
        });
    }
    

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

    gcloud functions logs read
    

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

    gcloud functions logs read <FUNCTION_NAME>
    

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

    gcloud functions logs read <FUNCTION_NAME> --execution-id EXECUTION_ID
    

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

    gcloud functions logs read -h
    

    Writing Logs

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

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

    有关查看 Cloud Function 日志的更多信息,请访问here

    【讨论】:

      猜你喜欢
      • 2015-07-13
      • 1970-01-01
      • 2013-12-09
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 2014-05-12
      相关资源
      最近更新 更多