logger.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from datetime import datetime
  2. import transport
  3. import copy
  4. import json
  5. import pandas as pd
  6. class X12Logger :
  7. def __init__(self,**_args) :
  8. self._store = copy.deepcopy(_args['store'])
  9. self._store['table'] = 'logs'
  10. self._store['context'] = 'write'
  11. def log(self,**_args):
  12. _date = datetime.now()
  13. _info = {'date':'-'.join([str(_date.month),str(_date.day),str(_date.year)])}
  14. for key in ['module','action','data'] :
  15. value = 'NA' if key not in _args else _args[key]
  16. value = value if type(value) not in [dict,list] else json.dumps(value)
  17. _info[key] = value
  18. # print ([key, type(value) in [dict,list], type(value)])
  19. #
  20. # Storing the whole thing
  21. try:
  22. _xwriter = transport.factory.instance(**self._store)
  23. # if not _xwriter.has(table=self._store['table']) :
  24. # table = self._store['table']
  25. # sql = f'''CREATE TABLE {table} (date date,module char(255), data JSON)'''
  26. # print (sql)
  27. # _xwriter.apply(sql)
  28. _df = pd.DataFrame([_info])
  29. _xwriter.write(_df)
  30. if hasattr(_xwriter,'close') :
  31. _xwriter.close()
  32. except Exception as e:
  33. print ([e])
  34. pass