Skip to content
This repository was archived by the owner on Mar 11, 2026. It is now read-only.

docs: improve write structured logEntry example#986

Merged
freelerobot merged 4 commits intomasterfrom
structuredLogSample
Feb 4, 2021
Merged

docs: improve write structured logEntry example#986
freelerobot merged 4 commits intomasterfrom
structuredLogSample

Conversation

@freelerobot
Copy link
Copy Markdown
Contributor

Fixes #973 🦕

For internal reference, here's the canonical sample

@freelerobot freelerobot requested review from a team February 4, 2021 00:40
@freelerobot freelerobot self-assigned this Feb 4, 2021
@product-auto-label product-auto-label Bot added the api: logging Issues related to the googleapis/nodejs-logging API. label Feb 4, 2021
@google-cla google-cla Bot added the cla: yes This human has signed the Contributor License Agreement. label Feb 4, 2021
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 4, 2021

Codecov Report

Merging #986 (0cb76ea) into master (b0ba942) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #986   +/-   ##
=======================================
  Coverage   98.39%   98.39%           
=======================================
  Files          18       18           
  Lines       12985    12985           
  Branches      399      399           
=======================================
  Hits        12776    12776           
  Misses        207      207           
  Partials        2        2           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b0ba942...fb542b1. Read the comment docs.

Comment thread samples/logs.js Outdated
Comment thread samples/logs.js Outdated
Comment thread samples/logs.js Outdated
const json_Entry = log.entry(metadata, message);

// Synchronously write the log entry
await log.write(text_entry);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is written right now, the user will see await calls without a wrapping async function. This will lead to copy/paste code that doesn't work sadly. This needs an outer main function that holds an async function that does an await. I know this is kind of terrible. For a good example:
https://github.com/googleapis/nodejs-bigquery/blob/master/samples/addColumnLoadAppend.js

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes we want it to be copy&paste runnable.

@JustinBeckwith should I refactor all node tests to have an outer main (might take some time).
Or is it ok if I wrap the await call inside an async function like the current state of the example?

...
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();
``

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't have to refactor the tests - just the samples themselves. What you have written there is perfect. That whole thing though ends up in it's own main function usually.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 updated . thanks for the feedback!

@freelerobot freelerobot added the priority: p2 Moderately-important priority. Fix may not be included in next release. label Feb 4, 2021
@freelerobot freelerobot merged commit 543533a into master Feb 4, 2021
@freelerobot freelerobot deleted the structuredLogSample branch February 4, 2021 20:40
Comment thread samples/logs.js

console.log(`Wrote to ${logName}`);
}
writeLogEntry();
Copy link
Copy Markdown
Contributor

@daniel-sanche daniel-sanche Feb 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to be inside the region tag?

Edit: nvm. I realized there's an inner and outer writeLogEntry. I'd probably suggest removing inner the function to keep it simpler, but I see it was like this before you touched it

Comment thread samples/logs.js
*/
// const logName = 'Name of the log to write to, e.g. my-log';

// const logName = 'my-log';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't want to have this as a comment, this should also work:

async function writeLogEntry(inputName) {
...
var logName = 'my-log';
[END logging_write_log_entry]
logName = inputName;
[START logging_write_log_entry]
...

The two halves of the region tag will be concatenated together, and the snippet will be both readable and testable

Comment thread samples/logs.js
favorite_color: 'Blue',
}
);
const json_Entry = log.entry(metadata, message);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More comments might be useful here. I wonder if users will be confused about the multiple logging payloads

Comment thread samples/logs.js
// Asynchronously let the logging library dispatch logs
log.write(text_entry);

console.log(`Wrote to ${logName}`);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a bit weird to be doing log and console.log. Is there any reason?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api: logging Issues related to the googleapis/nodejs-logging API. cla: yes This human has signed the Contributor License Agreement. priority: p2 Moderately-important priority. Fix may not be included in next release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve the Write logEntry sample

3 participants