log.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://swi.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import core.api
  20. import logging
  21. class Plugin(core.api.Plugin, core.api.IConfigurable):
  22. def declare_configuration(self, parser):
  23. parser.add_option("--general.log-level", default=r'INFO', choices=['DEBUG','INFO','WARNING','ERROR'],
  24. help="Defines log level. Possible values are 'DEBUG','INFO','WARNING' or 'ERROR' [default: %default]")
  25. def configure(self, options):
  26. if options.__dict__['general.log_level'] == 'ERROR':
  27. log_level = logging.ERROR
  28. elif options.__dict__['general.log_level'] == 'WARNING':
  29. log_level = logging.WARNING
  30. elif options.__dict__['general.log_level'] == 'INFO':
  31. log_level = logging.INFO
  32. elif options.__dict__['general.log_level'] == 'DEBUG':
  33. log_level = logging.DEBUG
  34. else:
  35. raise AssertionError("Unhandled choice of log level")
  36. logging.getLogger().setLevel(log_level)
  37. logging.warn("Logging enabled with " + options.__dict__['general.log_level'] + " level")
  38. def set_default_format():
  39. logging.basicConfig(format="[LOG]: %(levelname)s:\t%(message)s", level=logging.WARN)