diff --git a/tuf/log.py b/tuf/log.py index 0667e4ad..77487e36 100755 --- a/tuf/log.py +++ b/tuf/log.py @@ -73,6 +73,8 @@ '[%(funcName)s:%(lineno)s@%(filename)s] %(message)s' +# Ask all Formatter instances to talk GMT. +# http://docs.python.org/2/library/logging.html#logging.Formatter.formatException logging.Formatter.converter = time.gmtime formatter = logging.Formatter(_FORMAT_STRING) @@ -104,6 +106,51 @@ +class ConsoleFilter(logging.Filter): + def filter(self, record): + """ + + Use Vinay Sajip's recommendation from Python issue #6435 to modify a + LogRecord object. This is meant to be used with our console handler. + + http://stackoverflow.com/q/6177520 + http://stackoverflow.com/q/5875225 + http://bugs.python.org/issue6435 + http://docs.python.org/2/howto/logging-cookbook.html#filters-contextual + http://docs.python.org/2/library/logging.html#logrecord-attributes + + + record: + A logging.LogRecord object. + + + None. + + + Replaces the LogRecord exception text attribute. + + + True. + + """ + + # If this LogRecord object has an exception, then we will replace its text. + if record.exc_info: + # We place the record's cached exception text (which usually contains the + # exception traceback) with much simpler exception information. This is + # most useful for the console handler, which we do not wish to deluge + # with too much data. Assuming that this filter is not applied to the + # file logging handler, the user may always consult the file log for the + # original exception traceback. + record.exc_text = str(record.exc_info) + + # Always return True to signal that any given record must be formatted. + return True + + + + + def set_log_level(log_level=_DEFAULT_LOG_LEVEL): """ @@ -200,7 +247,6 @@ def set_console_log_level(log_level=_DEFAULT_CONSOLE_LOG_LEVEL): - def add_console_handler(log_level=_DEFAULT_CONSOLE_LOG_LEVEL): """ @@ -222,15 +268,25 @@ def add_console_handler(log_level=_DEFAULT_CONSOLE_LOG_LEVEL): None. """ - + # Assign to the global console_handler object. + global console_handler + # Does 'log_level' have the correct format? # Raise 'tuf.FormatError' if there is a mismatch. tuf.formats.LENGTH_SCHEMA.check_match(log_level) # Set the console handler for the logger. The built-in console handler will # log messages to 'sys.stderr' and capture 'log_level' messages. - global console_handler console_handler = logging.StreamHandler() + # Get our filter for the console handler. + console_filter = ConsoleFilter() + console_handler.setLevel(log_level) console_handler.setFormatter(formatter) + console_handler.addFilter(console_filter) logger.addHandler(console_handler) + + + + +