|
1 | 1 | import unicodecsv as csv |
2 | 2 | import json |
| 3 | +import logging |
3 | 4 | import sys |
4 | 5 | import os |
5 | 6 | import copy |
@@ -174,13 +175,35 @@ def __init__(self, audit_json, export_inactive_items=True): |
174 | 175 |
|
175 | 176 | :param audit_json: audit in JSON format to be converted to CSV |
176 | 177 | """ |
| 178 | + self.configure_logging() |
177 | 179 | self.audit_json = audit_json |
178 | 180 | self.export_inactive_items = export_inactive_items |
179 | 181 | self.item_category = EMPTY_RESPONSE |
180 | 182 | self.item_map = {} |
181 | 183 | self.map_items() |
182 | 184 | self.audit_table = self.convert_audit_to_table() |
183 | 185 |
|
| 186 | + def configure_logging(self): |
| 187 | + """ |
| 188 | + Configure logging to log to std output as well as to log file |
| 189 | + """ |
| 190 | + log_level = logging.DEBUG |
| 191 | + |
| 192 | + log_filename = datetime.now().strftime('%Y-%m-%d') + '.log' |
| 193 | + csvExporter_logger = logging.getLogger('csvExporter_logger') |
| 194 | + csvExporter_logger.setLevel(log_level) |
| 195 | + formatter = logging.Formatter('%(asctime)s : %(levelname)s : %(message)s') |
| 196 | + |
| 197 | + fh = logging.FileHandler(filename=os.getcwd() + log_filename) |
| 198 | + fh.setLevel(log_level) |
| 199 | + fh.setFormatter(formatter) |
| 200 | + csvExporter_logger.addHandler(fh) |
| 201 | + |
| 202 | + sh = logging.StreamHandler(sys.stdout) |
| 203 | + sh.setLevel(log_level) |
| 204 | + sh.setFormatter(formatter) |
| 205 | + csvExporter_logger.addHandler(sh) |
| 206 | + |
184 | 207 | def audit_id(self): |
185 | 208 | """ |
186 | 209 | :return: The audit ID |
@@ -353,20 +376,22 @@ def write_file(self, output_csv_path, mode): |
353 | 376 | :param output_csv_path: the full path to file to save |
354 | 377 | :param mode: write ('wb') or append ('ab') mode |
355 | 378 | """ |
| 379 | + csvExporter_logger = logging.getLogger('csvExporter_logger') |
356 | 380 | try: |
357 | 381 | csv_file = open(output_csv_path, mode) |
358 | 382 | wr = csv.writer(csv_file, dialect='excel', quoting=csv.QUOTE_ALL) |
359 | 383 | wr.writerows(self.audit_table) |
360 | 384 | csv_file.close() |
361 | | - except Exception as ex: |
362 | | - print(str(ex) + ': Error saving audit_table to ' + output_csv_path) |
| 385 | + except Exception: |
| 386 | + csvExporter_logger.exception('Error saving audit_table to ' + output_csv_path) |
363 | 387 |
|
364 | 388 | def get_item_response(self, item): |
365 | 389 | """ |
366 | 390 | Return item response value |
367 | 391 | :param item: single item in JSON format |
368 | 392 | :return: response property |
369 | 393 | """ |
| 394 | + csvExporter_logger = logging.getLogger('csvExporter_logger') |
370 | 395 | response = EMPTY_RESPONSE |
371 | 396 | item_type = get_json_property(item, TYPE) |
372 | 397 | if item_type == 'question': |
@@ -407,8 +432,13 @@ def get_item_response(self, item): |
407 | 432 | INFORMATION]: |
408 | 433 | pass |
409 | 434 | else: |
410 | | - print('Unhandled item type: ' + str(item_type) + ' from ' + |
| 435 | + # No item type might mean malformed item object, catch and log error accessing item |
| 436 | + try: |
| 437 | + csvExporter_logger.error('Unhandled item type: ' + str(item_type) + ' from ' + |
411 | 438 | self.audit_id() + ', ' + item.get(ID)) |
| 439 | + except Exception: |
| 440 | + csvExporter_logger.exception('Error parsing item, item likely malformed') |
| 441 | + |
412 | 442 | return response |
413 | 443 |
|
414 | 444 | @staticmethod |
|
0 commit comments