最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Where does console.log info showup for Google Cloud functions - Stack Overflow

programmeradmin1浏览0评论

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

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);
  }
};
Share Improve this question edited Feb 26, 2018 at 3:47 Jeff Puckett 41k19 gold badges124 silver badges173 bronze badges asked Jun 14, 2017 at 23:40 Mark PetersonMark Peterson 8993 gold badges10 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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;
    });
}
  • Using gcloud:

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

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() mands have the INFO log level.
  • console.error() mands have the ERROR log level.
  • Internal system messages have the DEBUG log level.

More info about viewing Cloud Function logs is available here.

发布评论

评论列表(0)

  1. 暂无评论