Python Logging Module Handler
Python logging
handlers for Stackdriver Logging.
class google.cloud.logging.handlers.handlers.CloudLoggingHandler(client, name='python', transport=<class 'google.cloud.logging.handlers.transports.background_thread.BackgroundThreadTransport'>, resource=Resource(type='global', labels={}), labels=None, stream=None)
Bases: 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.
Parameters
client (
google.cloud.logging.client.Client
) – the authenticated Google Cloud Logging client for this handler to usename (str) – 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.transport (
type
) – Class for creating new transport objects. It should extend from the baseTransport
type and implement :meth`.Transport.send`. Defaults toBackgroundThreadTransport
. The other option isSyncTransport
.resource (
Resource
) – (Optional) Monitored resource of the entry, defaults to the global resource type.labels (dict) – (Optional) Mapping of labels for the entry.
stream (file-like object) – (optional) stream to be used by the handler.
Example:
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
Initialize the handler.
If stream is not specified, sys.stderr is used.
emit(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
Parameters
record (
logging.LogRecord
) – The record to be logged.
google.cloud.logging.handlers.handlers.setup_logging(handler, excluded_loggers=('google.cloud', 'google.auth', 'google_auth_httplib2'), log_level=20)
Attach a logging handler to the Python root logger
Excludes loggers that this library itself uses to avoid infinite recursion.
Parameters
handler (
logging.handler
) – the handler to attach to the global handlerexcluded_loggers (tuple) – (Optional) The loggers to not attach the handler to. This will always include the loggers in the path of the logging client itself.
log_level (int) – (Optional) Python logging log level. Defaults to
logging.INFO
.
Example:
import logging
import google.cloud.logging
from google.cloud.logging.handlers import CloudLoggingHandler
client = google.cloud.logging.Client()
handler = CloudLoggingHandler(client)
google.cloud.logging.handlers.setup_logging(handler)
logging.getLogger().setLevel(logging.DEBUG)
logging.error('bad news') # API call