Skip to Content

See How Logging Can Be Used in Your Android Application

test
0 %
See How Logging Can Be Used in Your Android Application
Details

See How Logging Can Be Used in Your Android Application

May 20, 2020
Created by
October 14, 2018
Explore how the logging component can help make deployed applications more supportable.

You will learn

  • How to use the Logging component to log messages
  • How to change the log level
  • How to upload and view logs in the SAP Cloud Platform Mobile Services cockpit


Step 1: Use the logging component
  1. In Android Studio, on Windows, press Ctrl+N, or, on a Mac, press command+O, and enter EntitySetListActivity to open EntitySetListActivity.java.

  2. On Windows, press Ctrl+F12, or, on a Mac, press command+F12, and enter onOptionsItemSelected to move to the onOptionsItemSelected method.

    Note that the following method contains two LOGGER statements:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        LOGGER.debug("onOptionsItemSelected: " + item.getTitle());
        switch (item.getItemId()) {
            case SETTINGS_SCREEN_ITEM:
                LOGGER.debug("settings screen menu item selected.");
                Intent intent = new Intent(this, SettingsActivity.class);
                this.startActivityForResult(intent, SETTINGS_SCREEN_ITEM);
                return true;
    
            default:
                return false;
        }
    }
    

    These messages will be logged when the app’s log level is set to Debug or Path and the app’s Settings menu item is opened.

  1. In Android Studio, on Windows, press Ctrl+N, or, on a Mac, press command+O, and enter EntitySetListActivity to open EntitySetListActivity.kt.

  2. On Windows, press Ctrl+F12, or, on a Mac, press command+F12, and enter onOptionsItemSelected to move to the onOptionsItemSelected method.

    Note that the following method contains two LOGGER statements:

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        LOGGER.debug("onOptionsItemSelected: " + item.title)
        return when (item.itemId) {
            SETTINGS_SCREEN_ITEM -> {
                LOGGER.debug("settings screen menu item selected.")
                val intent = Intent(this, SettingsActivity::class.java)
                this.startActivityForResult(intent, SETTINGS_SCREEN_ITEM)
                true
            }
            else -> false
        }
    }
    

    These messages will be logged when the app’s log level is set to Debug or Path and the app’s Settings menu item is opened.

Log on to answer question
Step 2: Change the log level
  1. Navigate to the entity list screen and open the app’s menu.

    Settings menu button
  2. Choose Settings.

    Settings menu opened
  3. Select Log Level.

    Log level settings option
  4. Set the level to Debug.

    Debug log level option
  5. Navigate back to the entity list screen, then back into the Settings screen to see the effect of changing the log level.

    Settings menu opened
  6. Examine the Logcat (Located at the bottom of Android Studio, click it and you can see logs). In the filter, add the name of the class that we are interested in seeing the log from: com.sap.wizapp.mdui.EntitySetListActivity.

    Notice that the messages were logged since the log level of the app was set to Debug or Path.

    Debug log level output

The SDK libraries also log output based on the app’s log level.

  1. Change the filter to com.sap.cloud.mobile.foundation.

  2. Press Back to exit the app and you will see the logged lines from the foundation library.

    Debug log level output for foundation
If the application's log level is set to Info, which of the following would be logged?
×
Step 3: Upload and view the log
  1. Navigate back to the Settings menu in the app, and this time tap Upload Log.

    Upload log button in settings menu

    A Toast message is displayed confirming that the upload succeeded.

    Log upload succeeded
  2. In the Mobile Services cockpit, navigate to Analytics > Logs > Technical Logs.

    Analytics > Logs > Technical Logs
  3. You should be able to see the log you just uploaded in the list of technical logs. You can inspect the log in the browser by clicking on its table entry, or you can download the text version by selecting it and clicking Download. If the log doesn’t appear immediately, wait for a few moments, then click Go to refresh the view.

    View log
  4. Additionally, you can access the logs locally on an emulator. You can browse the file system of an Android emulator using the Device File Explorer to view the log files as shown below, in data > data > com.sap.wizapp (or the package name of your project) > files.

    View logs in emulator
  5. You can manage the initial log level of the application and the ability for mobile services to accept logs using the Client Policies feature, as shown below.

    Log Policy
  6. You can also set the log level of an individual registration in the Management Cockpit, as shown below. Click User Registrations and then select certain Registration ID to change Log Settings. In the pop-up, you can set the log level for this specific registration.

    User RegistrationsUser Registration Log Settings

For further information on logging, see Logging.

Congratulations! You have explored how you can use the logging feature to debug or support a deployed application.

Log on to answer question

Next Steps

Back to top