简体   繁体   中英

how to log to journal with mqtt mosquitto

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM