Skip to content

Logging

Initialize SDK Logging and Set Log Level

// Initialize SDK Logging
Logging.initialize(context,
    new Logging.ConfigurationBuilder()
        .logToConsole(true));//default is false

// Log message from application code
org.slf4j.Logger sLogger = org.slf4j.LoggerFactory.getLogger(this.getClass().getName());
((ch.qos.logback.classic.Logger) sLogger).setLevel(ch.qos.logback.classic.Level.DEBUG);
sLogger.debug("Some message");

// In certain debugging scenarios, you may be asked by SAP support or engineering team
// to enable tracing on SDK components. Do this when requested and for troubleshooting only.
String[] sdkComponents = {"com.sap.cloud.mobile.foundation",
    "com.sap.cloud.mobile.odata"};
for (String component : sdkComponents) {
    ((ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(component))
        .setLevel(ch.qos.logback.classic.Level.DEBUG);
}
// Initialize SDK Logging
Logging.initialize(
    context,
    Logging.ConfigurationBuilder()
        .logToConsole(true) //default is false
)

// Log message from application code
val sLogger = org.slf4j.LoggerFactory.getLogger(this.javaClass.name)
(sLogger as ch.qos.logback.classic.Logger).level = ch.qos.logback.classic.Level.DEBUG
sLogger.debug("Some message")

// In certain debugging scenarios, you may be asked by SAP support or engineering team
// to enable tracing on SDK components. Do this when requested and for troubleshooting only.
val sdkComponents = arrayOf("com.sap.cloud.mobile.foundation", "com.sap.cloud.mobile.odata")
for (component in sdkComponents) {
    (org.slf4j.LoggerFactory.getLogger(component) as ch.qos.logback.classic.Logger).level =
            ch.qos.logback.classic.Level.DEBUG
}

Upload Logs to SAP Mobile Services

// Initialize callback for upload
Logging.addUploadListener(new Logging.UploadListener() {

    // Executes on UI thread
    @Override
    public void onSuccess() {
        sLogger.debug("Upload completed successfully.");
    }

    @Override
    public void onError(Throwable ex) {
        sLogger.error("Log upload failed with an exception: {}", ex.getMessage());
    }

    @Override
    public void onProgress(int percentUploaded) {
        sLogger.debug("{}% log uploaded.", percentUploaded);
    }
});

// Upload log, make sure the mobile services application configuration is set to allow upload
Logging.uploadLog();
// Initialize callback for upload
Logging.addUploadListener(object : Logging.UploadListener {

    // Executes on UI thread
    override fun onSuccess() {
        sLogger.debug("Upload completed successfully.")
    }

    override fun onError(ex: Throwable) {
        sLogger.error("Log upload failed with an exception: {}", ex.message)
    }

    override fun onProgress(percentUploaded: Int) {
        sLogger.debug("{}% log uploaded.", percentUploaded)
    }
})

// Upload log, make sure the mobile services application configuration is set to allow upload
Logging.uploadLog()

Last update: April 14, 2021