Skip to Content

Getting Started with the SAP Cloud Platform SDK for iOS

Part 3 — Logging

Previous (Registration)   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
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

The log level of the application can be remotely set.

The log level is set to this level following the SAPcpmsSettingsDownloadStep. Note the log level is not persisted between application restarts so until it is explicitly set, its value is error. The log level when set in the management cockpit as shown above affects all users of the application.

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 viewDidLoad method, change the log level from debug to warn.
    rootLogger.logLevel = LogLevel.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 SAP Cloud Platform server 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)
                return
            }
            print("Logs have been uploaded successfully.")
        }
    }
    
  3. Call the uploadLogs function at the end of the buttonPressed method.
    uploadLogs(myContext.sapURLSession, myContext.sapcpmsSettingsParameters!)
    
  4. Notice that the log upload fails with the following exception.
    Error: LogUploaderError: Network error. StatusCode: 403, Error: N/A), Response: {"status":{"code":403,"message":"Log Upload disabled for Application Connection ID","parameters":{}}})

    This can be corrected in the management cockpit by selecting Enable Client Log Upload.

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 (com.iossdk.gs) 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/ViewController.swift####977c081f-3fa6-4ce2-a976-8addd4de5f97#73f2d61f-ad86-44a4-b476-cfe8dc13d398#com.iossdk.gs#buttonPressed#iXXXXX############Button pressed 1 times#

    Note that only the lines logged at the warn or higher log level are shown as the apps log level was previously set to LogLevel.warn. Also note that entries logged using print are also not logged to the application log file.

Previous (Registration)   Next (Online OData)

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply