verbosity.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import logging
  2. import borgmatic.logger
  3. VERBOSITY_DISABLED = -2
  4. VERBOSITY_ERROR = -1
  5. VERBOSITY_ANSWER = 0
  6. VERBOSITY_SOME = 1
  7. VERBOSITY_LOTS = 2
  8. def verbosity_to_log_level(verbosity):
  9. '''
  10. Given a borgmatic verbosity value, return the corresponding Python log level.
  11. '''
  12. borgmatic.logger.add_custom_log_levels()
  13. return {
  14. VERBOSITY_DISABLED: logging.DISABLED,
  15. VERBOSITY_ERROR: logging.ERROR,
  16. VERBOSITY_ANSWER: logging.ANSWER,
  17. VERBOSITY_SOME: logging.INFO,
  18. VERBOSITY_LOTS: logging.DEBUG,
  19. }.get(verbosity, logging.WARNING)
  20. DEFAULT_VERBOSITIES = {
  21. 'verbosity': 0,
  22. 'syslog_verbosity': -2,
  23. 'log_file_verbosity': 1,
  24. 'monitoring_verbosity': 1,
  25. }
  26. def get_verbosity(configs, option_name):
  27. '''
  28. Given a dict from configuration filename to configuration dict, and the name of a configuration
  29. verbosity option, return the maximum verbosity value from that option across the given
  30. configuration files.
  31. '''
  32. try:
  33. return max(
  34. verbosity
  35. for config in configs.values()
  36. for verbosity in (config.get(option_name, DEFAULT_VERBOSITIES[option_name]),)
  37. if verbosity is not None
  38. )
  39. except ValueError:
  40. return DEFAULT_VERBOSITIES[option_name]