main.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from showroom.interfaces import BasicCLI
  2. import logging
  3. # TODO: set log rotation time in config
  4. from datetime import time as dt_time, datetime
  5. from logging.handlers import TimedRotatingFileHandler
  6. from showroom.constants import TOKYO_TZ, HHMM_FMT
  7. import os
  8. def main():
  9. bcli = BasicCLI()
  10. setup_logging(os.path.join(bcli.settings.directory.log, 'showroom.log'))
  11. bcli.start()
  12. bcli.run()
  13. logging.shutdown()
  14. def tokyotime(obj, seconds=None):
  15. if seconds is None:
  16. return datetime.now(tz=TOKYO_TZ).timetuple()
  17. else:
  18. # Does this do what I want it to?
  19. return datetime.fromtimestamp(seconds, tz=TOKYO_TZ).timetuple()
  20. def setup_logging(log_file):
  21. # TODO: more advanced filters, logging info like when rooms go live to console
  22. # https://docs.python.org/3/library/logging.config.html#logging-config-dictschema
  23. log_backup_time = dt_time(tzinfo=TOKYO_TZ)
  24. log_filter = logging.Filter(name="showroom")
  25. file_log_handler = TimedRotatingFileHandler(log_file, encoding='utf8',
  26. when='midnight', atTime=log_backup_time)
  27. file_log_formatter = logging.Formatter(fmt='%(asctime)s %(name)-12s %(levelname)-8s %(threadName)s:\n%(message)s',
  28. datefmt='%m-%d %H:%M:%S')
  29. file_log_handler.setFormatter(file_log_formatter)
  30. # leave this in local time?
  31. file_log_handler.addFilter(log_filter)
  32. file_log_handler.setLevel(logging.DEBUG)
  33. console_handler = logging.StreamHandler()
  34. console_formatter = logging.Formatter(fmt='%(asctime)s %(message)s', datefmt=HHMM_FMT)
  35. console_formatter.converter = tokyotime
  36. console_handler.setLevel(logging.INFO)
  37. console_handler.setFormatter(console_formatter)
  38. console_handler.addFilter(log_filter)
  39. logger = logging.getLogger('showroom')
  40. logger.setLevel(logging.DEBUG)
  41. logger.propagate = False
  42. # at this moment, there shouldn't be any handlers in the showroom logger
  43. # however, i can't preclude the possibility of there ever being such handlers
  44. for handler in (file_log_handler, console_handler):
  45. if handler not in logger.handlers:
  46. logger.addHandler(handler)