Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.

Commit 6c70b86

Browse files
authored
[CC-35] Add logger to cvsExporter (#64)
* [CC-32] Update minor version to 4.1.0 * [CC-35] Add logger and cover known error/exceptions * [CC-35] clean up un-needed Exception reassignment * [CC-35] Updated patch version to 4.1.1 * [CC-35] remove testing log write
1 parent 6bde3de commit 6c70b86

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup
22

33
setup(name = 'safetyculture-sdk-python',
4-
version = '4.0.0',
4+
version = '4.1.1',
55
description = 'iAuditor Python SDK and integration tools',
66
url = 'https://github.com/SafetyCulture/safetyculture-sdk-python',
77
author = 'SafetyCulture',

tools/exporter/csvExporter.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unicodecsv as csv
22
import json
3+
import logging
34
import sys
45
import os
56
import copy
@@ -174,13 +175,35 @@ def __init__(self, audit_json, export_inactive_items=True):
174175
175176
:param audit_json: audit in JSON format to be converted to CSV
176177
"""
178+
self.configure_logging()
177179
self.audit_json = audit_json
178180
self.export_inactive_items = export_inactive_items
179181
self.item_category = EMPTY_RESPONSE
180182
self.item_map = {}
181183
self.map_items()
182184
self.audit_table = self.convert_audit_to_table()
183185

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+
184207
def audit_id(self):
185208
"""
186209
:return: The audit ID
@@ -353,20 +376,22 @@ def write_file(self, output_csv_path, mode):
353376
:param output_csv_path: the full path to file to save
354377
:param mode: write ('wb') or append ('ab') mode
355378
"""
379+
csvExporter_logger = logging.getLogger('csvExporter_logger')
356380
try:
357381
csv_file = open(output_csv_path, mode)
358382
wr = csv.writer(csv_file, dialect='excel', quoting=csv.QUOTE_ALL)
359383
wr.writerows(self.audit_table)
360384
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)
363387

364388
def get_item_response(self, item):
365389
"""
366390
Return item response value
367391
:param item: single item in JSON format
368392
:return: response property
369393
"""
394+
csvExporter_logger = logging.getLogger('csvExporter_logger')
370395
response = EMPTY_RESPONSE
371396
item_type = get_json_property(item, TYPE)
372397
if item_type == 'question':
@@ -407,8 +432,13 @@ def get_item_response(self, item):
407432
INFORMATION]:
408433
pass
409434
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 ' +
411438
self.audit_id() + ', ' + item.get(ID))
439+
except Exception:
440+
csvExporter_logger.exception('Error parsing item, item likely malformed')
441+
412442
return response
413443

414444
@staticmethod

0 commit comments

Comments
 (0)