log.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2011-2016 Guillaume Ayoub
  3. #
  4. # This library is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. Radicale logging module.
  18. Manage logging from a configuration file. For more information, see:
  19. http://docs.python.org/library/logging.config.html
  20. """
  21. import os
  22. import sys
  23. import logging
  24. import logging.config
  25. import signal
  26. from . import config
  27. LOGGER = logging.getLogger()
  28. def configure_from_file(filename, debug):
  29. logging.config.fileConfig(filename)
  30. if debug:
  31. LOGGER.setLevel(logging.DEBUG)
  32. for handler in LOGGER.handlers:
  33. handler.setLevel(logging.DEBUG)
  34. def start():
  35. """Start the logging according to the configuration."""
  36. filename = os.path.expanduser(config.get("logging", "config"))
  37. debug = config.getboolean("logging", "debug")
  38. if os.path.exists(filename):
  39. # Configuration taken from file
  40. configure_from_file(filename, debug)
  41. # Reload config on SIGHUP (UNIX only)
  42. if hasattr(signal, 'SIGHUP'):
  43. def handler(signum, frame):
  44. configure_from_file(filename, debug)
  45. signal.signal(signal.SIGHUP, handler)
  46. else:
  47. # Default configuration, standard output
  48. handler = logging.StreamHandler(sys.stdout)
  49. handler.setFormatter(logging.Formatter("%(message)s"))
  50. LOGGER.addHandler(handler)
  51. if debug:
  52. LOGGER.setLevel(logging.DEBUG)
  53. LOGGER.debug(
  54. "Logging configuration file '%s' not found, using stdout." %
  55. filename)