From 6068f065e26657ccd0257245f3fb28d11f1cba08 Mon Sep 17 00:00:00 2001 From: YouCyuan Jhang Date: Tue, 11 Sep 2018 12:09:23 -0700 Subject: [PATCH 1/2] Update Stackdriver Log Transport to support extra fields --- .../handlers/transports/background_thread.py | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/logging/google/cloud/logging/handlers/transports/background_thread.py b/logging/google/cloud/logging/handlers/transports/background_thread.py index 852e32dd42bb..262ee01354ce 100644 --- a/logging/google/cloud/logging/handlers/transports/background_thread.py +++ b/logging/google/cloud/logging/handlers/transports/background_thread.py @@ -37,6 +37,15 @@ _WORKER_TERMINATOR = object() _LOGGER = logging.getLogger(__name__) +# LogRecord attributes should not be handled separately +# http://docs.python.org/library/logging.html#logrecord-attributes +LOGRECORD_ATTRS = ( + 'args', 'asctime', 'created', 'exc_info', 'exc_text', 'filename', + 'funcName', 'levelname', 'levelno', 'lineno', 'module', + 'msecs', 'message', 'msg', 'name', 'pathname', 'process', + 'processName', 'relativeCreated', 'stack_info', 'thread', 'threadName' +) + def _get_many(queue_, max_items=None, max_latency=0): """Get multiple items from a Queue. @@ -254,10 +263,7 @@ def enqueue(self, record, message, resource=None, labels=None, Specify the trace parameter if span_id is set. """ self._queue.put_nowait({ - 'info': { - 'message': message, - 'python_logger': record.name, - }, + 'info': info, 'severity': record.levelname, 'resource': resource, 'labels': labels, @@ -269,6 +275,28 @@ def flush(self): """Submit any pending log records.""" self._queue.join() + def process_info(self, record, message): + """Process info to dict prior to transport. + + Extra fields in record will be added as well. + + :type record: :class:`logging.LogRecord` + :param record: Python log record that the handler was called with. + + :type message: str + :param message: The message from the ``LogRecord`` after being + formatted by the associated log formatters. + """ + info = { + 'message': message, + 'python_logger': record.name, + } + for key, value in record.__dict__.items(): + if key not in LOGRECORD_ATTRS: + info[key] = value + + return info + class BackgroundThreadTransport(Transport): """Asynchronous transport that uses a background thread. From c7004cad0feaca83a81b69329250149259680c04 Mon Sep 17 00:00:00 2001 From: YouCyuan Jhang Date: Tue, 11 Sep 2018 12:11:48 -0700 Subject: [PATCH 2/2] Fix method call --- .../cloud/logging/handlers/transports/background_thread.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logging/google/cloud/logging/handlers/transports/background_thread.py b/logging/google/cloud/logging/handlers/transports/background_thread.py index 262ee01354ce..7456d6c05c70 100644 --- a/logging/google/cloud/logging/handlers/transports/background_thread.py +++ b/logging/google/cloud/logging/handlers/transports/background_thread.py @@ -263,7 +263,7 @@ def enqueue(self, record, message, resource=None, labels=None, Specify the trace parameter if span_id is set. """ self._queue.put_nowait({ - 'info': info, + 'info': self.process_info(record, message), 'severity': record.levelname, 'resource': resource, 'labels': labels,