androidmqttpahojava.util.logging

How to enable Paho Mqtt Android Client logging in Android Studio


I have followed examples and docs to run MqttAndroidClient and successfully connect and pub/sub with mosquito broker, both in open and secure to ports 1883, 8883, 8884, & 8885. This success is built with this: implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1 implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0

But to connect to AWS IOT, I need the SNI extension added to mqttv3:1.2.3 or later. Keeping it simple for now, just still connecting with mosquito broker, connection fails as soon as I use mqttv3:1.2.1 or later.

So I need to enable the mqttv3 logging to diagnose why, but I am bewildered on how to do it in Android Studio even after looking over some similar questions for command line invocation of java.

I have added the AndroidClient logging:


mqttClient = MqttAndroidClient(context, serverURI, "PleaseHelpMeLog")


mqttClient.setTraceCallback(object : MqttTraceHandler {
    override fun traceDebug(tag: String?, message: String?) {
        Log.d("$tag", "$message")
    }

    override fun traceError(tag: String?, message: String?) {
        Log.d("$tag", "$message")
    }

    override fun traceException(tag: String?, message: String?, e: java.lang.Exception?) {
        Log.d("$tag", "$message")
    }
}
)

mqttClient.setTraceEnabled(true)

And I got the source of the failure in an AndroidClient log: connect fail, call connect to reconnect.reason:MqttException

But this is not the mqttv3 logging, which I would need for what leads up to "MqttException".

If I could get to the mqttClient used by the AndroidClient, I understand that I could use something like mqttDebug = mqttClient.getDebug() and then mqttDebug.dumpClientDebug()

How can this be enabled in an Android Studio build?


Solution

  • I have found a way to enable the FINE logs from Paho buried in this AWS-Amplify issue: https://github.com/aws-amplify/aws-sdk-android/issues/3022

    To view the Paho logs, create a new file called newlogging.properties and save it in the app/src/main/res/raw/ directory of your project. Add the following to that file:

    handlers= java.util.logging.ConsoleHandler
    # Global logging level.
    .level= FINEST
    # default file output is in user's home directory.
    java.util.logging.FileHandler.pattern = %h/java%u.log
    java.util.logging.FileHandler.limit = 50000
    java.util.logging.FileHandler.count = 1
    java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
    # Limit the message that are printed on the console to FINEST and above.
    java.util.logging.ConsoleHandler.level = FINEST
    java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
    

    The file contents above are essentially a copy of the logging.properties file from the Java installation but with lower logging levels in order to see the debug logs from Paho. Also add the following code to your app before creating the AWSIotMqttManager, which will set the logging properties to the newlogging.properties file created:

    LogManager logManager = LogManager.getLogManager();
    try {
        Context context = getApplicationContext();
        int resourceId = context.getResources().getIdentifier("newlogging", "raw", context.getPackageName());
        InputStream inputStream = context.getResources().openRawResource(resourceId);
        logManager.readConfiguration(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }