1616
1717import logging
1818
19+ from gcloud .logging .handlers .transports import BackgroundThreadTransport
20+
21+
1922EXCLUDE_LOGGER_DEFAULTS = (
2023 'gcloud' ,
21- 'oauth2client.client '
24+ 'oauth2client'
2225)
2326
27+ DEFAULT_LOGGER_NAME = 'python'
28+
2429
25- class CloudLoggingHandler (logging .StreamHandler , object ):
26- """Python standard logging handler to log messages to the Google Cloud
27- Logging API.
30+ class CloudLoggingHandler (logging .StreamHandler ):
31+ """Python standard ``logging`` handler.
2832
29- This handler can be used to route Python standard logging messages to
30- Google Cloud logging .
33+ This handler can be used to route Python standard logging messages
34+ directly to the Google Cloud Logging API .
3135
3236 Note that this handler currently only supports a synchronous API call,
3337 which means each logging statement that uses this handler will require
@@ -37,6 +41,18 @@ class CloudLoggingHandler(logging.StreamHandler, object):
3741 :param client: the authenticated gcloud logging client for this handler
3842 to use
3943
44+ :type name: str
45+ :param name: the name of the custom log in Stackdriver Logging. Defaults
46+ to 'python'. The name of the Python logger will be represented
47+ in the ``python_logger`` field.
48+
49+ :type transport: type
50+ :param transport: Class for creating new transport objects. It should
51+ extend from the base :class:`.Transport` type and
52+ implement :meth`.Transport.send`. Defaults to
53+ :class:`.BackgroundThreadTransport`. The other
54+ option is :class:`.SyncTransport`.
55+
4056 Example:
4157
4258 .. doctest::
@@ -51,30 +67,37 @@ class CloudLoggingHandler(logging.StreamHandler, object):
5167 cloud_logger.setLevel(logging.INFO)
5268 cloud_logger.addHandler(handler)
5369
54- cloud.logger.error(" bad news") # API call
70+ cloud.logger.error(' bad news') # API call
5571
5672 """
5773
58- def __init__ (self , client ):
74+ def __init__ (self , client ,
75+ name = DEFAULT_LOGGER_NAME ,
76+ transport = BackgroundThreadTransport ):
5977 super (CloudLoggingHandler , self ).__init__ ()
78+ self .name = name
6079 self .client = client
80+ self .transport = transport (client , name )
6181
6282 def emit (self , record ):
63- """
64- Overrides the default emit behavior of StreamHandler.
83+ """Actually log the specified logging record.
84+
85+ Overrides the default emit behavior of ``StreamHandler``.
6586
6687 See: https://docs.python.org/2/library/logging.html#handler-objects
88+
89+ :type record: :class:`logging.LogRecord`
90+ :param record: The record to be logged.
6791 """
6892 message = super (CloudLoggingHandler , self ).format (record )
69- logger = self .client .logger (record .name )
70- logger .log_struct ({"message" : message },
71- severity = record .levelname )
93+ self .transport .send (record , message )
7294
7395
7496def setup_logging (handler , excluded_loggers = EXCLUDE_LOGGER_DEFAULTS ):
75- """Helper function to attach the CloudLoggingAPI handler to the Python
76- root logger, while excluding loggers this library itself uses to avoid
77- infinite recursion
97+ """Attach the ``CloudLogging`` handler to the Python root logger
98+
99+ Excludes loggers that this library itself uses to avoid
100+ infinite recursion.
78101
79102 :type handler: :class:`logging.handler`
80103 :param handler: the handler to attach to the global handler
@@ -90,14 +113,14 @@ def setup_logging(handler, excluded_loggers=EXCLUDE_LOGGER_DEFAULTS):
90113
91114 import logging
92115 import gcloud.logging
93- from gcloud.logging.handlers import CloudLoggingAPIHandler
116+ from gcloud.logging.handlers import CloudLoggingHandler
94117
95118 client = gcloud.logging.Client()
96119 handler = CloudLoggingHandler(client)
97- setup_logging(handler)
120+ gcloud.logging. setup_logging(handler)
98121 logging.getLogger().setLevel(logging.DEBUG)
99122
100- logging.error(" bad news") # API call
123+ logging.error(' bad news') # API call
101124
102125 """
103126 all_excluded_loggers = set (excluded_loggers + EXCLUDE_LOGGER_DEFAULTS )
0 commit comments