Skip to content

CloudLoggingHandler disregards contextual information #2932

Closed
@Gennady-Andreyev

Description

@Gennady-Andreyev

Context

Python logging provides useful facilities to inject arbitrary contextual information into log records. I.e. the core logger's methods like debug, info, etc. can be provided with an optional parameter called extra. Reference to the doc. There are also more sophisticated techniques. It sounds extremely useful given stackdriver is quite friendly to structured data.

Problem

Unfortunately, google.cloud.logging.handlers.CloudLoggingHandler (or, strictly saying, underlying transports) disregards whatever is passed in extra parameter and sends further only message. Code reference 1 and 2. This is quite sad since log_struct does let us pass arbitrary structured entries.

Proposed solution

Although a bit hacky, it would probably be the easiest to pass the whole __dict__ of LogRecord object to log_struct here. I mean smth. like this:

self.batch.log_struct(record.__dict__, severity=record.levelname)

A property called message will automatically be there as well (LogRecord attributes).

Ideally, such useful log_struct's parameters as labels, insert_id, etc. should be recognized as well.

self.batch.log_struct(
    record.__dict__, 
    severity=record.levelname,
    labels=getattr(record, 'labels', None),
    insert_id=getattr(record, 'insert_id', None)
)

Metadata

Metadata

Labels

api: loggingIssues related to the Cloud Logging API.priority: p2Moderately-important priority. Fix may not be included in next release.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions