I'm writing a project focused on receiving and processing messages from a MQTT broker using paho-mqtt in python. The main project files are:
│ main.py
│ message_processor.py
│ mqtt_project.log
│ subscriber.py
│ utils.py
The problem is that the logger defined within message_processor.py, which is responsible for most of the logging, is not working.
### message_processor.py ###
from utils import setup_logging
class MessageProcessor:
def __init__(self) -> None:
...
self.config = load_config("config.yaml")
self.logger = setup_logging(__name__)
def process_message(self, message) -> list[dict] | None:
topic = message.topic
payload = message.payload.decode()
self.logger.debug("Received message from topic: %s, payload: %s", topic, payload) # This does not log
Despite being defined the exact same way, the logger from subscriber.py works as expected
### subscriber.py ###
from message_processor import MessageProcessor
class MessageSubscriber:
def __init__(self, message_processor:MessageProcessor) -> None:
self.message_processor = message_processor
self.client = self.create_client()
self.logger = setup_logging(__name__)
def on_message(self, client, userdata, message):
self.logger.debug("Received message from topic: %s, payload: %s", message.topic, message.payload) # This logs normally
items = self.message_processor.process_message(message) # This should generate log, but it does not
This is how I run the project in the main.py file:
from utils import load_config
from subscriber import MessageSubscriber
from message_processor import MessageProcessor
if __name__ == '__main__':
config = load_config()
sample_rate_ms = config['sample_rate_ms']
processor = MessageProcessor(sample_rate_ms, 500)
subscriber = MessageSubscriber(processor)
subscriber.connect_client()
subscriber.client.loop_forever(retry_first_connection=True)
Is there a way to make the logger on message_processor.py work?
The logs are generated normally if I call them inside the __init__(self) method. That means that when I instantiate the MessagaProcessor on the main.py file, it works as expected, but when the MessageProcessor methods are called inside the loop_forever(), they don't show up on the log file.