@@ -267,3 +267,122 @@ Delete a sink:
267267 :start-after: [START sink_delete]
268268 :end-before: [END sink_delete]
269269 :dedent: 4
270+ Integration with Python logging module
271+ --------------------------------------
272+
273+ It's possible to tie the Python :mod: `logging ` module directly into Google
274+ Stackdriver Logging. There are different handler options to accomplish this.
275+ To automatically pick the default for your current environment, use
276+ :meth: `~google.cloud.logging.client.Client.get_default_handler `.
277+
278+ .. code-block :: python
279+
280+ >> > import logging
281+ >> > import google.cloud.logging # Don't conflict with standard logging
282+ >> > from google.cloud.logging.handlers import CloudLoggingHandler
283+ >> > client = google.cloud.logging.Client()
284+ >> > handler = client.get_default_handler()
285+ >> > cloud_logger = logging.getLogger(' cloudLogger' )
286+ >> > cloud_logger.setLevel(logging.INFO )
287+ >> > cloud_logger.addHandler(handler)
288+ >> > cloud_logger.error(' bad news' )
289+
290+ It is also possible to attach the handler to the root Python logger, so that
291+ for example a plain ``logging.warn `` call would be sent to Stackdriver Logging,
292+ as well as any other loggers created. A helper method
293+ :meth: `~google.cloud.logging.client.Client.setup_logging ` is provided
294+ to configure this automatically.
295+
296+ .. code-block :: python
297+
298+ >> > import logging
299+ >> > import google.cloud.logging # Don't conflict with standard logging
300+ >> > client = google.cloud.logging.Client()
301+ >> > client.setup_logging(log_level = logging.INFO )
302+
303+ .. note ::
304+
305+ To reduce cost and quota usage, do not enable Stackdriver logging
306+ handlers while testing locally.
307+
308+ You can also exclude certain loggers:
309+
310+ .. code-block :: python
311+
312+ >> > client.setup_logging(log_level = logging.INFO ,
313+ excluded_loggers = (' werkzeug' ,))
314+
315+ Cloud Logging Handler
316+ =====================
317+
318+ If you prefer not to use
319+ :meth: `~google.cloud.logging.client.Client.get_default_handler `, you can
320+ directly create a
321+ :class: `~google.cloud.logging.handlers.handlers.CloudLoggingHandler ` instance
322+ which will write directly to the API.
323+
324+ .. code-block :: python
325+
326+ >> > import logging
327+ >> > import google.cloud.logging
328+ >> > from google.cloud.logging.handlers import CloudLoggingHandler
329+ >> > client = google.cloud.logging.Client()
330+ >> > handler = CloudLoggingHandler(client)
331+ >> > cloud_logger = logging.getLogger(' cloudLogger' )
332+ >> > cloud_logger.setLevel(logging.INFO )
333+ >> > cloud_logger.addHandler(handler)
334+ >> > cloud_logger.error(' bad news' )
335+
336+ .. note ::
337+
338+ This handler by default uses an asynchronous transport that sends log
339+ entries on a background thread. However, the API call will still be made
340+ in the same process. For other transport options, see the transports
341+ section.
342+
343+ All logs will go to a single custom log, which defaults to "python". The name
344+ of the Python logger will be included in the structured log entry under the
345+ "python_logger" field. You can change it by providing a name to the handler:
346+
347+ .. code-block :: python
348+
349+ >> > handler = CloudLoggingHandler(client, name = ' mycustomlog' )
350+
351+ fluentd logging handlers
352+ ========================
353+
354+ Besides :class: `~google.cloud.logging.handlers.handlers.CloudLoggingHandler `,
355+ which writes directly to the API, two other handlers are provided.
356+ :class: `~google.cloud.logging.handlers.app_engine.AppEngineHandler `, which is
357+ recommended when running on the Google App Engine Flexible vanilla runtimes
358+ (i.e. your app.yaml contains ``runtime: python ``), and
359+ :class: `~google.cloud.logging.handlers.container_engine.ContainerEngineHandler `
360+ , which is recommended when running on `Google Container Engine `_ with the
361+ Stackdriver Logging plugin enabled.
362+
363+ :meth: `~google.cloud.logging.client.Client.get_default_handler ` and
364+ :meth: `~google.cloud.logging.client.Client.setup_logging ` will attempt to use
365+ the environment to automatically detect whether the code is running in
366+ these platforms and use the appropriate handler.
367+
368+ In both cases, the fluentd agent is configured to automatically parse log files
369+ in an expected format and forward them to Stackdriver logging. The handlers
370+ provided help set the correct metadata such as log level so that logs can be
371+ filtered accordingly.
372+
373+ Cloud Logging Handler transports
374+ =================================
375+
376+ The :class: `~google.cloud.logging.handlers.handlers.CloudLoggingHandler `
377+ logging handler can use different transports. The default is
378+ :class: `~google.cloud.logging.handlers.BackgroundThreadTransport `.
379+
380+ 1. :class: `~google.cloud.logging.handlers.BackgroundThreadTransport ` this is
381+ the default. It writes entries on a background
382+ :class: `python.threading.Thread `.
383+
384+ 1. :class: `~google.cloud.logging.handlers.SyncTransport ` this handler does a
385+ direct API call on each logging statement to write the entry.
386+
387+
388+ .. _Google Container Engine : https://cloud.google.com/container-engine/
0 commit comments