2011-04-10 18:17:06 +02:00
|
|
|
# This file is part of Radicale Server - Calendar Server
|
2017-05-27 17:28:07 +02:00
|
|
|
# Copyright © 2011-2017 Guillaume Ayoub
|
2011-04-10 18:17:06 +02:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Radicale logging module.
|
|
|
|
|
|
|
|
Manage logging from a configuration file. For more information, see:
|
|
|
|
http://docs.python.org/library/logging.config.html
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import logging.config
|
2014-07-28 12:07:55 -07:00
|
|
|
import signal
|
2016-07-04 14:32:33 +02:00
|
|
|
import sys
|
2011-04-10 18:17:06 +02:00
|
|
|
|
|
|
|
|
2016-08-25 04:29:02 +02:00
|
|
|
def configure_from_file(logger, filename, debug):
|
2016-08-25 05:03:22 +02:00
|
|
|
logging.config.fileConfig(filename, disable_existing_loggers=False)
|
2014-07-28 12:07:55 -07:00
|
|
|
if debug:
|
2016-04-22 11:37:02 +09:00
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
for handler in logger.handlers:
|
2014-07-28 12:07:55 -07:00
|
|
|
handler.setLevel(logging.DEBUG)
|
2016-04-22 11:37:02 +09:00
|
|
|
return logger
|
2014-07-28 12:07:55 -07:00
|
|
|
|
|
|
|
|
2017-05-31 11:08:32 +02:00
|
|
|
class RemoveTracebackFilter(logging.Filter):
|
|
|
|
def filter(self, record):
|
|
|
|
record.exc_info = None
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2016-04-22 11:37:02 +09:00
|
|
|
def start(name="radicale", filename=None, debug=False):
|
2013-07-12 15:25:57 +02:00
|
|
|
"""Start the logging according to the configuration."""
|
2016-04-22 11:37:02 +09:00
|
|
|
logger = logging.getLogger(name)
|
2017-06-02 12:43:03 +02:00
|
|
|
if debug:
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
else:
|
|
|
|
logger.addFilter(RemoveTracebackFilter())
|
|
|
|
if filename:
|
2011-04-10 18:17:06 +02:00
|
|
|
# Configuration taken from file
|
2017-06-02 12:43:03 +02:00
|
|
|
try:
|
|
|
|
configure_from_file(logger, filename, debug)
|
|
|
|
except Exception as e:
|
|
|
|
raise RuntimeError("Failed to load logging configuration file %r: "
|
|
|
|
"%s" % (filename, e)) from e
|
2014-11-03 11:21:12 +01:00
|
|
|
# Reload config on SIGHUP (UNIX only)
|
2016-08-01 12:50:51 +02:00
|
|
|
if hasattr(signal, "SIGHUP"):
|
2016-08-25 05:02:31 +02:00
|
|
|
def handler(signum, frame):
|
2017-06-02 12:43:03 +02:00
|
|
|
try:
|
|
|
|
configure_from_file(logger, filename, debug)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error("Failed to reload logging configuration file "
|
|
|
|
"%r: %s", filename, e, exc_info=True)
|
2014-11-03 11:21:12 +01:00
|
|
|
signal.signal(signal.SIGHUP, handler)
|
2011-04-10 18:17:06 +02:00
|
|
|
else:
|
|
|
|
# Default configuration, standard output
|
2016-08-25 04:29:39 +02:00
|
|
|
handler = logging.StreamHandler(sys.stderr)
|
2016-09-03 10:19:29 +02:00
|
|
|
handler.setFormatter(
|
|
|
|
logging.Formatter("[%(thread)x] %(levelname)s: %(message)s"))
|
2016-04-22 11:37:02 +09:00
|
|
|
logger.addHandler(handler)
|
|
|
|
return logger
|