diff --git a/samples/logs.js b/samples/logs.js index c7bb204d..20f5bf52 100644 --- a/samples/logs.js +++ b/samples/logs.js @@ -15,7 +15,7 @@ 'use strict'; -function writeLogEntry(logName) { +async function writeLogEntry(logName) { // [START logging_write_log_entry] // Imports the Google Cloud client library const {Logging} = require('@google-cloud/logging'); @@ -53,18 +53,13 @@ function writeLogEntry(logName) { // Save the two log entries. You can write entries one at a time, but it is // best to write multiple entires together in a batch. - log - .write([entry, secondEntry]) - .then(() => { - console.log(`Wrote to ${logName}`); - }) - .catch(err => { - console.error('ERROR:', err); - }); + await log.write([entry, secondEntry]); + console.log(`Wrote to ${logName}`); + // [END logging_write_log_entry] } -function writeLogEntryAdvanced(logName, options) { +async function writeLogEntryAdvanced(logName, options) { // [START logging_write_log_entry_advanced] // Imports the Google Cloud client library const {Logging} = require('@google-cloud/logging'); @@ -88,18 +83,13 @@ function writeLogEntryAdvanced(logName, options) { // See // https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/log?method=write - log - .write(entry) - .then(() => { - console.log(`Wrote to ${logName}`); - }) - .catch(err => { - console.error('ERROR:', err); - }); + await log.write(entry); + console.log(`Wrote to ${logName}`); + // [END logging_write_log_entry_advanced] } -function listLogEntries(logName) { +async function listLogEntries(logName) { // [START logging_list_log_entries] // Imports the Google Cloud client library const {Logging} = require('@google-cloud/logging'); @@ -117,24 +107,17 @@ function listLogEntries(logName) { // List the most recent entries for a given log // See // https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getEntries - log - .getEntries() - .then(results => { - const entries = results[0]; - - console.log('Logs:'); - entries.forEach(entry => { - const metadata = entry.metadata; - console.log(`${metadata.timestamp}:`, metadata[metadata.payload]); - }); - }) - .catch(err => { - console.error('ERROR:', err); - }); + const [entries] = await log.getEntries(); + console.log('Logs:'); + entries.forEach(entry => { + const metadata = entry.metadata; + console.log(`${metadata.timestamp}:`, metadata[metadata.payload]); + }); + // [END logging_list_log_entries] } -function listLogEntriesAdvanced(filter, pageSize, orderBy) { +async function listLogEntriesAdvanced(filter, pageSize, orderBy) { // [START logging_list_log_entries_advanced] // Imports the Google Cloud client library const {Logging} = require('@google-cloud/logging'); @@ -161,24 +144,17 @@ function listLogEntriesAdvanced(filter, pageSize, orderBy) { // See // https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getEntries - logging - .getEntries(options) - .then(results => { - const entries = results[0]; - - console.log('Logs:'); - entries.forEach(entry => { - const metadata = entry.metadata; - console.log(`${metadata.timestamp}:`, metadata[metadata.payload]); - }); - }) - .catch(err => { - console.error('ERROR:', err); - }); + const [entries] = await logging.getEntries(options); + console.log('Logs:'); + entries.forEach(entry => { + const metadata = entry.metadata; + console.log(`${metadata.timestamp}:`, metadata[metadata.payload]); + }); + // [START logging_list_log_entries_advanced] } -function deleteLog(logName) { +async function deleteLog(logName) { // [START logging_delete_log] // Imports the Google Cloud client library const {Logging} = require('@google-cloud/logging'); @@ -197,102 +173,101 @@ function deleteLog(logName) { // Note that a deletion can take several minutes to take effect. // See // https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/log?method=delete - log - .delete() - .then(() => { - console.log(`Deleted log: ${logName}`); - }) - .catch(err => { - console.error('ERROR:', err); - }); + await log.delete(); + console.log(`Deleted log: ${logName}`); + // [END logging_delete_log] } -require(`yargs`) - .demand(1) - .command( - 'list', - 'Lists log entries, optionally filtering, limiting, and sorting results.', - { - filter: { - alias: 'f', - type: 'string', - requiresArg: true, - description: 'Only log entries matching the filter are written.', - }, - limit: { - alias: 'l', - type: 'number', - requiresArg: true, - description: 'Maximum number of results to return.', - }, - sort: { - alias: 's', - type: 'string', - requiresArg: true, - description: 'Sort results.', +async function main() { + require(`yargs`) + .demand(1) + .command( + 'list', + 'Lists log entries, optionally filtering, limiting, and sorting results.', + { + filter: { + alias: 'f', + type: 'string', + requiresArg: true, + description: 'Only log entries matching the filter are written.', + }, + limit: { + alias: 'l', + type: 'number', + requiresArg: true, + description: 'Maximum number of results to return.', + }, + sort: { + alias: 's', + type: 'string', + requiresArg: true, + description: 'Sort results.', + }, }, - }, - opts => { - listLogEntriesAdvanced(opts.filter, opts.limit, opts.sort); - } - ) - .command('list-simple ', 'Lists log entries.', {}, opts => - listLogEntries(opts.logName) - ) - .command( - 'write ', - 'Writes a log entry to the specified log.', - {}, - opts => { - try { - opts.resource = JSON.parse(opts.resource); - } catch (err) { - console.error('"resource" must be a valid JSON string!'); - return; + opts => { + listLogEntriesAdvanced(opts.filter, opts.limit, opts.sort); } - - try { - opts.entry = JSON.parse(opts.entry); - } catch (err) { - console.error('"entry" must be a valid JSON string!'); - return; + ) + .command('list-simple ', 'Lists log entries.', {}, opts => + listLogEntries(opts.logName) + ) + .command( + 'write ', + 'Writes a log entry to the specified log.', + {}, + opts => { + try { + opts.resource = JSON.parse(opts.resource); + } catch (err) { + console.error('"resource" must be a valid JSON string!'); + return; + } + + try { + opts.entry = JSON.parse(opts.entry); + } catch (err) { + console.error('"entry" must be a valid JSON string!'); + return; + } + + writeLogEntryAdvanced(opts.logName, opts); } + ) + .command( + 'write-simple ', + 'Writes a basic log entry to the specified log.', + {}, + opts => { + writeLogEntry(opts.logName); + } + ) + .command('delete ', 'Deletes the specified Log.', {}, opts => { + deleteLog(opts.logName); + }) + .example('node $0 list', 'List all log entries.') + .example( + 'node $0 list -f "severity=ERROR" -s "timestamp" -l 2', + 'List up to 2 error entries, sorted by timestamp ascending.' + ) + .example( + `node $0 list -f 'logName="my-log"' -l 2`, + 'List up to 2 log entries from the "my-log" log.' + ) + .example( + 'node $0 write my-log \'{"type":"gae_app","labels":{"module_id":"default"}}\' \'"Hello World!"\'', + 'Write a string log entry.' + ) + .example( + 'node $0 write my-log \'{"type":"global"}\' \'{"message":"Hello World!"}\'', + 'Write a JSON log entry.' + ) + .example('node $0 delete my-log', 'Delete "my-log".') + .wrap(120) + .recommendCommands() + .epilogue(`For more information, see https://cloud.google.com/logging/docs`) + .help() + .strict().argv; +} - writeLogEntryAdvanced(opts.logName, opts); - } - ) - .command( - 'write-simple ', - 'Writes a basic log entry to the specified log.', - {}, - opts => { - writeLogEntry(opts.logName); - } - ) - .command('delete ', 'Deletes the specified Log.', {}, opts => { - deleteLog(opts.logName); - }) - .example('node $0 list', 'List all log entries.') - .example( - 'node $0 list -f "severity=ERROR" -s "timestamp" -l 2', - 'List up to 2 error entries, sorted by timestamp ascending.' - ) - .example( - `node $0 list -f 'logName="my-log"' -l 2`, - 'List up to 2 log entries from the "my-log" log.' - ) - .example( - 'node $0 write my-log \'{"type":"gae_app","labels":{"module_id":"default"}}\' \'"Hello World!"\'', - 'Write a string log entry.' - ) - .example( - 'node $0 write my-log \'{"type":"global"}\' \'{"message":"Hello World!"}\'', - 'Write a JSON log entry.' - ) - .example('node $0 delete my-log', 'Delete "my-log".') - .wrap(120) - .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/logging/docs`) - .help() - .strict().argv; +main().catch(console.error); diff --git a/samples/quickstart.js b/samples/quickstart.js index 4ed3ca1c..acf7e39f 100644 --- a/samples/quickstart.js +++ b/samples/quickstart.js @@ -14,40 +14,38 @@ */ 'use strict'; +async function main() { + // [START logging_quickstart] + // Imports the Google Cloud client library + const {Logging} = require('@google-cloud/logging'); -// [START logging_quickstart] -// Imports the Google Cloud client library -const {Logging} = require('@google-cloud/logging'); - -// Your Google Cloud Platform project ID -const projectId = 'YOUR_PROJECT_ID'; - -// Creates a client -const logging = new Logging({ - projectId: projectId, -}); - -// The name of the log to write to -const logName = 'my-log'; -// Selects the log to write to -const log = logging.log(logName); - -// The data to write to the log -const text = 'Hello, world!'; -// The metadata associated with the entry -const metadata = { - resource: {type: 'global'}, -}; -// Prepares a log entry -const entry = log.entry(metadata, text); - -// Writes the log entry -log - .write(entry) - .then(() => { - console.log(`Logged: ${text}`); - }) - .catch(err => { - console.error('ERROR:', err); + // Your Google Cloud Platform project ID + const projectId = 'YOUR_PROJECT_ID'; + + // Creates a client + const logging = new Logging({ + projectId: projectId, }); -// [END logging_quickstart] + + // The name of the log to write to + const logName = 'my-log'; + // Selects the log to write to + const log = logging.log(logName); + + // The data to write to the log + const text = 'Hello, world!'; + // The metadata associated with the entry + const metadata = { + resource: {type: 'global'}, + }; + // Prepares a log entry + const entry = log.entry(metadata, text); + + // Writes the log entry + await log.write(entry); + console.log(`Logged: ${text}`); + + // [END logging_quickstart] +} + +main().catch(console.error); diff --git a/samples/sinks.js b/samples/sinks.js index e787f125..a79f545d 100644 --- a/samples/sinks.js +++ b/samples/sinks.js @@ -15,7 +15,7 @@ 'use strict'; -function createSink(sinkName, bucketName, filter) { +async function createSink(sinkName, bucketName, filter) { // [START logging_create_sink] // Imports the Google Cloud client libraries const {Logging} = require('@google-cloud/logging'); @@ -53,18 +53,13 @@ function createSink(sinkName, bucketName, filter) { // See // https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=create - sink - .create(config) - .then(() => { - console.log(`Created sink ${sinkName} to ${bucketName}`); - }) - .catch(err => { - console.error('ERROR:', err); - }); + await sink.create(config); + console.log(`Created sink ${sinkName} to ${bucketName}`); + // [END logging_create_sink] } -function getSinkMetadata(sinkName) { +async function getSinkMetadata(sinkName) { // [START logging_get_sink] // Imports the Google Cloud client library const {Logging} = require('@google-cloud/logging'); @@ -81,22 +76,15 @@ function getSinkMetadata(sinkName) { // See // https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=getMetadata - sink - .getMetadata() - .then(results => { - const metadata = results[0]; - - console.log(`Name: ${metadata.name}`); - console.log(`Destination: ${metadata.destination}`); - console.log(`Filter: ${metadata.filter}`); - }) - .catch(err => { - console.error('ERROR:', err); - }); + const [metadata] = await sink.getMetadata(); + console.log(`Name: ${metadata.name}`); + console.log(`Destination: ${metadata.destination}`); + console.log(`Filter: ${metadata.filter}`); + // [END logging_get_sink] } -function listSinks() { +async function listSinks() { // [START logging_list_sinks] // Imports the Google Cloud client library const {Logging} = require('@google-cloud/logging'); @@ -106,25 +94,18 @@ function listSinks() { // See // https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getSinks - logging - .getSinks() - .then(results => { - const sinks = results[0]; - - console.log('Sinks:'); - sinks.forEach(sink => { - console.log(sink.name); - console.log(` Destination: ${sink.metadata.destination}`); - console.log(` Filter: ${sink.metadata.filter}`); - }); - }) - .catch(err => { - console.error('ERROR:', err); - }); + const [sinks] = await logging.getSinks(); + console.log('Sinks:'); + sinks.forEach(sink => { + console.log(sink.name); + console.log(` Destination: ${sink.metadata.destination}`); + console.log(` Filter: ${sink.metadata.filter}`); + }); + // [END logging_list_sinks] } -function updateSink(sinkName, filter) { +async function updateSink(sinkName, filter) { // [START logging_update_sink] // Imports the Google Cloud client library const {Logging} = require('@google-cloud/logging'); @@ -147,25 +128,19 @@ function updateSink(sinkName, filter) { * See https://cloud.google.com/logging/docs/view/advanced_filters for more * filter information. */ - const metadata = { + const metadataInfo = { filter: filter, }; // See // https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=setMetadata - sink - .setMetadata(metadata) - .then(results => { - const metadata = results[0]; - console.log(`Sink ${sinkName} updated.`, metadata); - }) - .catch(err => { - console.error('ERROR:', err); - }); + const [metadata] = await sink.setMetadata(metadataInfo); + console.log(`Sink ${sinkName} updated.`, metadata); + // [END logging_update_sink] } -function deleteSink(sinkName) { +async function deleteSink(sinkName) { // [START logging_delete_sink] // Imports the Google Cloud client library const {Logging} = require('@google-cloud/logging'); @@ -182,66 +157,65 @@ function deleteSink(sinkName) { // See // https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=delete - sink - .delete() - .then(() => { - console.log(`Sink ${sinkName} deleted.`); - }) - .catch(err => { - console.error('ERROR:', err); - }); + await sink.delete(); + console.log(`Sink ${sinkName} deleted.`); + // [END logging_delete_sink] } -require(`yargs`) - .demand(1) - .command( - 'create [filter]', - 'Creates a new sink with the given name to the specified bucket with an optional filter.', - {}, - opts => { - createSink(opts.sinkName, opts.bucketName, opts.filter); - } - ) - .command( - 'get ', - 'Gets the metadata for the specified sink.', - {}, - opts => { - getSinkMetadata(opts.sinkName); - } - ) - .command('list', 'Lists all sinks.', {}, listSinks) - .command( - 'update ', - 'Updates the filter for the specified sink.', - {}, - opts => { - updateSink(opts.sinkName, opts.filter); - } - ) - .command('delete ', 'Deletes the specified sink.', {}, opts => { - deleteSink(opts.sinkName); - }) - .example( - 'node $0 create export-errors app-error-logs', - 'Create a new sink named "export-errors" that exports logs to a bucket named "app-error-logs".' - ) - .example( - 'node $0 get export-errors', - 'Get the metadata for a sink name "export-errors".' - ) - .example('node $0 list', 'List all sinks.') - .example( - 'node $0 update export-errors "severity >= WARNING"', - 'Update the filter for a sink named "export-errors".' - ) - .example( - 'node $0 delete export-errors', - 'Delete a sink named "export-errors".' - ) - .wrap(120) - .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/logging/docs`) - .help() - .strict().argv; +async function main() { + require(`yargs`) + .demand(1) + .command( + 'create [filter]', + 'Creates a new sink with the given name to the specified bucket with an optional filter.', + {}, + opts => { + createSink(opts.sinkName, opts.bucketName, opts.filter); + } + ) + .command( + 'get ', + 'Gets the metadata for the specified sink.', + {}, + opts => { + getSinkMetadata(opts.sinkName); + } + ) + .command('list', 'Lists all sinks.', {}, listSinks) + .command( + 'update ', + 'Updates the filter for the specified sink.', + {}, + opts => { + updateSink(opts.sinkName, opts.filter); + } + ) + .command('delete ', 'Deletes the specified sink.', {}, opts => { + deleteSink(opts.sinkName); + }) + .example( + 'node $0 create export-errors app-error-logs', + 'Create a new sink named "export-errors" that exports logs to a bucket named "app-error-logs".' + ) + .example( + 'node $0 get export-errors', + 'Get the metadata for a sink name "export-errors".' + ) + .example('node $0 list', 'List all sinks.') + .example( + 'node $0 update export-errors "severity >= WARNING"', + 'Update the filter for a sink named "export-errors".' + ) + .example( + 'node $0 delete export-errors', + 'Delete a sink named "export-errors".' + ) + .wrap(120) + .recommendCommands() + .epilogue(`For more information, see https://cloud.google.com/logging/docs`) + .help() + .strict().argv; +} + +main().catch(console.error);