Skip to content

Using the SAP Logging API

The SAP BTP SDK for Android uses Simple Logging Facade for Java (SLF4J) for its logging API, and logback-android for the logging implementation. For more information about SLF4J, refer to the SLF4J documentation. For more information about logback-android, refer to the documentation on the logback-android site.

Notes

  • If you don't need to upload logs, you don't have to use the SDK logging API. Make sure you don't use LoggingService. It will be up to you to configure the logging implementation.
  • You can use another SLF4J logging implementation instead of logback-android, such as Log4j or java.util.logging. In this case you will not be able to use the SDK logging API, and make sure you don't use LoggingService.
  • You can use a logging API other than SLF4J too. In that case you will not be able to use the SDK logging API, and make sure you don't use LoggingService.
  • If you want to implement your own log upload, see the documentation for Client Log Upload Service.

Using LoggingService

The SAP BTP SDK for Android version 4.0 introduces a simplified LoggingService API, replacing the existing LogService and Logging APIs. This API combines all of the LogService and Logging functionality so that the client code can simply use this single API to manage all logging-related tasks.

Initialize LoggingService

The best place to initialize LoggingService is in the onCreate method of Android Application. LoggingService takes two arguments in the constructor: one to indicate whether to enable the log automatic upload feature, and the other to set automatic upload type. Please refer to Uploading Logs for information on upload types. There are also two properties: one to configure the log policy and the other to set whether to write the log information to the console. The client code needs to add a LoggingService instance to the SDKInitializer.start method for log upload.

val services = mutableListOf<MobileService>()
services.add(LoggingService(autoUpload = false).apply {
    policy = LogPolicy(logLevel = "WARN", maxFileSize = 8, maxFileNumber = 2)
    logToConsole = true
})
SDKInitializer.start(
    this,
    services = services.toTypedArray()
)

Here, LoggingService is initialized as follows:

  • The automatic log upload feature is turned off
  • The initial log level is set to 'DEBUG'
  • The rolling file size is set to 8 MB
  • The rolling file count is set to 2
  • The log information will be written to the console.

If you are using the flows component for onboarding, LoggingService initialized using SDKInitializer will be updated automatically with the log policy defined at the server after the onboarding or restore finishes.

Note

Due to the limitations of the SAP Mobile Services log upload API, the maximum log file size that can be set in the log policy is 60 MB.

Get Named Logger

In classes for which you want to add logging, get a named logger from the LoggerFactory and then log at the level you require. For example:

val log = LoggerFactory.getLogger(MyActivity::class.java)
log.info("info")
log.warn("warn")
log.debug("debug")
log.error("error")
log.trace("trace")

The SAP Mobile Services does not support trace logging. Any messages logged as "trace" will be converted to "path" when uploaded.


Last update: February 29, 2024