Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 1 | # Logging |
| 2 | |
| 3 | This page provides logging tips to help you debug your applications. |
| 4 | |
| 5 | ## Log Level |
| 6 | |
| 7 | 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: |
| 8 | |
| 9 | - CRITICAL (least amount of logging) |
| 10 | - ERROR |
| 11 | - WARNING |
| 12 | - INFO |
| 13 | - DEBUG (most amount of logging) |
| 14 | |
| 15 | In the following code, the logging level is set to `INFO`, and the Google Translate API is called: |
| 16 | |
Bu Sun Kim | 8496ebe | 2019-08-12 18:04:35 -0700 | [diff] [blame^] | 17 | ```python |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 18 | import logging |
Bu Sun Kim | 8496ebe | 2019-08-12 18:04:35 -0700 | [diff] [blame^] | 19 | from googleapiclient.discovery import build |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 20 | |
| 21 | logger = logging.getLogger() |
| 22 | logger.setLevel(logging.INFO) |
| 23 | |
| 24 | 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'] |
| 30 | ).execute() |
| 31 | |
| 32 | if __name__ == '__main__': |
| 33 | main() |
| 34 | ``` |
| 35 | |
| 36 | The output of this code should print basic logging info: |
| 37 | |
| 38 | ``` |
| 39 | INFO:root:URL being requested: https://www.googleapis.com/discovery/v1/apis/translate/v2/rest |
| 40 | 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 |
| 41 | {u'translations': [{u'translatedText': u'fleur'}, {u'translatedText': u'voiture'}]} |
| 42 | ``` |
| 43 | |
| 44 | ## HTTP Traffic |
| 45 | |
| 46 | 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: |
| 47 | |
Bu Sun Kim | 8496ebe | 2019-08-12 18:04:35 -0700 | [diff] [blame^] | 48 | ```python |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 49 | import httplib2 |
| 50 | httplib2.debuglevel = 4 |
| 51 | ``` |