|
6 | 6 |
|
7 | 7 | # Standard library
|
8 | 8 | import datetime as dt
|
| 9 | +import logging |
9 | 10 | import os
|
10 | 11 | import sys
|
11 | 12 | import traceback
|
|
37 | 38 | API_KEYS = os.getenv("GOOGLE_API_KEYS").split(",")
|
38 | 39 | PSE_KEY = os.getenv("PSE_KEY")
|
39 | 40 |
|
| 41 | +# Set up logging |
| 42 | +logging.basicConfig( |
| 43 | + level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" |
| 44 | +) |
| 45 | +logger = logging.getLogger(__name__) |
| 46 | + |
40 | 47 |
|
41 | 48 | def get_license_list():
|
42 | 49 | """
|
@@ -82,7 +89,7 @@ def get_request_url(license):
|
82 | 89 | )
|
83 | 90 | except Exception as e:
|
84 | 91 | if isinstance(e, IndexError):
|
85 |
| - print("Depleted all API Keys provided", file=sys.stderr) |
| 92 | + logger.error("Depleted all API Keys provided") |
86 | 93 | else:
|
87 | 94 | raise e
|
88 | 95 |
|
@@ -122,9 +129,7 @@ def get_response_elems(license):
|
122 | 129 | # If quota limit exceeded, switch to the next API key
|
123 | 130 | global API_KEYS_IND
|
124 | 131 | API_KEYS_IND += 1
|
125 |
| - print( |
126 |
| - "Changing API KEYS due to depletion of quota", file=sys.stderr |
127 |
| - ) |
| 132 | + logger.error("Changing API KEYS due to depletion of quota") |
128 | 133 | return get_response_elems(license)
|
129 | 134 | else:
|
130 | 135 | raise e
|
@@ -177,9 +182,9 @@ def main():
|
177 | 182 | except SystemExit as e:
|
178 | 183 | sys.exit(e.code)
|
179 | 184 | except KeyboardInterrupt:
|
180 |
| - print("INFO (130) Halted via KeyboardInterrupt.", file=sys.stderr) |
| 185 | + logger.info("Halted via KeyboardInterrupt.") |
181 | 186 | sys.exit(130)
|
182 | 187 | except Exception:
|
183 |
| - print("ERROR (1) Unhandled exception:", file=sys.stderr) |
184 |
| - print(traceback.print_exc(), file=sys.stderr) |
| 188 | + logger.exception("Unhandled exception:") |
| 189 | + logger.exception(traceback.print_exc()) |
185 | 190 | sys.exit(1)
|
0 commit comments