log.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://metrixplusplus.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. import os
  22. class Plugin(core.api.Plugin, core.api.IConfigurable):
  23. def declare_configuration(self, parser, default_value='INFO'):
  24. allowed_values = ['DEBUG','INFO','WARNING','ERROR']
  25. default_value_cur = default_value
  26. if os.environ.has_key('METRIXPLUSPLUS_LOG_LEVEL') and os.environ['METRIXPLUSPLUS_LOG_LEVEL'] in allowed_values:
  27. default_value_cur = os.environ['METRIXPLUSPLUS_LOG_LEVEL']
  28. parser.add_option("--general.log-level", default=default_value_cur, choices=allowed_values,
  29. help="Defines log level. Possible values are 'DEBUG','INFO','WARNING' or 'ERROR'. "
  30. "Default value is inherited from environment variable 'METRIXPLUSPLUS_LOG_LEVEL' if set. "
  31. "Otherwise, it is '" + default_value_cur + "' [default: %default]")
  32. def configure(self, options):
  33. if options.__dict__['general.log_level'] == 'ERROR':
  34. log_level = logging.ERROR
  35. elif options.__dict__['general.log_level'] == 'WARNING':
  36. log_level = logging.WARNING
  37. elif options.__dict__['general.log_level'] == 'INFO':
  38. log_level = logging.INFO
  39. elif options.__dict__['general.log_level'] == 'DEBUG':
  40. log_level = logging.DEBUG
  41. else:
  42. raise AssertionError("Unhandled choice of log level")
  43. self.level = log_level
  44. logging.getLogger().setLevel(self.level)
  45. os.environ['METRIXPLUSPLUS_LOG_LEVEL'] = options.__dict__['general.log_level']
  46. logging.warn("Logging enabled with " + options.__dict__['general.log_level'] + " level")
  47. def set_default_format():
  48. logging.basicConfig(format="[LOG]: %(levelname)s:\t%(message)s", level=logging.WARN)