log.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import logging
  2. import os
  3. from logging import handlers
  4. class MyLogging():
  5. # init logging
  6. def __init__(self):
  7. # the file of log
  8. logPath = 'logs/'
  9. if not os.path.exists(logPath):
  10. os.makedirs(logPath)
  11. logName = 'app_manage.log'
  12. logFile = logPath + logName
  13. formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
  14. # handler
  15. time_rotating_file_handler = handlers.TimedRotatingFileHandler(filename=logFile, when="MIDNIGHT", interval=1, encoding='utf-8')
  16. time_rotating_file_handler.setLevel(logging.DEBUG)
  17. time_rotating_file_handler.setFormatter(formatter)
  18. # config
  19. logging.basicConfig(
  20. level= logging.DEBUG,
  21. handlers= [time_rotating_file_handler],
  22. datefmt='%Y-%m-%d %H:%M:%S',
  23. format='%(asctime)s %(levelname)s: %(message)s'
  24. )
  25. def info_logger(self, content):
  26. logging.info(content)
  27. def error_logger(self, content):
  28. logging.error(content)
  29. def debug_logger(self, content):
  30. logging.debug(content)
  31. def warning_logger(self, content):
  32. logging.warning(content)
  33. myLogger = MyLogging()