Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
Showing results for 
Search instead for 
Did you mean: 
Product and Topic Expert
Product and Topic Expert
0 Kudos
Previous (Registration)   Home   Next (Online OData)

The SDK provides a separate log file for an application which can be periodically uploaded to the SAP Cloud Platform Mobile Services server.
The root logger has a log level that can be set in the application or can be remotely set using the SAP Cloud Platform mobile services cockpit.
The SDK components themselves have loggers whose log level can be individually set. Child loggers can inherit the log level from their parent.

The following are some additional sources of documentation on Logging.
API Logging Reference
API Log Upload Reference
Logging with Native iOS Mobile Interactive Tutorial

The following examples attempt to demonstrate the functionality of the Logger.
Initiating a Logger and Setting its Log Level
Setting the Log Level in the Cockpit
Using the Logger to Log
Uploading the Log
Viewing the Uploaded Log

Initiating a Logger and Setting its Log Level

  1. Add the following import to the top of ViewController.
    import SAPCommon

  2. Near the top of the ViewController class, in the declaration section, add a reference to the rootLogger and create a logger for the ViewController class.

    let rootLogger = Logger.root
    let logger = Logger.shared(named: "ViewController")

  3. In the viewDidLoad method change the log level of the rootLogger to debug by adding the below line after super.viewDidLoad().
    rootLogger.logLevel = LogLevel.debug

  4. Add a breakpoint in the just added line by double clicking on the line number.
    Add a second breakpoint on the following line in the onboardOrRestore method.
    print("Successfully restored")

    When the first breakpoint hits, examine the Logger Hierarchy using the dumpHierarchy method. Notice that the ViewController logger is a child of the root logger and that it inherits its parents log level as it has not been explicitly set.
    po rootLogger.dumpHierarchy()

  5. In the debugger, expand the rootLogger and notice that it has two handlers added to it (ConsoleLogHandler and OSLog). Also note that a handler can have a formatter. These can be helpful if you wish to further customize where the logs are written to or the format of the log.

  6. Using the debugger, step over the line that sets the log level to DEBUG.
    Notice this time the log level of all the child loggers has changed since the root log level was set.

  7. Using the debugger, continue to the next breakpoint in the onboardOrRestore method.
    Notice the log hierarchy now contains additional loggers that were added as part of the registration.

Setting the Log Level in Cockpit

Add the following line to the buttonPressed method and use it to see the effect of changing the log level in the management cockpit as shown below.
print("Log level of app is now at \(self.rootLogger.logLevel!)")

The log level of the application can be remotely set at the application level.

It can also be set for an individual registration.

Using the Logger to Log

In the following section, the ViewController logger will be used to log a few lines when the Button is pressed.

  1. In the Mobile Services Management cockpit, set the log level to be warn.

  2. In the buttonPressed method add the following methods after the existing print method.
    logger.debug("Button pressed \(numberOfPresses) times")
    logger.warn("Button pressed \(numberOfPresses) times")

    Run the app and press the button. Notice that since the log level of the app is warn, the line logged at the debug level is not logged. Also notice the extra information that the formatter adds such as the date, time, log level, class and method name.

Uploading the Log

In the following section, the client log will be uploaded to the Mobile Services and then viewed in the management cockpit.

  1. In the viewDidLoad method, after super.viewDidLoad(), add the following code.
    do {
    try SAPcpmsLogUploader.attachToRootLogger()
    catch {
    logger.error("Failed to attach to root logger.", error: error)

  2. Add the following function.
    private func uploadLogs(_ urlSession: SAPURLSession, _ settingsParameters: SAPcpmsSettingsParameters) {
    SAPcpmsLogUploader.uploadLogs(sapURLSession: urlSession, settingsParameters: settingsParameters) { error in
    if let error = error {
    self.logger.error("Error happened during log upload.", error: error)
    print("Logs have been uploaded successfully.")

  3. Call the uploadLogs function at the end of the buttonPressed method.
    uploadLogs(myContext.sapURLSession,[OnboardingInfoKey.sapcpmsSettingsParameters] as! SAPcpmsSettingsParameters)​

  4. If the log upload fails with the following exception it can be corrected by enabling Client Log Upload.
    Error: LogUploaderError: Network error. StatusCode: 403, Error: N/A), Response: {"status":{"code":403,"message":"Log Upload disabled for Application Connection ID","parameters":{}}})

Viewing the Uploaded Log

In the following section, the client log previously uploaded will be viewed in the management cockpit.

  1. Filters can be applied to limit the shown logs to match the application id ( and on the type (Client Log).
    From this screen, the log can be clicked on to show a simplified view that contains the uploaded messages, or a log file can be checked and then either the download or view option can be clicked on to see the content that contains additional details such as the time the line was logged, the log level and the file name. An example log line follows as well, a screen shot showing the simplified format.
    #2.0#2017-11-09 06:33:25 AM#WARNING#/Users/p1743065160/Documents/iOSSDK/Getting Started/Getting Started/ pressed 1 times#

    Note that entries logged using print are not logged to the application log file.

Previous (Registration)   Home   Next (Online OData)