from datetime import datetime import transport import copy import json import pandas as pd class X12Logger : def __init__(self,**_args) : self._store = copy.deepcopy(_args['store']) self._store['table'] = 'logs' self._store['context'] = 'write' def log(self,**_args): _date = datetime.now() _info = {'date':'-'.join([str(_date.month),str(_date.day),str(_date.year)])} for key in ['module','action','data'] : value = 'NA' if key not in _args else _args[key] value = value if type(value) not in [dict,list] else json.dumps(value) _info[key] = value # print ([key, type(value) in [dict,list], type(value)]) # # Storing the whole thing try: _xwriter = transport.factory.instance(**self._store) # if not _xwriter.has(table=self._store['table']) : # table = self._store['table'] # sql = f'''CREATE TABLE {table} (date date,module char(255), data JSON)''' # print (sql) # _xwriter.apply(sql) _df = pd.DataFrame([_info]) _xwriter.write(_df) if hasattr(_xwriter,'close') : _xwriter.close() except Exception as e: print ([e]) pass