13

I have a previous question and to get closer to a solution I want to enable Mosquitto broker logging on Windows 7.

Originally I have started the broker manually as follows:

mosquitto -p 1883 -v

-v means verbose console logging. But this does not provide enough information, only the following line in case of my problem:

1486293976: Socket error on client <unknown>, disconnecting.

I have tried doing what is described in this answer. Here is the config file's logging part:

# Note that if the broker is running as a Windows service it will default to
# "log_dest none" and neither stdout nor stderr logging is available.
# Use "log_dest none" if you wish to disable logging.
log_dest stdout

# If using syslog logging (not on Windows), messages will be logged to the
# "daemon" facility by default. Use the log_facility option to choose which of
# local0 to local7 to log to instead. The option value should be an integer
# value, e.g. "log_facility 5" to use local5.
#log_facility

# Types of messages to log. Use multiple log_type lines for logging
# multiple types of messages.
# Possible types are: debug, error, warning, notice, information, 
# none, subscribe, unsubscribe, websockets, all.
# Note that debug type messages are for decoding the incoming/outgoing
# network packets. They are not logged in "topics".
log_type error
log_type warning
log_type notice
log_type information

# Change the websockets logging level. This is a global option, it is not
# possible to set per listener. This is an integer that is interpreted by
# libwebsockets as a bit mask for its lws_log_levels enum. See the
# libwebsockets documentation for more details. "log_type websockets" must also
# be enabled.
#websockets_log_level 0

# If set to true, client connection and disconnection messages will be included
# in the log.
connection_messages true

# If set to true, add a timestamp value to each log message.
log_timestamp true

In this case I have started the broker as follows:

mosquitto -p 1883

-v option would override the config file with the default config so I have left that out. But I see no logging on the console.


Instead of stdout I have tried to log into a file, and changed the configuration as follows:

log_dest file d:\mosquitto.txt

I have created the file manually and started the broker in the same way but no avail.


I do not get any log message if I do not use the -v option. How should it be done properly?

3
  • Does log_type debug or log_type all give you any more information?
    – Aurora0001
    Feb 5, 2017 at 12:04
  • 1
    @Aurora0001 No matter what I set in the configuration file, I get no logging output. Only the -v option gives some log entries on the console (cmd) but that uses default config. Feb 5, 2017 at 12:08
  • @JimmyWestberg While your answer gives a convenient logging mechanism, it only logs topic related information while I originally was interested in the mosquitto broker's error/warning logs which I tired to configure in the mosquitto config file. Mar 1, 2017 at 12:18

4 Answers 4

8

This is what I do. Following script saved as timestampLog.vbs:

Dim str
Do While Not WScript.StdIn.AtEndOfStream
  str = WScript.StdIn.ReadLine
  WScript.StdErr.WriteLine "[" & now & "]" & str
Loop

Then I run this from command line:

C:\Program Files\mosquitto>mosquitto_sub -t +/# -v | cscript //nologo timestampLog.vbs 2> C:\*USER*\Desktop\logfile.txt

Where as you might want to change path to your mosquitto folder and change the "logfile.txt" path to whatever you want.

If this is what you are looking for I don't know but someone else might like the solution provided here. To break the command line down:

C:\Program Files\mosquitto> is the local folder for Mosquitto

mosquitto_sub is the .exe file used to listen to the broker

-t +/# "-t" is to give the .exe file the information to listen to topic of +/# which is all topics from clients. "#" would mean all topics, even broker created topics. \$SYS/# will only listen to broker topics. It is possible to have multiple -t (I think).

