-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Convert logging doctest examples to testable snippets. #2675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| Integration with Python logging module | ||
| -------------------------------------- | ||
|
|
||
|
|
||
| It's possible to tie the Python :mod:`logging` module directly into Google Cloud Logging. To use it, | ||
| create a :class:`CloudLoggingHandler <google.cloud.logging.CloudLoggingHandler>` instance from your | ||
| Logging client. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| >>> import logging | ||
| >>> import google.cloud.logging # Don't conflict with standard logging | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| >>> 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) # defaults to WARN | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| >>> cloud_logger.addHandler(handler) | ||
| >>> cloud_logger.error('bad news') | ||
|
|
||
| .. note:: | ||
|
|
||
| This handler by default uses an asynchronous transport that sends log entries on a background | ||
| thread. However, the API call will still be made in the same process. For other transport | ||
| options, see the transports section. | ||
|
|
||
| All logs will go to a single custom log, which defaults to "python". The name of the Python | ||
| logger will be included in the structured log entry under the "python_logger" field. You can | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| change it by providing a name to the handler: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| >>> handler = CloudLoggingHandler(client, name="mycustomlog") | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| It is also possible to attach the handler to the root Python logger, so that for example a plain | ||
| `logging.warn` call would be sent to Cloud Logging, as well as any other loggers created. However, | ||
| you must avoid infinite recursion from the logging calls the client itself makes. A helper | ||
| method :meth:`setup_logging <google.cloud.logging.handlers.setup_logging>` is provided to configure | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| this automatically: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| >>> import logging | ||
| >>> import google.cloud.logging # Don't conflict with standard logging | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| >>> from google.cloud.logging.handlers import CloudLoggingHandler, setup_logging | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| >>> client = google.cloud.logging.Client() | ||
| >>> handler = CloudLoggingHandler(client) | ||
| >>> logging.getLogger().setLevel(logging.INFO) # defaults to WARN | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| >>> setup_logging(handler) | ||
| >>> logging.error('bad news') | ||
|
|
||
| You can also exclude certain loggers: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| >>> setup_logging(handler, excluded_loggers=('werkzeug',))) | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
|
|
||
|
|
||
This comment was marked as spam.
Sorry, something went wrong. |
||
| Python logging handler transports | ||
| ================================== | ||
|
|
||
| The Python logging handler can use different transports. The default is | ||
| :class:`google.cloud.logging.handlers.BackgroundThreadTransport`. | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| 1. :class:`google.cloud.logging.handlers.BackgroundThreadTransport` this is the default. It writes | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| entries on a background :class:`python.threading.Thread`. | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| 1. :class:`google.cloud.logging.handlers.SyncTransport` this handler does a direct API call on each | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| logging statement to write the entry. | ||
|
|
||
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.