app_logging.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from distutils.debug import DEBUG
  2. import logging
  3. from logging.handlers import RotatingFileHandler
  4. global logger_name
  5. logger_name = "OpenISP"
  6. __is_init__ = False
  7. #TODO init function + add category + parameters
  8. def init() :
  9. global __is_init__
  10. if __is_init__ :
  11. return
  12. #create log folder at execution path"
  13. import os
  14. if not os.path.exists('logs'):
  15. os.makedirs('logs')
  16. # Create a custom logger
  17. logger = logging.getLogger(logger_name)
  18. #TODO create Categories here
  19. # of this style : logger = logging.getLogger(logger_name + ".persistence")
  20. # Create handlers
  21. c_handler = logging.StreamHandler()
  22. f_handler = RotatingFileHandler('logs/OpenISP.log',encoding = "UTF-8",backupCount=5,maxBytes=2000000) # 2mo per log
  23. c_handler.setLevel(logging.DEBUG)
  24. f_handler.setLevel(logging.DEBUG)
  25. f_handler.doRollover()
  26. # Create formatters and add it to handlers
  27. c_format = logging.Formatter('[%(asctime)s] - %(name)s - %(levelname)s - %(message)s')
  28. f_format = logging.Formatter('[%(asctime)s] - %(name)s - %(levelname)s - %(message)s')
  29. c_handler.setFormatter(c_format)
  30. f_handler.setFormatter(f_format)
  31. # Add handlers to the logger
  32. logger.addHandler(c_handler)
  33. logger.addHandler(f_handler)
  34. logger.setLevel(logging.DEBUG)
  35. logger.debug("logging engine started")
  36. __is_init__ = True