|
| 1 | +// [START sd_logging_import] |
| 2 | +const { Logging } = require('@google-cloud/logging'); |
| 3 | +// [END sd_logging_import] |
| 4 | +const functions = require('firebase-functions'); |
| 5 | + |
| 6 | +// [START sd_example_function] |
| 7 | +exports.helloError = functions.https.onRequest((request, response) => { |
| 8 | + console.log('I am a log entry!'); |
| 9 | + response.send('Hello World...'); |
| 10 | +}); |
| 11 | +// [END sd_example_function] |
| 12 | + |
| 13 | +// [START sd_logging_setup] |
| 14 | +// Instantiate the StackDriver Logging SDK. The project ID will |
| 15 | +// be automatically inferred from the Cloud Functions environment. |
| 16 | +const logging = new Logging(); |
| 17 | +const log = logging.log('my-custom-log-name'); |
| 18 | + |
| 19 | +// This metadata is attached to each log entry. This specifies a fake |
| 20 | +// Cloud Function called 'Custom Metrics' in order to make your custom |
| 21 | +// log entries appear in the Cloud Functions logs viewer. |
| 22 | +const METADATA = { |
| 23 | + resource: { |
| 24 | + type: 'cloud_function', |
| 25 | + labels: { |
| 26 | + function_name: 'CustomMetrics', |
| 27 | + region: 'us-central1' |
| 28 | + } |
| 29 | + } |
| 30 | +}; |
| 31 | +// [END sd_logging_setup] |
| 32 | + |
| 33 | +function writeLog() { |
| 34 | + // [START sd_write_log] |
| 35 | + // Data to write to the log. This can be a JSON object with any properties |
| 36 | + // of the event you want to record. |
| 37 | + const data = { |
| 38 | + event: 'my-event', |
| 39 | + value: 'foo-bar-baz', |
| 40 | + |
| 41 | + // Optional 'message' property will show up in the Firebase |
| 42 | + // console and other human-readable logging surfaces |
| 43 | + message: 'my-event: foo-bar-baz' |
| 44 | + }; |
| 45 | + |
| 46 | + // Write to the log. The log.write() call returns a Promise if you want to |
| 47 | + // make sure that the log was written successfully. |
| 48 | + const entry = log.entry(METADATA, data); |
| 49 | + log.write(entry); |
| 50 | + // [END sd_write_log] |
| 51 | +} |
0 commit comments