Skip to content

Instantly share code, notes, and snippets.

@briggleman
Created June 19, 2019 19:56
Show Gist options
  • Save briggleman/0b25c0e2814b3f5f337f10720d94c9fc to your computer and use it in GitHub Desktop.
Save briggleman/0b25c0e2814b3f5f337f10720d94c9fc to your computer and use it in GitHub Desktop.
import logging
import google.cloud.logging
from google.cloud.logging.handlers.transports import BackgroundThreadTransport
from google.cloud.logging.logger import _GLOBAL_RESOURCE
DEFAULT_LOGGER_NAME = "python"
EXCLUDED_LOGGER_DEFAULTS = ("google.cloud", "google.auth", "google_auth_httplib2")
class Stackdriver(logging.StreamHandler):
"""Handler that directly makes Stackdriver logging API calls.
This is a Python standard ``logging`` handler using that can be used to
route Python standard logging messages directly to the Stackdriver
Logging API.
This handler is used when not in GAE or GKE environment.
This handler supports both an asynchronous and synchronous transport.
:type client: :class:`google.cloud.logging.client.Client`
:param client: the authenticated Google Cloud Logging client for this
handler to use
:type name: str
:param name: the name of the custom log in Stackdriver Logging. Defaults
to 'python'. The name of the Python logger will be represented
in the ``python_logger`` field.
:type transport: :class:`type`
:param transport: Class for creating new transport objects. It should
extend from the base :class:`.Transport` type and
implement :meth`.Transport.send`. Defaults to
:class:`.BackgroundThreadTransport`. The other
option is :class:`.SyncTransport`.
:type resource: :class:`~google.cloud.logging.resource.Resource`
:param resource: (Optional) Monitored resource of the entry, defaults
to the global resource type.
:type labels: dict
:param labels: (Optional) Mapping of labels for the entry.
:type stream: file-like object
:param stream: (optional) stream to be used by the handler.
Example:
.. code-block:: python
import logging
import google.cloud.logging
from google.cloud.logging.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') # API call
"""
def __init__(self, client=google.cloud.logging.Client(), name=DEFAULT_LOGGER_NAME, transport=BackgroundThreadTransport, resource=_GLOBAL_RESOURCE, labels=None, stream=None):
super(Stackdriver, self).__init__(stream)
self.name = name
self.client = client
self.resource = resource
self.labels = labels
self.transport = transport(client, name)
def emit(self, record):
"""Actually log the specified logging record.
Overrides the default emit behavior of ``StreamHandler``.
See https://docs.python.org/2/library/logging.html#handler-objects
:type record: :class:`logging.LogRecord`
:param record: The record to be logged.
"""
message = super(Stackdriver, self).format(record)
self.transport.send(record, message, resource=self.resource, labels=self.labels)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment