Last active
October 2, 2022 09:47
-
-
Save philsch/0df6868bacb98f1b8ba982e3e585e167 to your computer and use it in GitHub Desktop.
gcloud log example one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('@google-cloud/functions-framework'); | |
const bunyan = require('bunyan'); | |
// Imports the Google Cloud client library for Bunyan | |
const {LoggingBunyan} = require('@google-cloud/logging-bunyan'); | |
// Creates a Bunyan Cloud Logging client | |
const loggingBunyan = new LoggingBunyan(); | |
// Create a Bunyan logger that streams to Cloud Logging | |
// Logs will be written to: "projects/YOUR_PROJECT_ID/logs/bunyan_log" | |
const logger = bunyan.createLogger({ | |
// The JSON payload of the log as it appears in Cloud Logging | |
// will contain "name": "my-service" | |
name: 'my-service', | |
streams: [ | |
// Log to the console at 'info' and above | |
// TODO: remove this when running in cloud to avoid double logging | |
{stream: process.stdout, level: 'info'}, | |
// And log to Cloud Logging, logging at 'info' and above | |
loggingBunyan.stream('info'), | |
], | |
}); | |
functions.http('test_one', (req, res) => { | |
// Writes some log entries | |
logger.info('info log'); | |
logger.warn('warn log'); | |
logger.error('error log'); | |
logger.error(new Error('js error')); | |
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