[tor-commits] [stem/master] Drop LogBuffer class
atagar at torproject.org
atagar at torproject.org
Mon Feb 10 03:14:50 UTC 2020
commit 76b6b49fd8164d858b3b081e6b6a7aa8cdf315cb
Author: Damian Johnson <atagar at torproject.org>
Date: Sat Feb 1 14:59:33 2020 -0800
Drop LogBuffer class
Deprecated class that was only used within our tests. Python 3's logging module
has better handlers for us to use.
---
stem/util/log.py | 31 -------------------------------
test/unit/response/events.py | 19 +++++++++++--------
test/unit/util/log.py | 13 +++++++++++--
3 files changed, 22 insertions(+), 41 deletions(-)
diff --git a/stem/util/log.py b/stem/util/log.py
index 4ef977b1..3093396f 100644
--- a/stem/util/log.py
+++ b/stem/util/log.py
@@ -218,37 +218,6 @@ def error(message):
log(Runlevel.ERROR, 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.
-
- .. versionchanged:: 1.4.0
- Added the yield_records argument.
-
- .. deprecated:: 1.8.0
- This will be dropped in Stem 2.x. Use python's logging.BufferingHandler instead.
- """
-
- def __init__(self, runlevel, yield_records = False):
- super(LogBuffer, self).__init__(level = logging_level(runlevel))
-
- self.formatter = FORMATTER
- self._buffer = []
- self._yield_records = yield_records
-
- def is_empty(self):
- return not bool(self._buffer)
-
- def __iter__(self):
- while self._buffer:
- record = self._buffer.pop(0)
- yield record if self._yield_records else self.formatter.format(record)
-
- def emit(self, record):
- self._buffer.append(record)
-
-
class _StdoutLogger(logging.Handler):
def __init__(self, runlevel):
logging.Handler.__init__(self, level = logging_level(runlevel))
diff --git a/test/unit/response/events.py b/test/unit/response/events.py
index 27862054..8e2512bd 100644
--- a/test/unit/response/events.py
+++ b/test/unit/response/events.py
@@ -3,6 +3,8 @@ Unit tests for the stem.response.events classes.
"""
import datetime
+import logging.handlers
+import queue
import threading
import unittest
@@ -1642,21 +1644,22 @@ class TestEvents(unittest.TestCase):
"""
stem_logger = stem.util.log.get_logger()
- logging_buffer = stem.util.log.LogBuffer(stem.util.log.INFO)
- stem_logger.addHandler(logging_buffer)
+
+ messages = queue.Queue()
+ handler = logging.handlers.QueueHandler(messages)
+ handler.setLevel(stem.util.log.INFO)
+ stem_logger.addHandler(handler)
# Try parsing a valid event. We shouldn't log anything.
_get_event(STATUS_GENERAL_CONSENSUS_ARRIVED)
- self.assertTrue(logging_buffer.is_empty())
- self.assertEqual([], list(logging_buffer))
+ self.assertTrue(messages.empty())
# Parse an invalid runlevel.
_get_event(STATUS_GENERAL_CONSENSUS_ARRIVED.replace('NOTICE', 'OMEGA_CRITICAL!!!'))
- logged_events = list(logging_buffer)
- self.assertEqual(1, len(logged_events))
- self.assertTrue('STATUS_GENERAL event had an unrecognized runlevel' in logged_events[0])
+ self.assertEqual(1, messages.qsize())
+ self.assertTrue('STATUS_GENERAL event had an unrecognized runlevel' in str(messages.get_nowait()))
- stem_logger.removeHandler(logging_buffer)
+ stem_logger.removeHandler(handler)
diff --git a/test/unit/util/log.py b/test/unit/util/log.py
index 615d8d36..7b5f1209 100644
--- a/test/unit/util/log.py
+++ b/test/unit/util/log.py
@@ -2,6 +2,7 @@
Unit tests for the stem.util.log functions.
"""
+import logging
import unittest
from stem.util import log
@@ -15,9 +16,17 @@ class TestLog(unittest.TestCase):
try:
self.assertFalse(log.is_tracing())
- logger.addHandler(log.LogBuffer(log.DEBUG))
+
+ handler = logging.NullHandler()
+ handler.setLevel(log.DEBUG)
+ logger.addHandler(handler)
+
self.assertFalse(log.is_tracing())
- logger.addHandler(log.LogBuffer(log.TRACE))
+
+ handler = logging.NullHandler()
+ handler.setLevel(log.TRACE)
+ logger.addHandler(handler)
+
self.assertTrue(log.is_tracing())
finally:
logger.handlers = original_handlers
More information about the tor-commits
mailing list