commit 4a5586afb64d50c627652c1ead9314596cb0b36c Author: Damian Johnson atagar@torproject.org Date: Sun Jan 1 10:24:38 2012 -0800
Deduplicating unknown auth type messages
There's quite a few log messages that really don't make sense to repeatedly log. Adding optinal logging deduplication and applying it to the INFO level message about getting an auth method we don't recognize. --- stem/connection.py | 3 ++- stem/util/log.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletions(-)
diff --git a/stem/connection.py b/stem/connection.py index 6bd50fd..37578cf 100644 --- a/stem/connection.py +++ b/stem/connection.py @@ -794,7 +794,8 @@ class ProtocolInfoResponse(stem.socket.ControlMessage): auth_methods.append(AuthMethod.COOKIE) else: unknown_auth_methods.append(method) - log.info("PROTOCOLINFO response had an unrecognized authentication method: %s" % method) + message_id = "stem.connection.unknown_auth_%s" % method + log.log_once(message_id, log.INFO, "PROTOCOLINFO response had an unrecognized authentication method: %s" % method)
# our auth_methods should have a single AuthMethod.UNKNOWN entry if # any unknown authentication methods exist diff --git a/stem/util/log.py b/stem/util/log.py index f9efa09..de84ead 100644 --- a/stem/util/log.py +++ b/stem/util/log.py @@ -37,6 +37,10 @@ LOG_VALUES = {
LOGGER = logging.getLogger("stem")
+# There's some messages that we don't want to log more than once. This set has +# the messages IDs that we've logged which fall into this category. +DEDUPLICATION_MESSAGE_IDS = set() + # Adds a default nullhandler for the stem logger, suppressing the 'No handlers # could be found for logger "stem"' warning as per... # http://docs.python.org/release/3.1.3/library/logging.html#configuring-loggin... @@ -81,6 +85,27 @@ def log(runlevel, message): if runlevel: LOGGER.log(LOG_VALUES[runlevel], message)
+def log_once(message_id, runlevel, message): + """ + Logs a message at the given runlevel. If a message with this ID has already + been logged then this is a no-op. + + Arguments: + message_id (str) - unique message identifier to deduplicate on + runlevel (Runlevel) - runlevel to log the message at, logging is skipped if + None + message (str) - message to be logged + + Returns: + True if we log the message, False otherwise + """ + + if not runlevel or message_id in DEDUPLICATION_MESSAGE_IDS: + return False + else: + DEDUPLICATION_MESSAGE_IDS.add(message_id) + log(runlevel, message) + # shorter aliases for logging at a runlevel def trace(message): log(Runlevel.TRACE, message) def debug(message): log(Runlevel.DEBUG, message)
tor-commits@lists.torproject.org