Using 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.
Use LoggingService¶
The SAP BTP SDK for Android version 4.0 introduces a simplified LoggingService API. It replaces the existing LogService and Logging APIs. This new API combines all the functionality of LogService and Logging. Client code can now use this single API to manage all logging-related tasks.
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 orjava.util.logging. In this case you will not be able to use the SDK logging API, and make sure you don't useLoggingService. - You can use a logging API other than
SLF4Jtoo. In that case you will not be able to use the SDK logging API, and make sure you don't useLoggingService. - If you want to implement your own log upload, see the documentation for Client Log Upload Service.
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.
If the Mobile Client Log Upload feature is not assigned to the mobile application in mobile services cockpit, LoggingService will keep using the log policy defined in the client code.
Use LogUtil¶
Version 25.8 of the SAP BTP SDK for Android introduces LogUtil in a standalone foundation-logging module. This is designed for client apps that want to use the logging API without using SAP Mobile Services. LogUtil continues to use SLF4J for the logging API and logback-android for the logging implementation.
The client code uses the LogUtil.configure method to configure the logging settings for the applications. This method takes three parameters: the path where logs are stored, the logging settings (including log level and file size), and an optional handler for log file rollover.
val path = application.filesDir.absolutePath + "/"
val settings = LogSettings(
logLevel = "DEBUG",
logToConsole = true,
maxFileSize = 1,
maxFileNumber = 5
)
LogUtil.configure(path, settings) {
// rolloverHandler
val activeLog = File("${LogUtil.path}${File.separator}log.0.log")
assertTrue(activeLog.length() >= 1024 * 1024) // at least 1MB
true
}
After configuration, client codes can use the LogUtil.export method to get logs. The method returns an InputStream of the exported logs. Client apps can then further process it: saving to a file or uploading to a server.
Note
The LogUtil offers the same logging API and implementation as LoggingService. Client apps that want to use SAP Mobile Services functionalities should continue using LoggingService.
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.