Skip to content

Instantly share code, notes, and snippets.

@philsch
Last active October 2, 2022 14:18
Show Gist options
  • Save philsch/af58dce3c22259908aeb83b43556ca2f to your computer and use it in GitHub Desktop.
Save philsch/af58dce3c22259908aeb83b43556ca2f to your computer and use it in GitHub Desktop.
gcloud log example two
const functions = require('@google-cloud/functions-framework');
// Imports the Google Cloud client library
const {Logging} = require('@google-cloud/logging');
functions.http('test_two', async (req, res) => {
// Init Logging and set current context
const logging = new Logging();
await logging.setProjectId();
await logging.setDetectedResource();
// Create a LogSync transport, defaulting to `process.stdout`
const logger = logging.logSync('my-service');
// Prepare the current request as metadata
const metadata = {httpRequest: req};
// Writes some log entries
logger.info(logger.entry(metadata, 'info log'));
logger.warning(logger.entry(metadata, 'warn log'));
logger.error(logger.entry(metadata, 'error log'));
logger.error(logger.entry(metadata, new Error('js error'))); // doesn't work
logger.error(logger.entry(metadata, new Error('js error').stack));
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('hello world');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment