log.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2011-2013 Guillaume Ayoub
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library 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 Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Radicale logging module.
  20. Manage logging from a configuration file. For more information, see:
  21. http://docs.python.org/library/logging.config.html
  22. """
  23. import os
  24. import sys
  25. import logging
  26. import logging.config
  27. import signal
  28. from . import config
  29. LOGGER = logging.getLogger()
  30. def configure_from_file(filename, debug):
  31. logging.config.fileConfig(filename)
  32. if debug:
  33. LOGGER.setLevel(logging.DEBUG)
  34. for handler in LOGGER.handlers:
  35. handler.setLevel(logging.DEBUG)
  36. def start():
  37. """Start the logging according to the configuration."""
  38. filename = os.path.expanduser(config.get("logging", "config"))
  39. debug = config.getboolean("logging", "debug")
  40. if os.path.exists(filename):
  41. # Configuration taken from file
  42. configure_from_file(filename, debug)
  43. # Reload config on SIGHUP (UNIX only)
  44. if hasattr(signal, 'SIGHUP'):
  45. def handler(signum, frame):
  46. configure_from_file(filename, debug)
  47. signal.signal(signal.SIGHUP, handler)
  48. else:
  49. # Default configuration, standard output
  50. handler = logging.StreamHandler(sys.stdout)
  51. handler.setFormatter(logging.Formatter("%(message)s"))
  52. LOGGER.addHandler(handler)
  53. if debug:
  54. LOGGER.setLevel(logging.DEBUG)
  55. LOGGER.debug(
  56. "Logging configuration file '%s' not found, using stdout." %
  57. filename)