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