Skip to content

Commit 08b1969

Browse files
ohmayrvchudnov-g
andauthored
chore: add documentation for logging (#2308)
Co-authored-by: Victor Chudnovsky <vchudnov@google.com>
1 parent cb23844 commit 08b1969

File tree

6 files changed

+563
-0
lines changed

6 files changed

+563
-0
lines changed

gapic/templates/README.rst.j2

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,96 @@ Windows
4848
<your-env>\Scripts\activate
4949
<your-env>\Scripts\pip.exe install \path\to\library
5050

51+
52+
Logging
53+
-------
54+
55+
This library uses the standard Python :code:`logging` functionality to log some RPC events that could be of interest for debugging and monitoring purposes.
56+
Note the following:
57+
58+
#. 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.
59+
#. 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**.
60+
#. By default, the logging events from this library are not handled. You must **explicitly configure log handling** using one of the mechanisms below.
61+
62+
63+
Simple, environment-based configuration
64+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65+
66+
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
67+
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
68+
messages in a structured format. It does not currently allow customizing the logging levels captured nor the handlers, formatters, etc. used for any logging
69+
event.
70+
71+
A logging scope is a period-separated namespace that begins with :code:`google`, identifying the Python module or package to log.
72+
73+
- Valid logging scopes: :code:`google`, :code:`google.cloud.asset.v1`, :code:`google.api`, :code:`google.auth`, etc.
74+
- Invalid logging scopes: :code:`foo`, :code:`123`, etc.
75+
76+
**NOTE**: If the logging scope is invalid, the library does not set up any logging handlers.
77+
78+
79+
Examples
80+
^^^^^^^^
81+
82+
- Enabling the default handler for all Google-based loggers
83+
84+
.. code-block:: console
85+
86+
export GOOGLE_SDK_PYTHON_LOGGING_SCOPE=google
87+
88+
- Enabling the default handler for a specific Google module (for a client library called :code:`library_v1`):
89+
90+
.. code-block:: console
91+
92+
export GOOGLE_SDK_PYTHON_LOGGING_SCOPE=google.cloud.library_v1
93+
94+
95+
Advanced, code-based configuration
96+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97+
98+
You can also configure a valid logging scope using Python's standard `logging` mechanism.
99+
100+
101+
Examples
102+
^^^^^^^^
103+
104+
- Configuring a handler for all Google-based loggers
105+
106+
.. code-block:: python
107+
108+
import logging
109+
110+
from google.cloud.translate_v3 import translate
111+
112+
base_logger = logging.getLogger("google")
113+
base_logger.addHandler(logging.StreamHandler())
114+
base_logger.setLevel(logging.DEBUG)
115+
116+
- Configuring a handler for a specific Google module (for a client library called :code:`library_v1`):
117+
118+
.. code-block:: python
119+
120+
import logging
121+
122+
from google.cloud.translate_v3 import translate
123+
124+
base_logger = logging.getLogger("google.cloud.library_v1")
125+
base_logger.addHandler(logging.StreamHandler())
126+
base_logger.setLevel(logging.DEBUG)
127+
128+
129+
Logging details
130+
~~~~~~~~~~~~~~~
131+
132+
#. 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
133+
logger from the `google`-level logger. If you need the events to be propagated to the root logger, you must explicitly set
134+
:code:`logging.getLogger("google").propagate = True` in your code.
135+
#. You can mix the different logging configurations above for different Google modules. For example, you may want use a code-based logging configuration for
136+
one library, but decide you need to also set up environment-based logging configuration for another library.
137+
138+
#. If you attempt to use both code-based and environment-based configuration for the same module, the environment-based configuration will be ineffectual
139+
if the code -based configuration gets applied first.
140+
141+
#. The Google-specific logging configurations (default handlers for environment-based configuration; not propagating logging events to the root logger) get
142+
executed the first time *any* client library is instantiated in your application, and only if the affected loggers have not been previously configured.
143+
(This is the reason for 2.i. above.)

tests/integration/goldens/asset/README.rst

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,97 @@ Windows
4747
python3 -m venv <your-env>
4848
<your-env>\Scripts\activate
4949
<your-env>\Scripts\pip.exe install \path\to\library
50+
51+
52+
Logging
53+
-------
54+
55+
This library uses the standard Python :code:`logging` functionality to log some RPC events that could be of interest for debugging and monitoring purposes.
56+
Note the following:
57+
58+
#. 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.
59+
#. 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**.
60+
#. By default, the logging events from this library are not handled. You must **explicitly configure log handling** using one of the mechanisms below.
61+
62+
63+
Simple, environment-based configuration
64+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65+
66+
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
67+
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
68+
messages in a structured format. It does not currently allow customizing the logging levels captured nor the handlers, formatters, etc. used for any logging
69+
event.
70+
71+
A logging scope is a period-separated namespace that begins with :code:`google`, identifying the Python module or package to log.
72+
73+
- Valid logging scopes: :code:`google`, :code:`google.cloud.asset.v1`, :code:`google.api`, :code:`google.auth`, etc.
74+
- Invalid logging scopes: :code:`foo`, :code:`123`, etc.
75+
76+
**NOTE**: If the logging scope is invalid, the library does not set up any logging handlers.
77+
78+
79+
Examples
80+
^^^^^^^^
81+
82+
- Enabling the default handler for all Google-based loggers
83+
84+
.. code-block:: console
85+
86+
export GOOGLE_SDK_PYTHON_LOGGING_SCOPE=google
87+
88+
- Enabling the default handler for a specific Google module (for a client library called :code:`library_v1`):
89+
90+
.. code-block:: console
91+
92+
export GOOGLE_SDK_PYTHON_LOGGING_SCOPE=google.cloud.library_v1
93+
94+
95+
Advanced, code-based configuration
96+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97+
98+
You can also configure a valid logging scope using Python's standard `logging` mechanism.
99+
100+
101+
Examples
102+
^^^^^^^^
103+
104+
- Configuring a handler for all Google-based loggers
105+
106+
.. code-block:: python
107+
108+
import logging
109+
110+
from google.cloud.translate_v3 import translate
111+
112+
base_logger = logging.getLogger("google")
113+
base_logger.addHandler(logging.StreamHandler())
114+
base_logger.setLevel(logging.DEBUG)
115+
116+
- Configuring a handler for a specific Google module (for a client library called :code:`library_v1`):
117+
118+
.. code-block:: python
119+
120+
import logging
121+
122+
from google.cloud.translate_v3 import translate
123+
124+
base_logger = logging.getLogger("google.cloud.library_v1")
125+
base_logger.addHandler(logging.StreamHandler())
126+
base_logger.setLevel(logging.DEBUG)
127+
128+
129+
Logging details
130+
~~~~~~~~~~~~~~~
131+
132+
#. 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
133+
logger from the `google`-level logger. If you need the events to be propagated to the root logger, you must explicitly set
134+
:code:`logging.getLogger("google").propagate = True` in your code.
135+
#. You can mix the different logging configurations above for different Google modules. For example, you may want use a code-based logging configuration for
136+
one library, but decide you need to also set up environment-based logging configuration for another library.
137+
138+
#. If you attempt to use both code-based and environment-based configuration for the same module, the environment-based configuration will be ineffectual
139+
if the code -based configuration gets applied first.
140+
141+
#. The Google-specific logging configurations (default handlers for environment-based configuration; not propagating logging events to the root logger) get
142+
executed the first time *any* client library is instantiated in your application, and only if the affected loggers have not been previously configured.
143+
(This is the reason for 2.i. above.)

tests/integration/goldens/credentials/README.rst

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,97 @@ Windows
4747
python3 -m venv <your-env>
4848
<your-env>\Scripts\activate
4949
<your-env>\Scripts\pip.exe install \path\to\library
50+
51+
52+
Logging
53+
-------
54+
55+
This library uses the standard Python :code:`logging` functionality to log some RPC events that could be of interest for debugging and monitoring purposes.
56+
Note the following:
57+
58+
#. 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.
59+
#. 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**.
60+
#. By default, the logging events from this library are not handled. You must **explicitly configure log handling** using one of the mechanisms below.
61+
62+
63+
Simple, environment-based configuration
64+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65+
66+
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
67+
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
68+
messages in a structured format. It does not currently allow customizing the logging levels captured nor the handlers, formatters, etc. used for any logging
69+
event.
70+
71+
A logging scope is a period-separated namespace that begins with :code:`google`, identifying the Python module or package to log.
72+
73+
- Valid logging scopes: :code:`google`, :code:`google.cloud.asset.v1`, :code:`google.api`, :code:`google.auth`, etc.
74+
- Invalid logging scopes: :code:`foo`, :code:`123`, etc.
75+
76+
**NOTE**: If the logging scope is invalid, the library does not set up any logging handlers.
77+
78+
79+
Examples
80+
^^^^^^^^
81+
82+
- Enabling the default handler for all Google-based loggers
83+
84+
.. code-block:: console
85+
86+
export GOOGLE_SDK_PYTHON_LOGGING_SCOPE=google
87+
88+
- Enabling the default handler for a specific Google module (for a client library called :code:`library_v1`):
89+
90+
.. code-block:: console
91+
92+
export GOOGLE_SDK_PYTHON_LOGGING_SCOPE=google.cloud.library_v1
93+
94+
95+
Advanced, code-based configuration
96+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97+
98+
You can also configure a valid logging scope using Python's standard `logging` mechanism.
99+
100+
101+
Examples
102+
^^^^^^^^
103+
104+
- Configuring a handler for all Google-based loggers
105+
106+
.. code-block:: python
107+
108+
import logging
109+
110+
from google.cloud.translate_v3 import translate
111+
112+
base_logger = logging.getLogger("google")
113+
base_logger.addHandler(logging.StreamHandler())
114+
base_logger.setLevel(logging.DEBUG)
115+
116+
- Configuring a handler for a specific Google module (for a client library called :code:`library_v1`):
117+
118+
.. code-block:: python
119+
120+
import logging
121+
122+
from google.cloud.translate_v3 import translate
123+
124+
base_logger = logging.getLogger("google.cloud.library_v1")
125+
base_logger.addHandler(logging.StreamHandler())
126+
base_logger.setLevel(logging.DEBUG)
127+
128+
129+
Logging details
130+
~~~~~~~~~~~~~~~
131+
132+
#. 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
133+
logger from the `google`-level logger. If you need the events to be propagated to the root logger, you must explicitly set
134+
:code:`logging.getLogger("google").propagate = True` in your code.
135+
#. You can mix the different logging configurations above for different Google modules. For example, you may want use a code-based logging configuration for
136+
one library, but decide you need to also set up environment-based logging configuration for another library.
137+
138+
#. If you attempt to use both code-based and environment-based configuration for the same module, the environment-based configuration will be ineffectual
139+
if the code -based configuration gets applied first.
140+
141+
#. The Google-specific logging configurations (default handlers for environment-based configuration; not propagating logging events to the root logger) get
142+
executed the first time *any* client library is instantiated in your application, and only if the affected loggers have not been previously configured.
143+
(This is the reason for 2.i. above.)

tests/integration/goldens/eventarc/README.rst

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,97 @@ Windows
4747
python3 -m venv <your-env>
4848
<your-env>\Scripts\activate
4949
<your-env>\Scripts\pip.exe install \path\to\library
50+
51+
52+
Logging
53+
-------
54+
55+
This library uses the standard Python :code:`logging` functionality to log some RPC events that could be of interest for debugging and monitoring purposes.
56+
Note the following:
57+
58+
#. 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.
59+
#. 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**.
60+
#. By default, the logging events from this library are not handled. You must **explicitly configure log handling** using one of the mechanisms below.
61+
62+
63+
Simple, environment-based configuration
64+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65+
66+
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
67+
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
68+
messages in a structured format. It does not currently allow customizing the logging levels captured nor the handlers, formatters, etc. used for any logging
69+
event.
70+
71+
A logging scope is a period-separated namespace that begins with :code:`google`, identifying the Python module or package to log.
72+
73+
- Valid logging scopes: :code:`google`, :code:`google.cloud.asset.v1`, :code:`google.api`, :code:`google.auth`, etc.
74+
- Invalid logging scopes: :code:`foo`, :code:`123`, etc.
75+
76+
**NOTE**: If the logging scope is invalid, the library does not set up any logging handlers.
77+
78+
79+
Examples
80+
^^^^^^^^
81+
82+
- Enabling the default handler for all Google-based loggers
83+
84+
.. code-block:: console
85+
86+
export GOOGLE_SDK_PYTHON_LOGGING_SCOPE=google
87+
88+
- Enabling the default handler for a specific Google module (for a client library called :code:`library_v1`):
89+
90+
.. code-block:: console
91+
92+
export GOOGLE_SDK_PYTHON_LOGGING_SCOPE=google.cloud.library_v1
93+
94+
95+
Advanced, code-based configuration
96+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97+
98+
You can also configure a valid logging scope using Python's standard `logging` mechanism.
99+
100+
101+
Examples
102+
^^^^^^^^
103+
104+
- Configuring a handler for all Google-based loggers
105+
106+
.. code-block:: python
107+
108+
import logging
109+
110+
from google.cloud.translate_v3 import translate
111+
112+
base_logger = logging.getLogger("google")
113+
base_logger.addHandler(logging.StreamHandler())
114+
base_logger.setLevel(logging.DEBUG)
115+
116+
- Configuring a handler for a specific Google module (for a client library called :code:`library_v1`):
117+
118+
.. code-block:: python
119+
120+
import logging
121+
122+
from google.cloud.translate_v3 import translate
123+
124+
base_logger = logging.getLogger("google.cloud.library_v1")
125+
base_logger.addHandler(logging.StreamHandler())
126+
base_logger.setLevel(logging.DEBUG)
127+
128+
129+
Logging details
130+
~~~~~~~~~~~~~~~
131+
132+
#. 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
133+
logger from the `google`-level logger. If you need the events to be propagated to the root logger, you must explicitly set
134+
:code:`logging.getLogger("google").propagate = True` in your code.
135+
#. You can mix the different logging configurations above for different Google modules. For example, you may want use a code-based logging configuration for
136+
one library, but decide you need to also set up environment-based logging configuration for another library.
137+
138+
#. If you attempt to use both code-based and environment-based configuration for the same module, the environment-based configuration will be ineffectual
139+
if the code -based configuration gets applied first.
140+
141+
#. The Google-specific logging configurations (default handlers for environment-based configuration; not propagating logging events to the root logger) get
142+
executed the first time *any* client library is instantiated in your application, and only if the affected loggers have not been previously configured.
143+
(This is the reason for 2.i. above.)

0 commit comments

Comments
 (0)