Skip to content

fix: add special logging fields section in the doc #728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .readme-partials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ body: |-

You may also want to see the [@google-cloud/error-reporting][@google-cloud/error-reporting] module which provides direct access to the Error Reporting API.

### Special Payload Fields in LogEntry

There are some fields that are considered special by Google cloud logging and will be extracted into the LogEntry structure. For example, `severity`, `message` and `labels` can be extracted to LogEntry if included in the bunyan log payload. These [special JSON fields](https://cloud.google.com/logging/docs/structured-logging#special-payload-fields) will be used to set the corresponding fields in the `LogEntry`. Please be aware of these special fields to avoid unexpected logging behavior.
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this only applies to logs written to stdout, when parsed through the Logging Agent. You should make sure that is captured in the message here

Copy link
Contributor Author

@cindy-peng cindy-peng Dec 5, 2023

Choose a reason for hiding this comment

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

Sorry Daniel I missed this comment earlier.

Initially I thought the same, then I looked into code and it looks like we are honoring these special fields and do a special processing from bunyan entry even without redirectToStdout: true. Here is the where the logEntry is formatted: https://github.com/googleapis/nodejs-logging-bunyan/blob/main/src/index.ts#L253.

An example code snippet I was using Bunyan logger without redirectToStdout:

const loggingBunyan = new LoggingBunyan();


 const logger = bunyan.createLogger({
  name: 'my-service',
  streams: [
    {stream: process.stdout, level: 'info'},
    loggingBunyan.stream('info'),
  ],
});


logger.info({ message: 'boom11',  labels: {first: "label-name", count: "2"} , "logging.googleapis.com/trace_sampled": true }, 'this is the actual message');

You can see the special fields (traceSampled and labels here) are being processed and actually promoted to the log entry level:
image

Maybe we can just leave the doc as this since special fields handling looks like the default behavior?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok interesting, I was thinking from the backend perspective, but I guess nodejs must be doing its own parsing as well. LGTM then


### LogEntry Labels

If the bunyan log record contains a label property where all the values are strings, we automatically promote that
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ Make sure to add logs to your [uncaught exception](https://nodejs.org/api/proces

You may also want to see the [@google-cloud/error-reporting][@google-cloud/error-reporting] module which provides direct access to the Error Reporting API.

### Special Payload Fields in LogEntry

There are some fields that are considered special by Google cloud logging and will be extracted into the LogEntry structure. For example, `severity`, `message` and `labels` can be extracted to LogEntry if included in the bunyan log payload. These [special JSON fields](https://cloud.google.com/logging/docs/structured-logging#special-payload-fields) will be used to set the corresponding fields in the `LogEntry`. Please be aware of these special fields to avoid unexpected logging behavior.

### LogEntry Labels

If the bunyan log record contains a label property where all the values are strings, we automatically promote that
Expand Down