log.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. '''
  2. Created on 26/06/2012
  3. @author: konstaa
  4. '''
  5. import core.api
  6. import logging
  7. class Plugin(core.api.Plugin, core.api.IConfigurable):
  8. def declare_configuration(self, parser):
  9. parser.add_option("--general.log-level", default=r'INFO', choices=['DEBUG','INFO','WARNING','ERROR'],
  10. help="Defines log level. Possible values are 'DEBUG','INFO','WARNING' or 'ERROR' [default: %default]")
  11. def configure(self, options):
  12. if options.__dict__['general.log_level'] == 'ERROR':
  13. log_level = logging.ERROR
  14. elif options.__dict__['general.log_level'] == 'WARNING':
  15. log_level = logging.WARNING
  16. elif options.__dict__['general.log_level'] == 'INFO':
  17. log_level = logging.INFO
  18. elif options.__dict__['general.log_level'] == 'DEBUG':
  19. log_level = logging.DEBUG
  20. else:
  21. raise AssertionError("Unhandled choice of log level")
  22. logging.getLogger().setLevel(log_level)
  23. logging.warn("Logging enabled with " + options.__dict__['general.log_level'] + " level")
  24. def set_default_format():
  25. logging.basicConfig(format="[LOG]: %(levelname)s:\t%(message)s", level=logging.WARN)