Skip to content

Logging

Initialize SDK Logging and Set Log Level

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// 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);
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// 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 Cloud Platform Mobile Services

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Initialize callback for upload
Logging.addLogUploadListener(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(ClientProvider.get(), CpmsParameters.getSettingsParameters());
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// Initialize callback for upload
Logging.addLogUploadListener(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(ClientProvider.get(), CpmsParameters.getSettingsParameters())