Mosquitto has two different log types
- System status logs
- Informational and debugging logs
Broker System Status logs
These are always logged to the $SYS topic and can be accessed using an MQTT client subscribed to the $SYS/# topic.
System status logs are always enabled by default, and cannot be disabled, as far as I can tell.
Different brokers will provide different $SYS log topics. This Github page has a list and description of the $SYS log topics split into optional and required.
Information and Debugging logs
These provide detailed information about clients, and client broker commands.
Using them you can see which clients are connected, subscribed etc.
Logging is configured in the Logging section of the mosquitto.conf file.
You can choose which event types to log, by default error, information, notice and warning events are logged.
For debugging I use log_type all.
This is essentially the same as starting mosquitto with the verbose switch – mosquitto -v
Here is a screen shot showing a client connect, subscribe and disconnect sequence and the console log messages that are generated.
Note: On Windows logging is disabled when starting mosquitto as a service. This is because the broker is running as a background process and has no console attached.
You can log messages to multiple destinations.
For example you could send log messages to the console, to the $SYS topic, and to a log file.
The screenshot below shows my mosquitto.conf file configured to log to three destinations. (console, logfile and topic)
You can also disable logging using the log_dest none option.
Viewing Topic logs in a Client
Normally you will view these logs on the Mosquitto broker console but you can also view them using an MQTT client if you have logged them to a topic.
If you send the log messages to topic they are sent to the $SYS/broker/log topic ,and you need to subscribe the topic tree $SYS/broker/log/#.
Two common question are:
- Can I see all connected clients?
- Can I list all topics?
The broker doesn’t let you do this directly but by enabling logging to a topic and monitoring the topic with an MQTT client you can get a good idea.
However using syslog and remote logging you can view connection details. See below.
On Linux systems you can also use syslog to log the messages.
Messages are sent to the syslog file in the /var/log folder.
There are 2 settings
log_dest syslog log_facility 5 #this is what I use
If you use syslog for logging it is possible to send all your logs to a central log server or to a log analyser.
The log_facility it allows you to only send logs from mosquitto.
Coming– Centralised mosquitto logging and analytics using node-red.
On Linux if you log using to the file /var/log/mosquitto.log then the log file will automatically be rotated as an entry is made in the logrotate.d folder by the install script.
The file is shown below:
If you look in the /var/log/mosquitto folder you will see the mosquitto.log file and zipped archives.
The default configuration archives the log file when it exceeds 100k.
Log rotation is handled by cron and runs once per day.
You can manully rotate the mosquitto logs by using the command
sudo logrotate -v /etc/logrotate.d/mosquitto
Viewing The $SYS logs
To view the system status logs you need to subscribe to the $SYS topic using a MQTT client.
The topic path is $SYS/#
You can use the mosquiito_sub client that comes with the broker as shown below.
In the screenshot below ws4 is the broker name.
You can also use the Paho MQTT Python client see subscribing to topics.
If you use node-red I have created a flow that you can download that displays them in browser.
Common Questions and Answers
Q- Are $SYS logs saved to disk?
Q– Why Do I get the message: error: unable to open log file /var/log/mosquitto/mosquitto.log for writing.
A- This is a permission problem and is caused when you start mosquitto manually. You can either start mosquitto as root using sudo or when test change the log file location to your home directory.
Q- How can I list All topics?
A_ Mosquitto doesn’t provide this functionality by default but you can use topic logging as discussed above.
Q- How can I list All connected clients?
A- Use topic logging.
Q- Can I use the $SYS logs for monitoring the broker status?
A- Yes but not probably the best method and some brokers don’t have them enabled. If the broker doesn’t have any other methods built in (mosquitto doesn’t) then you would probably be better using an MQTT client to send background data and monitor that. See this article- why you shouldn’t use the $SYS topic for monitoring
Mosquitto Configuration Tutorials
- Installing and Testing the Mosquitto MQTT broker
- Quick Guide to The Mosquitto.conf File With Examples
- Configuring and Testing MQTT Topic Restrictions
- Configuring Username and Password Authentication
- Configuring TLS (SSL) on Mosquitto
- Understanding and Configuring Bridging on Mosquitto
Other Related Articles and Resources: