Skip to content

Commit 1fb7b51

Browse files
authored
chore: update readme for logging (#2062)
1 parent ccbc59b commit 1fb7b51

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

synthtool/gcp/templates/python_library/README.rst

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,92 @@ Next Steps
110110

111111
.. _{{ metadata['repo']['name_pretty'] }} API Product documentation: {{ metadata['repo']['product_documentation'] }}
112112
.. _README: https://github.com/googleapis/google-cloud-python/blob/main/README.rst
113+
114+
Logging
115+
-------
116+
117+
This library uses the standard Python :code:`logging` functionality to log some RPC events that could be of interest for debugging and monitoring purposes.
118+
Note the following:
119+
120+
#. 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.
121+
#. 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**.
122+
#. By default, the logging events from this library are not handled. You must **explicitly configure log handling** using one of the mechanisms below.
123+
124+
Simple, environment-based configuration
125+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126+
127+
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
128+
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
129+
messages in a structured format. It does not currently allow customizing the logging levels captured nor the handlers, formatters, etc. used for any logging
130+
event.
131+
132+
A logging scope is a period-separated namespace that begins with :code:`google`, identifying the Python module or package to log.
133+
134+
- Valid logging scopes: :code:`google`, :code:`google.cloud.asset.v1`, :code:`google.api`, :code:`google.auth`, etc.
135+
- Invalid logging scopes: :code:`foo`, :code:`123`, etc.
136+
137+
**NOTE**: If the logging scope is invalid, the library does not set up any logging handlers.
138+
139+
Examples
140+
^^^^^^^^
141+
142+
- Enabling the default handler for all Google-based loggers
143+
144+
.. code-block:: console
145+
146+
export GOOGLE_SDK_PYTHON_LOGGING_SCOPE=google
147+
148+
- Enabling the default handler for a specific Google module (for a client library called :code:`library_v1`):
149+
150+
.. code-block:: console
151+
152+
export GOOGLE_SDK_PYTHON_LOGGING_SCOPE=google.cloud.library_v1
153+
154+
155+
Advanced, code-based configuration
156+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
157+
158+
You can also configure a valid logging scope using Python's standard `logging` mechanism.
159+
160+
Examples
161+
^^^^^^^^
162+
163+
- Configuring a handler for all Google-based loggers
164+
165+
.. code-block:: python
166+
167+
import logging
168+
169+
from google.cloud.translate_v3 import translate
170+
171+
base_logger = logging.getLogger("google")
172+
base_logger.addHandler(logging.StreamHandler())
173+
base_logger.setLevel(logging.DEBUG)
174+
175+
- Configuring a handler for a specific Google module (for a client library called :code:`library_v1`):
176+
177+
.. code-block:: python
178+
179+
import logging
180+
181+
from google.cloud.translate_v3 import translate
182+
183+
base_logger = logging.getLogger("google.cloud.library_v1")
184+
base_logger.addHandler(logging.StreamHandler())
185+
base_logger.setLevel(logging.DEBUG)
186+
187+
Logging details
188+
~~~~~~~~~~~~~~~
189+
190+
#. 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
191+
logger from the `google`-level logger. If you need the events to be propagated to the root logger, you must explicitly set
192+
:code:`logging.getLogger("google").propagate = True` in your code.
193+
#. You can mix the different logging configurations above for different Google modules. For example, you may want use a code-based logging configuration for
194+
one library, but decide you need to also set up environment-based logging configuration for another library.
195+
196+
#. If you attempt to use both code-based and environment-based configuration for the same module, the environment-based configuration will be ineffectual
197+
if the code -based configuration gets applied first.
198+
199+
#. The Google-specific logging configurations (default handlers for environment-based configuration; not propagating logging events to the root logger) get
200+
executed the first time *any* client library is instantiated in your application, and only if the affected loggers have not been previously configured.
201+
(This is the reason for 2.i. above.)

0 commit comments

Comments
 (0)