Issue #731 resolved

Custom log handlers

Anonymous created an issue

The current logging implementation has possibilities for improvements.

Changing the default log handler is ticky, because you have to remove the already installed handlers manually after the initialisation of the logfiles. That's the missing point in the example ([wiki:Logging]).

The attached patch adds three class attributes to change the default handler. {{{ --- _cplogging.py.orig 2007-09-17 14:07:29.000000000 +0200 +++ _cplogging.py 2007-09-17 15:10:38.000000000 +0200 @@ -16,6 +16,9 @@ appid = None error_log = None access_log = None + file_handler_class = logging.FileHandler + file_handler_args = [] + file_handler_kwargs = {}

 def __init__(self, appid=None, logger_root="cherrypy"):
     self.logger_root = logger_root

@@ -112,7 +115,8 @@ # -------------------------- File handlers -------------------------- #

 def _add_builtin_file_handler(self, log, fname):
  • h = logging.FileHandler(fname)
  • h = self.file_handler_class(fname,
  • self.file_handler_args, *self.file_handler_kwargs) h.setLevel(logging.DEBUG) h.setFormatter(logfmt) h._cpbuiltin = "file" }}}

This patch doesn't solve the whole problem (unfortunately). You have to change the handlers before the configuration update, because the updates will be done in a random sequence.

'''Example:''' {{{

import logging.handlers cherrypy.log.file_handler_class = logging.handlers.RotatingFileHandler cherrypy.log.file_handler_kwargs = {'maxBytes': 100000, 'backupCount': 5} cherrypy.config.update(CONFIG_DICT) }}}

What's your opinion about this changes?

Reported by Carsten Grohmann <carstengrohmann.gmx.de>

Comments (2)

  1. Robert Brewer

    IMO _add_builtin_file_handler etc are only there for backward compatibility with log.screen etc, and shouldn't be extended to handle even more cases. I've updated wiki:Logging#CustomHandlers to minimize confusion in the future.

  2. Log in to comment