Hivemq client logging

Hi
Where can I find the log file created by the java client library (‘com.hivemq:hivemq-mqtt-client:1.2.2’)
if there is one ?

Kind regards
Peter

Hi @pvittali ,

Nice to see you are interested in MQTT, please welcome to our community!

Logs will be output to the console.

I hope it helps. If you have further questions, please do not hesitate to ask.

Kind regards,
Dasha from HiveMQ Team

HI Dasha

Thanks, but here, no logs are output to the console.
Isn’t there a way to configure this like the broker with LogBack ?

@pvittali ,

What do you mean by “here”, it is some specific environment? Did you try System.out.println("my log message");?

Kind regards,
Dasha from HiveMQ Team

Hi Dasha

Sorry for not being clear. What I mean is that I would like to see the logging messages from the client lib, like I can see them when I use mongodb, hivemq broker, etc. That is, logging that can be configured with a LogBack ( or such) config file. The target can be a file, the console , etc. With “here” I simply mean in my standard java 19 environment. Perhaps I should have said: “I can’t figure out where the logging is sent to”.

When I read the hivemq broker documentation, I can read that logging by default goes to hivemq.log.
This is what I am looking for for the client library.

Kind regards
Peter

:slight_smile: pvittali ,

In the hivemq-mqtt-client, the logs generated by the Java hivemq-mqtt-client library using the InternalSlf4jLogger class

By default, InternalSlf4jLogger uses the Simple Logging Facade for Java (SLF4J) API for logging. The actual logging framework used will depend on the SLF4J binding you have configured in your project.

If you have not explicitly configured an SLF4J binding, then the default binding will be used, which is slf4j-simple. In this case, the logs will be output to the console.

However, if you have configured a different SLF4J binding (such as Log4j or Logback), then the logs will be output according to the configuration of that framework.

To configure an SLF4J binding to Logback, you can follow these steps:

  1. Add the Logback and SLF4J dependencies to your project. Refer to the demo project. Note the dependency added to the build.gradle.kts file:
    implementation("ch.qos.logback:logback-classic:${property("logback.version")}")
  1. Create a logback.xml or logback-test.xml configuration file in your project’s classpath. The logback-test.xml file is used for testing purposes and will override the logback.xml file if both files exist. Refer to the demo project and see the logback.xml file in the src/main/java/resources/.
    logback.xml
<configuration scan="true" scanPeriod="10 seconds">

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%-30(%d %level)- %msg%n%ex</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>hivemq-example.log</file>
        <append>true</append>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>hivemq-example.%d{yyyy-MM-dd}.log</fileNamePattern>

            <!-- keep 30 days' worth of history -->
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%-30(%d %level)- %msg%n%ex</pattern>
        </encoder>
    </appender>

    <logger name="org.example" level="DEBUG" />

    <root level="DEBUG">
        <appender-ref ref="FILE"/>
        <appender-ref ref="CONSOLE"/>
    </root>

</configuration>
  1. Update your test code to use the SLF4J API. Refer to the demo project and see the Main.java file.
...
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Main {
    private static final Logger LOG = LoggerFactory.getLogger(Main.class);
    public static void main(String[] args) throws Exception {
             LOG.debug("Debug message");
             LOG.info("Info message");
             LOG.warn("Warn message");
             LOG.error("Error message");
  }
}

Note, the demo project is connecting to HiveMQ Cloud cluster. In order to make the demo project run, you MUST update the host, username and password with your own valid values, otherwise, the run will FAIL.

        final String host = "hostname.s1.eu.hivemq.cloud";
        final String username = "Username";
        final String password = "Password";

If you are using a self-hosted broker and not using SSL, you must update the .serverPort(8883) and remove the sslWithDefaultConfig() from here:

        final Mqtt5BlockingClient client = MqttClient.builder()
                .useMqttVersion5()
                .serverHost(host)
                .serverPort(8883)
                .sslWithDefaultConfig()
                .buildBlocking();

I hope it helps. Should you have any further questions, please do not hesitate to ask.

Kind regards,
Dasha from HiveMQ Team

Hi Dasha

Thank you very much for help but I am afraid I still didn’t manage to explain what I wanted.

You showed how to use logging in java user code in general, I know how to do that.
I thought that the hivemq-client library would internally log various messages ( info, debug, etc) which can then be used for troubleshooting and my question was how to configure the internal logging.
Perhaps there is no internal logging ?

Kind regards
Peter

Hi Dasha

I think I understand what I messed up. I had to set the logging level to DEBUG in logback-test.xml.

Now, I get for example

22:20:49.105 [com.hivemq.client.mqtt-1-1] WARN c.h.c.i.m.h.p.i.MqttIncomingPublishService - No publish flow registered for MqttStatefulPublish{statel
ess=MqttPublish{topic=test, payload=15byte, qos=AT_LEAST_ONCE, retain=true, messageExpiryInterval=4294967220}, packetIdentifier=1, dup=false, topicAli
as=0, subscriptionIdentifiers=}.

This is what I was looking for , sorry for the noise, I was expecting messages with level INFO.
Kind regards
Peter