-v is to give the .exe file the information to output the topics verbose, i.e. also output the topic itself (not only it's value).

| is to pipe this first command into the script command.

cscript //nologo timestampLog.vbs is to execute cscript.exe with the vbs script that we previously wrote. //nologo is telling the command line prompt to output the cscript.exe output information into a external file instead of the prompt itself.

2> is telling the command line prompt to output "StdErr" (which is what we told the script to funnel all messages to in the .vbs file).

C:\*USER*\Desktop\logfile.txt is the path and file name of the output. Instead of .txt you might consider using .log or something else.

0
6

The answer of this question: How to enable WebSockets on Mosquitto running on Windows? actually answered this question as well.

As it turned out the Mosquitto's config file has to be added explicitly in the command line.

When you run mosquitto from the command line you have to explicitly point to the config file with the -c option

mosquitto -v -c /path/to/mosquitto.conf

After this I was able to obtain additional information about socket error events or such.

1489438223: mosquitto version 1.4.10 (build date 24/08/2016 21:03:24.73) starting

1489438223: Config loaded from mosquitto.conf.

1489438223: Opening ipv6 listen socket on port 1883.

1489438223: Error: Only one usage of each socket address (protocol/network address/port) is normally permitted.

1489438341: mosquitto version 1.4.10 (build date 24/08/2016 21:03:24.73) starting

1489438341: Config loaded from mosquitto.conf.

1489438341: Opening ipv6 listen socket on port 1883.

1489438341: Opening ipv4 listen socket on port 1883.

1489438363: New connection from 192.168.1.4 on port 1883.

1489438363: New client connected from 192.168.1.4 as root.1489438369381 (c1, k60, u'phone').

1489438363: Sending CONNACK to root.1489438369381 (0, 0)

1489438363: Received SUBSCRIBE from root.1489438369381

1489438363: room/humidity (QoS 1)

1489438363: root.1489438369381 1 room/humidity

1489438363: Sending > SUBACK to root.1489438369381

1489438453: Client root.1489438369381 has exceeded timeout, disconnecting.

1489438453: Socket error on client root.1489438369381, disconnecting.

4
  1. log payload

    If you want to log the PUBLISH message payload, here I add custome log in mosquitto v1.5.3 souce:

    Source on Git Hub.

    //only show ASCII payload, binary data maybe broke the terminal.

  2. usage in mosquitto.conf:

    log_type payload

  3. result

    log become:

    1542293777: Received PUBLISH from client_20454 (d0, q0, r0, m0, '$rpc/device1/client_20454/25MhY4xUwiMZIuytfb89Vjrh4QU=/req', ... (64 bytes))
    1542293777: > payload: '{"method":"ServerExposed.Ping","params":[{"Num":20454}],"id":0}'
    1542293777: Sending PUBLISH to device1 (d0, q0, r0, m0, '$rpc/device1/client_20454/25MhY4xUwiMZIuytfb89Vjrh4QU=/req', ... (64 bytes))
    
    1542293777: Received PUBLISH from device1 (d0, q0, r0, m0, '$rpc/device1/client_20454/25MhY4xUwiMZIuytfb89Vjrh4QU=/res', ... (57 bytes))
    1542293777: > payload: '{"id":0,"result":{"Done":true,"Num":20454},"error":null}'
    1542293777: Sending PUBLISH to client_20454 (d0, q0, r0, m0, '$rpc/device1/client_20454/25MhY4xUwiMZIuytfb89Vjrh4QU=/res', ... (57 bytes))
    
2
  • 1
    What happens when you publish a binary payload?
    – hardillb
    Nov 15, 2018 at 22:51
  • oh, only tested with ascii payload, binary maybe broke the terminal, forgot to say
    – yurenchen
    Nov 16, 2018 at 14:50
2

I found this a while back but I'm unable to attribute to the original author. Works great for existing logs, but can't 'tail -f' with this solution:

sudo cat /var/log/mosquitto/mosquitto.log | grep -v datab|perl -pe 's/(\d+)/localtime($1)/e'

Using this on linux, but should work on WSL/cygwin.

1
  • 2
    Why can't you swap the cat for tail -f? and if not then the cat is redundant and you should just let grep open the file
    – hardillb
    Jan 6, 2020 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.