commit 608a5371370796a3d6ace5deb54bc019783cf2c9 Author: Damian Johnson atagar@torproject.org Date: Thu Jan 5 22:42:33 2012 -0800
Moving log handlers to log util
Adding a couple basic log handlers to the log util... log_to_stdout - sends further events to stdout (mostly intended for debugging at the interpretor) LogBuffer - simple handler used by the testing to buffer logged events so we can run over them later --- run_tests.py | 2 +- stem/util/log.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++------ test/output.py | 26 +------------------ 3 files changed, 68 insertions(+), 35 deletions(-)
diff --git a/run_tests.py b/run_tests.py index 8b59710..1b252df 100755 --- a/run_tests.py +++ b/run_tests.py @@ -208,7 +208,7 @@ if __name__ == '__main__': )
stem_logger = log.get_logger() - logging_buffer = test.output.LogBuffer(log.logging_level(logging_runlevel)) + logging_buffer = log.LogBuffer(logging_runlevel) stem_logger.addHandler(logging_buffer)
if run_unit_tests: diff --git a/stem/util/log.py b/stem/util/log.py index 5c5815e..65d5659 100644 --- a/stem/util/log.py +++ b/stem/util/log.py @@ -8,6 +8,25 @@ runlevels being used as follows... INFO - high level library activity DEBUG - low level library activity TRACE - request/reply logging + +get_logger - provides the stem's Logger instance +logging_level - converts a runlevel to its logging number +escape - escapes special characters in a message in preparation for logging + +log - logs a message at the given runlevel +log_once - logs a message, deduplicating if it has already been logged +trace - logs a message at the TRACE runlevel +debug - logs a message at the DEBUG runlevel +info - logs a message at the INFO runlevel +notice - logs a message at the NOTICE runlevel +warn - logs a message at the WARN runlevel +error - logs a message at the ERROR runlevel + +LogBuffer - Buffers logged events so they can be iterated over. + |- is_empty - checks if there's events in our buffer + +- __iter__ - iterates over and removes the buffered events + +log_to_stdout - reports further logged events to stdout """
import logging @@ -76,6 +95,22 @@ def logging_level(runlevel): if runlevel: return LOG_VALUES[runlevel] else: return logging.FATAL + 5
+def escape(message): + """ + Escapes specific sequences for logging (newlines, tabs, carrage returns). + + Arguments: + message (str) - string to be escaped + + Returns: + str that is escaped + """ + + for pattern, replacement in (("\n", "\n"), ("\r", "\r"), ("\t", "\t")): + message = message.replace(pattern, replacement) + + return message + def log(runlevel, message): """ Logs a message at the given runlevel. @@ -118,19 +153,41 @@ def notice(message): log(Runlevel.NOTICE, message) def warn(message): log(Runlevel.WARN, message) def error(message): log(Runlevel.ERROR, message)
-def escape(message): +class LogBuffer(logging.Handler): + """ + Basic log handler that listens for stem events and stores them so they can be + read later. Log entries are cleared as they are read. """ - Escapes specific sequences for logging (newlines, tabs, carrage returns).
- Arguments: - message (str) - string to be escaped + def __init__(self, runlevel): + logging.Handler.__init__(self, level = logging_level(runlevel)) + self.formatter = logging.Formatter( + fmt = '%(asctime)s [%(levelname)s] %(message)s', + datefmt = '%D %H:%M:%S') + + self._buffer = []
- Returns: - str that is escaped + def is_empty(self): + return not bool(self._buffer) + + def __iter__(self): + while self._buffer: + yield self.formatter.format(self._buffer.pop(0)) + + def emit(self, record): + self._buffer.append(record) + +def log_to_stdout(runlevel): """ + Logs further events to stdout.
- for pattern, replacement in (("\n", "\n"), ("\r", "\r"), ("\t", "\t")): - message = message.replace(pattern, replacement) + Arguments: + runlevel (Runlevel) - minimum runlevel a message needs to be to be logged + """
- return message + logging.basicConfig( + level = logging_level(runlevel), + format = '%(asctime)s [%(levelname)s] %(message)s', + datefmt = '%D %H:%M:%S', + )
diff --git a/test/output.py b/test/output.py index 3ebed9e..6c00d28 100644 --- a/test/output.py +++ b/test/output.py @@ -61,7 +61,7 @@ def apply_filters(testing_output, *filters): if line != None: results.append(line)
- return "\n".join(results) + return "\n".join(results) + "\n"
def colorize(line_type, line_content): """ @@ -134,27 +134,3 @@ class ErrorTracker: for error_line in self._errors: yield error_line
-class LogBuffer(logging.Handler): - """ - Basic log handler that listens for all stem events and stores them so they - can be read later. Log entries are cleared as they are read. - """ - - def __init__(self, runlevel): - logging.Handler.__init__(self, level = runlevel) - self.formatter = logging.Formatter( - fmt = '%(asctime)s [%(levelname)s] %(message)s', - datefmt = '%D %H:%M:%S') - - self._buffer = [] - - def is_empty(self): - return not bool(self._buffer) - - def __iter__(self): - while self._buffer: - yield self.formatter.format(self._buffer.pop(0)) - - def emit(self, record): - self._buffer.append(record) -
tor-commits@lists.torproject.org