Understanding and Configuring Logging – Mosquitto Broker Configuration

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.

mosquitto-verbose-logging

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.

Logging Destination

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)

mosquitto-logging

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/#.

topic-logs

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.

mosquitto-sys-log

You can also use the Paho MQTT Python client see subscribing to topics.

Common Questions and Answers

Q- Are $SYS logs saved to disk?

A- No

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

Other Related Articles and Resources:

Save

Save

Save

Save

Facebooktwittergoogle_plusredditpinterestlinkedinmail

14 comments

  1. Hi Steve,
    Do you happen to know if more admin-level functions are logged as well, such as changing files the broker would use for ACLs or usernames and password changes through mosquitto_passwd?

    1. Patrick
      No there aren’t as far as I know but it could change as I know that it is being upgraded for mqtt v5 and that might contain additional features.

    1. Hi
      need to do more research on that but if you add the following to the conf file you will see lots of info on websockets
      log_type websockets
      websockets_log_level 9

      I need to check the log levels but level 6 seems to give basic info
      rgds
      steve

  2. I have looked everywhere and can’t find anything called the “Mosquitto broker console”.
    What is it?
    Where is it?

    Other than ‘sudo cat mosquitto.log’, is there a better way to see the log file?

    1. When you install mosquitto on linux or windows it is configured to start as a daemon or service. This means that you can’t interact with it.
      However if you start it from a command prompt then you will have a console were you can see the log messages.
      Take a look at the install tutorial and there is a video on youtube here about start mosquitto from the command line
      https://youtu.be/dHH1kSQLhsU
      The log file is probably in the var/log/mosquitto folder and you can open it with a text editor but you will need to change permissions.
      However you can change the folder in the mosquitto.conf file
      Does that help?

  3. Hi Steve,
    I have two problems 🙁
    the first problem is that when I want to log into the file, this error comes:
    1503843648: Error: Unable to open log file log.log /var/log/mosquitto/ for writing.
    I use Ubuntu and permission of log file is 777.
    the second problem is that my configs are set to:
    log_dest topic
    log_type all
    connection_messages true
    and when I subscribe to $SYS/broker/log/# it just sends subscribe logs but I want to receive publish log, connection log,… exactly like stdout.
    thanks.

    1. Hi
      the logfile message is due to permissions. You are probably starting mosquitto from the command line. Use sudo mosquitto -c file
      and it will work.
      It seems publish messages are classed as debug messages which you can confirm by not logging debug messages and then publish messages don’t appear on the console.
      Debug messages aren’t sent to the topic so you will never see them there but you should see connection messages.

      rgds
      steve

  4. dear steve
    your url is refer to this page that we are inside it.
    I read this article,but its not clear for windows at all.
    I could use “log file” only , but “console log” and “topic log” does not work at all.
    please clarify me about this your sentence:
    “Note: On Windows logging is disabled when starting mosquitto as a service”
    what do you mean from the above line?

    also, when I wrote : mosquitto_sub -h ws4 -t $SYS/# -v
    I received this message in cmd : Unable to connect (Lookup error)

    1. Hi Steve,
      Thank you for your informative article.
      I have mosquitto logging for quite some time to a single log file on my cents server.
      What I would like to do is log to a new file each day with a date-stamp as part of the filename.
      Currently my filename is mosquitto.log
      What I would like is YYYYMMDD_mosquitto.log
      Is there a way to do this?
      Regards

      1. Not as far as I now. You specify the name in the conf file and it only accepts a file name. You would need to use a script to read the log file and create and move the log data into the files.
        The log data contains the datetime stamp for each log entry

  5. Hi steve.
    you said :
    “Note: On Windows logging is disabled when starting mosquitto as a service”
    So in the windows we can not enable logging?
    I saw the mosquitto.conf file also and there is not “log_type all” expression!
    How can I enable full logs in the windows?becuase i want use it in the ACL section to see all logs when ACL block any topic…
    thank you

    1. When you start mosquitto as a service it can’t send log messages to the screen as it is running as a background process.
      You can edit the conf file and log to a file.
      To log to the console you need to start mosquitto from the command prompt. The you can set the
      log_type all
      in the conf file.

Leave a Reply

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