File size: 1,560 Bytes
065fee7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# Logging
This page provides logging tips to help you debug your applications.
## Log Level
You can enable logging of key events in this library by configuring Python's standard [logging](http://docs.python.org/library/logging.html) module. You can set the logging level to one of the following:
- CRITICAL (least amount of logging)
- ERROR
- WARNING
- INFO
- DEBUG (most amount of logging)
In the following code, the logging level is set to `INFO`, and the Google Translate API is called:
```python
import logging
from googleapiclient.discovery import build
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def main():
service = build('translate', 'v2', developerKey='your_api_key')
print service.translations().list(
source='en',
target='fr',
q=['flower', 'car']
).execute()
if __name__ == '__main__':
main()
```
The output of this code should print basic logging info:
```
INFO:root:URL being requested: https://www.googleapis.com/discovery/v1/apis/translate/v2/rest
INFO:root:URL being requested: https://www.googleapis.com/language/translate/v2?q=flower&q=car&source=en&alt=json&target=fr&key=your_api_key
{u'translations': [{u'translatedText': u'fleur'}, {u'translatedText': u'voiture'}]}
```
## HTTP Traffic
For even more detailed logging you can set the debug level of the [httplib2](https://github.com/httplib2/httplib2) module used by this library. The following code snippet enables logging of all HTTP request and response headers and bodies:
```python
import httplib2
httplib2.debuglevel = 4
``` |