Skip to content

Commit

Permalink
docs(samples): move awaits into async scope (#693)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
stephenplusplus and bcoe committed Feb 13, 2020
1 parent 0239b81 commit 8ccd960
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 72 deletions.
2 changes: 1 addition & 1 deletion samples/http-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ async function logHttpRequest(
await log.write(entry);
console.log(`Logged: ${text}`);
}
// [END logging_quickstart]
writeLog();
// [END logging_quickstart]
}

const args = process.argv.slice(2);
Expand Down
82 changes: 46 additions & 36 deletions samples/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ async 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.
await log.write([entry, secondEntry]);
console.log(`Wrote to ${logName}`);

async function writeLogEntry() {
// 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.
await log.write([entry, secondEntry]);
console.log(`Wrote to ${logName}`);
}
writeLogEntry();
// [END logging_write_log_entry]
}

Expand All @@ -80,11 +82,13 @@ async function writeLogEntryAdvanced(logName, options) {
// Prepare the entry
const entry = log.entry({resource: options.resource}, options.entry);

// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/log?method=write
await log.write(entry);
console.log(`Wrote to ${logName}`);

async function writeLogEntry() {
// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/log?method=write
await log.write(entry);
console.log(`Wrote to ${logName}`);
}
writeLogEntry();
// [END logging_write_log_entry_advanced]
}

Expand Down Expand Up @@ -122,16 +126,18 @@ async function listLogEntries(logName) {

const log = logging.log(logName);

// List the most recent entries for a given log
// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getEntries
const [entries] = await log.getEntries();
console.log('Logs:');
entries.forEach(entry => {
const metadata = entry.metadata;
console.log(`${metadata.timestamp}:`, metadata[metadata.payload]);
});

async function printEntryMetadata() {
// List the most recent entries for a given log
// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getEntries
const [entries] = await log.getEntries();
console.log('Logs:');
entries.forEach(entry => {
const metadata = entry.metadata;
console.log(`${metadata.timestamp}:`, metadata[metadata.payload]);
});
}
printEntryMetadata();
// [END logging_list_log_entries]
}

Expand Down Expand Up @@ -160,15 +166,17 @@ async function listLogEntriesAdvanced(filter, pageSize, orderBy) {
orderBy: orderBy,
};

// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getEntries
const [entries] = await logging.getEntries(options);
console.log('Logs:');
entries.forEach(entry => {
const metadata = entry.metadata;
console.log(`${metadata.timestamp}:`, metadata[metadata.payload]);
});

async function printEntryMetadata() {
// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getEntries
const [entries] = await logging.getEntries(options);
console.log('Logs:');
entries.forEach(entry => {
const metadata = entry.metadata;
console.log(`${metadata.timestamp}:`, metadata[metadata.payload]);
});
}
printEntryMetadata();
// [START logging_list_log_entries_advanced]
}

Expand All @@ -187,13 +195,15 @@ async function deleteLog(logName) {

const log = logging.log(logName);

// Deletes a logger and all its entries.
// 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
await log.delete();
console.log(`Deleted log: ${logName}`);

async function deleteLog() {
// Deletes a logger and all its entries.
// 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
await log.delete();
console.log(`Deleted log: ${logName}`);
}
deleteLog();
// [END logging_delete_log]
}

Expand Down
9 changes: 6 additions & 3 deletions samples/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ async function quickstart(
// Prepares a log entry
const entry = log.entry(metadata, text);

// Writes the log entry
await log.write(entry);
console.log(`Logged: ${text}`);
async function writeLog() {
// Writes the log entry
await log.write(entry);
console.log(`Logged: ${text}`);
}
writeLog();
}
// [END logging_quickstart]

Expand Down
74 changes: 42 additions & 32 deletions samples/sinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ async function createSink(sinkName, bucketName, filter) {
filter: filter,
};

// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=create
await sink.create(config);
console.log(`Created sink ${sinkName} to ${bucketName}`);

async function createSink() {
// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=create
await sink.create(config);
console.log(`Created sink ${sinkName} to ${bucketName}`);
}
createSink();
// [END logging_create_sink]
}

Expand All @@ -73,13 +75,15 @@ async function getSinkMetadata(sinkName) {

const sink = logging.sink(sinkName);

// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=getMetadata
const [metadata] = await sink.getMetadata();
console.log(`Name: ${metadata.name}`);
console.log(`Destination: ${metadata.destination}`);
console.log(`Filter: ${metadata.filter}`);

async function printSinkMetadata() {
// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=getMetadata
const [metadata] = await sink.getMetadata();
console.log(`Name: ${metadata.name}`);
console.log(`Destination: ${metadata.destination}`);
console.log(`Filter: ${metadata.filter}`);
}
printSinkMetadata();
// [END logging_get_sink]
}

Expand All @@ -91,16 +95,18 @@ async function listSinks() {
// Creates a client
const logging = new Logging();

// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getSinks
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}`);
});

async function printSinkMetadata() {
// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getSinks
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}`);
});
}
printSinkMetadata();
// [END logging_list_sinks]
}

Expand Down Expand Up @@ -131,11 +137,13 @@ async function updateSink(sinkName, filter) {
filter: filter,
};

// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=setMetadata
const [metadata] = await sink.setMetadata(metadataInfo);
console.log(`Sink ${sinkName} updated.`, metadata);

async function updateSink() {
// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=setMetadata
const [metadata] = await sink.setMetadata(metadataInfo);
console.log(`Sink ${sinkName} updated.`, metadata);
}
updateSink();
// [END logging_update_sink]
}

Expand All @@ -154,11 +162,13 @@ async function deleteSink(sinkName) {

const sink = logging.sink(sinkName);

// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=delete
await sink.delete();
console.log(`Sink ${sinkName} deleted.`);

async function deleteSink() {
// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=delete
await sink.delete();
console.log(`Sink ${sinkName} deleted.`);
}
deleteSink();
// [END logging_delete_sink]
}

Expand Down

0 comments on commit 8ccd960

Please sign in to comment.