Skip to content

Commit e68b033

Browse files
committed
chore: add logging section to readme
1 parent 5e7d5ed commit e68b033

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

README.rst

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,92 @@ In this example all tracing data will be published to the Google
139139

140140
.. _OpenTelemetry documentation: https://opentelemetry-python.readthedocs.io
141141
.. _Cloud Trace: https://cloud.google.com/trace
142+
143+
Logging
144+
-------
145+
146+
This library uses the standard Python :code:`logging` functionality to log some RPC events that could be of interest for debugging and monitoring purposes.
147+
Note the following:
148+
149+
#. Logs may contain sensitive information. Take care to **restrict access to the logs** if they are saved, whether it be on local storage or on Google Cloud Logging.
150+
#. Google may refine the occurrence, level, and content of various log messages in this library without flagging such changes as breaking. **Do not depend on immutability of the logging events**.
151+
#. By default, the logging events from this library are not handled. You must **explicitly configure log handling** using one of the mechanisms below.
152+
153+
Simple, environment-based configuration
154+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
155+
156+
To enable logging for this library without any changes in your code, set the :code:`GOOGLE_SDK_PYTHON_LOGGING_SCOPE` environment variable to a valid Google
157+
logging scope. This configures handling of logging events (at level :code:`logging.DEBUG` or higher) from this library in a default manner, emitting the logged
158+
messages in a structured format. It does not currently allow customizing the logging levels captured nor the handlers, formatters, etc. used for any logging
159+
event.
160+
161+
A logging scope is a period-separated namespace that begins with :code:`google`, identifying the Python module or package to log.
162+
163+
- Valid logging scopes: :code:`google`, :code:`google.cloud.asset.v1`, :code:`google.api`, :code:`google.auth`, etc.
164+
- Invalid logging scopes: :code:`foo`, :code:`123`, etc.
165+
166+
**NOTE**: If the logging scope is invalid, the library does not set up any logging handlers.
167+
168+
Examples
169+
^^^^^^^^
170+
171+
- Enabling the default handler for all Google-based loggers
172+
173+
.. code-block:: console
174+
175+
export GOOGLE_SDK_PYTHON_LOGGING_SCOPE=google
176+
177+
- Enabling the default handler for a specific Google module (for a client library called :code:`google.cloud.bigquery_v2`):
178+
179+
.. code-block:: console
180+
181+
export GOOGLE_SDK_PYTHON_LOGGING_SCOPE=google.cloud.bigquery_v2
182+
183+
184+
Advanced, code-based configuration
185+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
186+
187+
You can also configure a valid logging scope using Python's standard `logging` mechanism.
188+
189+
Examples
190+
^^^^^^^^
191+
192+
- Configuring a handler for all Google-based loggers
193+
194+
.. code-block:: python
195+
196+
import logging
197+
198+
from google.cloud.translate_v3 import translate
199+
200+
base_logger = logging.getLogger("google")
201+
base_logger.addHandler(logging.StreamHandler())
202+
base_logger.setLevel(logging.DEBUG)
203+
204+
- Configuring a handler for a specific Google module (for a client library called :code:`google.cloud.bigquery_v2`):
205+
206+
.. code-block:: python
207+
208+
import logging
209+
210+
from google.cloud.translate_v3 import translate
211+
212+
base_logger = logging.getLogger("google.cloud.bigquery_v2")
213+
base_logger.addHandler(logging.StreamHandler())
214+
base_logger.setLevel(logging.DEBUG)
215+
216+
Logging details
217+
~~~~~~~~~~~~~~~
218+
219+
#. Regardless of which of the mechanisms above you use to configure logging for this library, by default logging events are not propagated up to the root
220+
logger from the `google`-level logger. If you need the events to be propagated to the root logger, you must explicitly set
221+
:code:`logging.getLogger("google").propagate = True` in your code.
222+
#. You can mix the different logging configurations above for different Google modules. For example, you may want use a code-based logging configuration for
223+
one library, but decide you need to also set up environment-based logging configuration for another library.
224+
225+
#. If you attempt to use both code-based and environment-based configuration for the same module, the environment-based configuration will be ineffectual
226+
if the code -based configuration gets applied first.
227+
228+
#. The Google-specific logging configurations (default handlers for environment-based configuration; not propagating logging events to the root logger) get
229+
executed the first time *any* client library is instantiated in your application, and only if the affected loggers have not been previously configured.
230+
(This is the reason for 2.i. above.)

0 commit comments

Comments
 (0)