From f53efb2e70993053d795e802d6e2830d10efca33 Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Fri, 15 Nov 2024 12:50:32 +0200 Subject: [PATCH 1/2] Fix logger names for Listener and ListenerContainer --- posttroll/listener.py | 4 ++-- posttroll/tests/test_pubsub.py | 37 ++++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/posttroll/listener.py b/posttroll/listener.py index e89c486..704f058 100644 --- a/posttroll/listener.py +++ b/posttroll/listener.py @@ -34,7 +34,7 @@ class ListenerContainer: """Container for a listener instance.""" - logger = logging.getLogger("ListenerContainer") + logger = logging.getLogger(__name__ + ".ListenerContainer") def __init__(self, topics=None, addresses=None, nameserver="localhost", services=""): """Initialize the class.""" @@ -82,7 +82,7 @@ def stop(self): class Listener: """PyTroll listener class for reading messages for eg. operational product generation.""" - logger = logging.getLogger("Listener") + logger = logging.getLogger(__name__ + ".Listener") def __init__(self, topics=None, queue=None, addresses=None, nameserver="localhost", services=""): diff --git a/posttroll/tests/test_pubsub.py b/posttroll/tests/test_pubsub.py index c152bf8..38f595f 100644 --- a/posttroll/tests/test_pubsub.py +++ b/posttroll/tests/test_pubsub.py @@ -23,6 +23,7 @@ """Test the publishing and subscribing facilities.""" +import logging import time import unittest from contextlib import contextmanager @@ -198,6 +199,11 @@ def _get_port_from_publish_instance(min_port=None, max_port=None): class TestListenerContainerNoNameserver(unittest.TestCase): """Testing listener container without nameserver.""" + @pytest.fixture(autouse=True) + def inject_fixtures(self, caplog): + """Inject fixtures.""" + self._caplog = caplog + def setUp(self): """Set up the testing class.""" test_lock.acquire() @@ -215,20 +221,25 @@ def test_listener_container(self): pub = Publisher(pub_addr, name="test") pub.start() time.sleep(2) - sub = ListenerContainer(topics=["/counter"], nameserver=False, addresses=[pub_addr]) - time.sleep(2) - for counter in range(5): - tested = False - msg_out = Message("/counter", "info", str(counter)) - pub.send(str(msg_out)) + with self._caplog.at_level(logging.DEBUG): + sub = ListenerContainer(topics=["/counter"], nameserver=False, addresses=[pub_addr]) + time.sleep(2) + for counter in range(5): + tested = False + msg_out = Message("/counter", "info", str(counter)) + pub.send(str(msg_out)) + + msg_in = sub.output_queue.get(True, 1) + if msg_in is not None: + assert str(msg_in) == str(msg_out) + tested = True + assert tested + pub.stop() + sub.stop() - msg_in = sub.output_queue.get(True, 1) - if msg_in is not None: - assert str(msg_in) == str(msg_out) - tested = True - assert tested - pub.stop() - sub.stop() + assert "posttroll.listener.ListenerContainer" in self._caplog.text + assert sub.logger.name == "posttroll.listener.ListenerContainer" + assert sub.listener.logger.name == "posttroll.listener.Listener" # Test create_publisher_from_config From efa3e36aa603dfe909dbf464b4b8458f0d850307 Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Fri, 15 Nov 2024 12:51:59 +0200 Subject: [PATCH 2/2] Rename LOGGER to logger --- posttroll/address_receiver.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/posttroll/address_receiver.py b/posttroll/address_receiver.py index 42c0d4c..689410b 100644 --- a/posttroll/address_receiver.py +++ b/posttroll/address_receiver.py @@ -44,7 +44,7 @@ __all__ = ("AddressReceiver", "getaddress") -LOGGER = logging.getLogger(__name__) +logger = logging.getLogger(__name__) debug = os.environ.get("DEBUG", False) @@ -123,7 +123,7 @@ def get(self, name=""): mda = copy.copy(metadata) mda["receive_time"] = mda["receive_time"].isoformat() addrs.append(mda) - LOGGER.debug("return address %s", str(addrs)) + logger.debug("return address %s", str(addrs)) return addrs def _check_age(self, pub, min_interval=zero_seconds): @@ -132,7 +132,7 @@ def _check_age(self, pub, min_interval=zero_seconds): if (now - self._last_age_check) <= min_interval: return - LOGGER.debug("%s - checking addresses", str(dt.datetime.utcnow())) + logger.debug("%s - checking addresses", str(dt.datetime.utcnow())) self._last_age_check = now to_del = [] with self._address_lock: @@ -144,7 +144,7 @@ def _check_age(self, pub, min_interval=zero_seconds): "service": metadata["service"]} msg = Message("/address/" + metadata["name"], "info", mda) to_del.append(addr) - LOGGER.info(f"publish remove '{msg}'") + logger.info(f"publish remove '{msg}'") pub.send(str(msg.encode())) for addr in to_del: del self._addresses[addr] @@ -164,7 +164,7 @@ def _run(self): except TimeoutError: if self._do_run: if self._multicast_enabled: - LOGGER.debug("Multicast socket timed out on recv!") + logger.debug("Multicast socket timed out on recv!") continue else: raise @@ -178,9 +178,9 @@ def _run(self): ip_, port = fromaddr if self._restrict_to_localhost and ip_ not in self._local_ips: # discard external message - LOGGER.debug("Discard external message") + logger.debug("Discard external message") continue - LOGGER.debug("data %s", data) + logger.debug("data %s", data) msg = Message.decode(data) name = msg.subject.split("/")[1] if msg.type == "info" and msg.subject.lower().startswith(self._subject): @@ -189,10 +189,10 @@ def _run(self): metadata = copy.copy(msg.data) metadata["name"] = name - LOGGER.debug("receiving address %s %s %s", str(addr), + logger.debug("receiving address %s %s %s", str(addr), str(name), str(metadata)) if addr not in self._addresses: - LOGGER.info("nameserver: publish add '%s'", + logger.info("nameserver: publish add '%s'", str(msg)) pub.send(msg.encode()) self._add(addr, metadata) @@ -209,7 +209,7 @@ def set_up_address_receiver(self, port): recv = MulticastReceiver(port) except IOError as err: if err.errno == errno.ENODEV: - LOGGER.error("Receiver initialization failed " + logger.error("Receiver initialization failed " "(no such device). " "Trying again in %d s", 10) @@ -218,7 +218,7 @@ def set_up_address_receiver(self, port): raise else: recv.settimeout(tout=2.0) - LOGGER.info("Receiver initialized.") + logger.info("Receiver initialized.") break else: