Skip to content

Commit 7915d19

Browse files
authored
Fix logging example in docs to include required StreamHandler
The logging example in the documentation previously set the log level but did not include a handler, which can result in no output being shown in many environments (e.g., terminal, script execution). This PR improves the example by: Adding a StreamHandler configured to write to sys.stdout Including a formatter to make the output more readable This change improves developer experience, especially for those new to the library or debugging API calls.
1 parent 9fbe4bf commit 7915d19

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

docs/logging.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,31 @@ In the following code, the logging level is set to `INFO`, and the Google Transl
1616

1717
```python
1818
import logging
19+
import sys
1920
from googleapiclient.discovery import build
2021

22+
# Configure root logger
2123
logger = logging.getLogger()
2224
logger.setLevel(logging.INFO)
2325

26+
# Ensure logs are printed to stdout
27+
handler = logging.StreamHandler(sys.stdout)
28+
formatter = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
29+
handler.setFormatter(formatter)
30+
logger.addHandler(handler)
31+
2432
def main():
25-
service = build('translate', 'v2', developerKey='your_api_key')
26-
print service.translations().list(
27-
source='en',
28-
target='fr',
29-
q=['flower', 'car']
33+
service = build('translate', 'v2', developerKey='your_api_key')
34+
result = service.translations().list(
35+
source='en',
36+
target='fr',
37+
q=['flower', 'car']
3038
).execute()
39+
print(result)
3140

3241
if __name__ == '__main__':
33-
main()
42+
main()
43+
3444
```
3545

3646
The output of this code should print basic logging info:
@@ -48,4 +58,4 @@ For even more detailed logging you can set the debug level of the [httplib2](htt
4858
```python
4959
import httplib2
5060
httplib2.debuglevel = 4
51-
```
61+
```

0 commit comments

Comments
 (0)