1

Mosquitto has an extensive logging mechanism. But it seems it only can log against syslog.

Is there a way to get logs captured in the systemd's jounal without running it as a service?

Note: mosquito runs inside a docker container

mosquitto.conf logging section is:

    # =================================================================
    # Logging
    # =================================================================
    
    # Places to log to. Use multiple log_dest lines for multiple
    # logging destinations.
    # Possible destinations are: stdout stderr syslog topic file dlt
    #
    # stdout and stderr log to the console on the named output.
    #
    # syslog uses the userspace syslog facility which usually ends up
    # in /var/log/messages or similar.
    #
    # topic logs to the broker topic '$SYS/broker/log/<severity>',
    # where severity is one of D, E, W, N, I, M which are debug, error,
    # warning, notice, information and message. Message type severity is used by
    # the subscribe/unsubscribe log_types and publishes log messages to
    # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe.
    #
    # The file destination requires an additional parameter which is the file to be
    # logged to, e.g. "log_dest file /var/log/mosquitto.log". The file will be
    # closed and reopened when the broker receives a HUP signal. Only a single file
    # destination may be configured.
    #
    # The dlt destination is for the automotive `Diagnostic Log and Trace` tool.
    # This requires that Mosquitto has been compiled with DLT support.
    #
    # 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 syslog
    log_dest file /var/log/mosquitto/mosquitto.log
    
    # 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 all
    
    
    # If set to true, client connection and disconnection messages will be included
    # in the log.
    #connection_messages true
    
    # 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 5
    
    # If set to true, add a timestamp value to each log message.
    #log_timestamp true
    
    # Set the format of the log timestamp. If left unset, this is the number of
    # seconds since the Unix epoch.
    # This is a free text string which will be passed to the strftime function. To
    # get an ISO 8601 datetime, for example:
    # log_timestamp_format %Y-%m-%dT%H:%M:%S
    #log_timestamp_format
    
    # 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

Because of log_dest syslog is specified, logging to syslog should be enabled, shouldn't it?

There are logs of mosquitto in /var/log/syslog:

    Nov 17 23:08:29 raspberrypi mosquitto[40]: 1668726509: Opening ipv6 listen socket on port 1883.
    Nov 17 23:08:29 raspberrypi mosquitto[40]: 1668726509: Opening ipv4 listen socket on port 1883.
    Nov 17 23:08:29 raspberrypi mosquitto[40]: 1668726509: Opening websockets listen socket on port 9001.
    Nov 17 23:08:29 raspberrypi mosquitto[40]: 1668726509: mosquitto version 2.0.15 running
    ...
    Nov 30 12:12:18 raspberrypi mosquitto[39]: 1669810338: Sending PUBLISH to shell (d0, q0, r0, m0, '$SYS/broker/load/bytes/sent/1min', ... (4 bytes))
    Nov 30 12:12:29 raspberrypi mosquitto[39]: 1669810349: Sending PUBLISH to shell (d0, q0, r0, m0, '$SYS/broker/load/bytes/sent/1min', ... (4 bytes))
    Nov 30 12:12:40 raspberrypi mosquitto[39]: 1669810360: Sending PUBLISH to shell (d0, q0, r0, m0, '$SYS/broker/load/bytes/sent/1min', ... (4 bytes))

But they do not end up in the journal. I suspect it is because no systemd init is running. Can this be worked around?

woodz
  • 737
  • 6
  • 13
  • 1
    Why do you not want to run it as a service? – romkey Nov 29 '22 at 18:00
  • Of course a service would ease the setup. But that requires `systemd`. Since mqtt runs in a docker container, there usually is no `systemd` init process unless you are working hard against the docker's idea – woodz Nov 30 '22 at 08:08
  • If it's running in a container then why are you not letting the container runtime handle routing the logs to where you want them? – hardillb Nov 30 '22 at 10:03

1 Answers1

0

Just configure mosquitto to log to syslog and it will end up in the journal.

Settings log_dest syslog should just work.

Edit for clarity.

This works on my Fedora 36 machine where journald appears to be consuming syslogd messages as can be seen by running journalctl -f -t mosquitto which output the usual mosquitto startup log info.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • If you have journald running then they will end up in the journal, if you don't (as you are not running systemd in the container) then they won't. As I said in the comment, why are you trying to log directly to the host's journald instance, and not configuring Docker to do this for you. I think you are trying to solve the wrong problem here. – hardillb Nov 30 '22 at 13:14
  • Sorry, where did I say, that I am logging to the host's instance? Or where did you read about that? – woodz Nov 30 '22 at 17:53
  • You've already said you are not running systemd in the container, so where else could it be running? It's not explicitly stated in the question so we have to guess and it's the next logical location. But it doesn't matter where it's running, I still think you are trying to solve the wrong problem. Let mosquitto log to stderr/stdout and let Docker deal with where to send the logs. – hardillb Nov 30 '22 at 18:05
  • >Settings `log_dest syslog` should just work<, to complete the answer: if you have a systemd inside your container. Did I interpret your effective answer correctly? – woodz Nov 30 '22 at 18:25