Description
Is your feature request related to a problem? Please describe.
When attaching the CloudLoggingHandler to a standard python logger, it is not possible to perform structured logging through the extra
keyword argument for each logging call. Please refer to the standard logging python library for more documentation about the extra
parameter. Take for instance the following snippet of code, written in Python 3.9 and using google-cloud-logging==2.6.0
as a dependency:
import logging
import google.cloud.logging
from google.cloud.logging_v2.handlers import CloudLoggingHandler
client = google.cloud.logging.Client()
handler = CloudLoggingHandler(client)
cloud_logger = logging.getLogger('cloudLogger')
cloud_logger.setLevel(logging.INFO)
cloud_logger.addHandler(handler)
cloud_logger.error('bad news', extra={"error_code": 300}) # Note that we include an extra dictionary
The extra
dictionary is completely lost and does not appear in the received log from the Cloud Logging UI (see screenshot).
Describe the solution you'd like
All properties of a LogRecord that are considered as extra
s appear in the jsonPayload of the related entry in Cloud Logging. This can be implemented for instance in the body of the emit
method of the CloudLoggingHandler (reference).
Describe alternatives you've considered
The only alternative solution we found is to use the log_struct
method of the google.cloud.logging.logger.Logger()
object directly. This is not a valid solution for us as we make heavy use of the standard logging library shipped with python: using a separate logger would introduce too much overhead for our existing and future projects, while killing the purpose of the centralized logging infrastructure that the python standard logging library provides.