-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
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)
)