Simple Python MQTT Topic Logger

mqtt-topic-loggerThe MQTT data logger logs data to all monitored topics to the same log files.

If the data need to be split into topics then it will need to be done by the log analyser software.

However the topics logger logs data based on topic so that each topic has it’s own log file as shown in the diagram below:

MQTT-Topic-Logger File-Folder-Structure

Note: Node-red version here

Topic Logger Class

This class is similar to the mlogger class but automatically creates the folder hierarchy to match the topic hierarchy.

Data is stored in log files starting with log000.txt. When the log reaches the maximum size which is set at 5MB by default then the log file is rotated.

MQTT Data Logger Code Overview

The topic logger uses the tlogger class.

This script will log data on a collections of topics and by default only if changed. It logs

  • Message time
  • Message topic
  • message

Data is logged as a json string. The following lines are added to the on_message callback or a function called by the on_message callback..

    #log.log_json(data) #log in JSON format

You can log the data direct as shown above, but I’ve commented it out.

Instead I prefer to use a separate thread to log the data and so I place the data in a queue using the Queue class.

The message handler function calls the has_changed function to check if the message status is different from the last message.

If it is the same then the message isn’t stored (default mode) as there is no point storing the same message value multiple times.

This can be changed using the -s command line switch.

In addition the message handler function checks if the msg is already JSON encoded.

If it is it converts it back to a Javascript object other wise it will be JSON encoded twice.

The worker takes the data from the queue and logs it to disk using the log_json method, which converts the data to a JSON encoded string before storing.

The relevant code is shown below.

def on_message(client,userdata, msg):
    #print("message received")
def message_handler(client,msg,topic):
        msg=json.loads(msg)#convert to Javascript before saving
        #print("json data")
        #print("not already json")
    if command.options["storechangesonly"]:
        if has_changed(client,topic,msg):
            client.q.put(data) #put messages on queue
        client.q.put(data) #put messages on queue

def has_changed(topic,msg):
    if topic2.find("control")!=-1:
        return False
    if topic in last_message:
        if last_message[topic]==msg:
            return False
    return True
def log_worker():
    """runs in own thread to log data"""
    while Log_worker_flag:
        while not q.empty():
            results = q.get()
            if results is None:
            #print("message saved ",results["message"])

The worker is started at the beginning of the script.

t = threading.Thread(target=log_worker) #start logger
t.start() #start logging thread

The Log_worker_flag is used to stop the worker when the script terminates.

Using the Topic Logger

Note: By default the logger will only log changed data.

For example a door sensor publishing it’s status every 10 seconds will send the same message repeatedly until someone opens or closes it.

You need to provide the script with:

  • List of topics to monitor
  • broker name and port
  • username and password if needed.

The script is run from the command line. Type

python3 -h #Linux
C:\python34\python.exe -h #Windows

for a list of options.

Example Usage:

You will always need to specify the broker name or IP address and the topics to log

Note: You may not need to use the python prefix or may need to use python3 (Linux)

Specify broker and topics

python -b -t sensors/#

Specify broker and multiple topics

python -b -t sensors/# -t home/#

Log All Data:

python b -t sensors/# -s

Specify the client name used by the logger

python b -t sensors/# -n data-logger

Specify the log directory

python b -t sensors/# -l mylogs

Comments and Feedback

Was there enough detail in this tutorial for you to follow?

Please help me improve these tutorials by leaving your comments,rating them,asking questions.

Make a Contribution

pay-pal-buy-nowIf you find this script useful and would like to support its development then please consider making a small contribution by clicking here.


If you are using the logger in your projects then let me know and I can feature them on this page.

Testing Notes

I have tested this logger on a raspberry pi as the data logger with a message stream of 40,000 messages each message approximately 60bytes in length.

When logging changes I achieved a message rate of  76 messages/second.

When logging all messages I achieved a message rate of  50 messages/second.

Related Tutorials and Resources

Please rate? And use Comments to let me know more
[Total: 1    Average: 5/5]


  1. How can I log multiple clients?
    Say I have node1 and node2 sending the same data, but I want to make the log data say “client”:”espxxxxx”

    1. Hi
      You can’t as the logger doesn’t know the client id as that is the way mqtt works. The only place it could be done is on the broker.
      However MQTT v5 has a reply topic feature and others that might make it possible in the future but it would also require the sender to utilise them.

Leave a Reply

Your email address will not be published. Required fields are marked *