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 dowsn’t let you do this directly but by enabling logging to a a topic and monitoring the topic with an MQTT client you can get a good idea.
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.
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: