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.
cancel
Showing results for 
Search instead for 
Did you mean: 
Dan_vL
Product and Topic Expert
Product and Topic Expert
0 Kudos
1,903

Logger

The easiest way to log a message in a Kapsel app that is being run on a device or emulator is to use the console.log method.

console.log("Log this");

  This method works in Kapsel apps and in regular web apps being debugged on the desktop using Chrome.
Note the two screenshots below show a message being logged following the call to console.log in monitor.bat which is part of the Android SDK and in Xcode.

Note that in an iOS Cordova app, it requires adding the console plugin, the import of cordova.js, and will only work after the deviceready event is fired.  You may also need to execute the following bit of code as mentioned in console.log not working.

if (window.cordova.logger) {
    window.cordova.logger.__onDeviceReady();
}

The console plugin can be added with the cordova plugin add command.

cordova plugin add cordova-plugin-console

For Android, a log tag filter can be applied to only show messages logged from chromium.

Note it is also possible to exclude certain tags from the view by entering a regular expression such as

tag:^(?!(AndroidRuntime|linker|memtrack|dalvikvm|android.os.Debug|jdwp|SoundPool|AudioService|eglCodecCommon))

If using an Android device or simulator, the Android log file can be retrieved from the device using adb which is part of the Android SDK.

adb logcat > C:\temp\log.txt

On iOS simulators and devices, the output from console.log can be seen in the All Output view in Xcode.
On devices, the console log can also be viewed by connecting the device to a Mac and in Xcode choosing Window > Device and if needed click on the up-triangle at the bottom left.


The Kapsel logger plugin saves messages logged using console.log into a separate log file that can be emailed from the device, viewed on the device, or uploaded to an SMP server via the following commands.

sap.Logger.emailLog()
sap.Logger.getLogEntries()
sap.Logger.upload()

As of SP10, it also has a method to clear the log.

sap.Logger.clearLog()

The Kapsel log level can be set.

sap.Logger.setLogLevel(sap.Logger.DEBUG)

The default log level is ERROR.
Note the log level once set is persistent between application restarts.

The logger plugin will only save messages that are logged at a higher level than the apps current log level.  For example if the log level is sap.Logger.WARN and a message is logged at the DEBUG level, it will not be saved to the log.

Messages can be logged  at one of four levels (Debug, Info, Warn or Error) using

sap.Logger.debug("Message to be logged at DEBUG level");  or console.debug("Message to be logged at DEBUG level");
sap.Logger.info("Message to be logged at INFO level");    or console.log or console.info("Message to be logged at INFO level");
sap.Logger.warn("Message to be logged at WARN level");    or console.warn("Message to be logged at WARN level");
sap.Logger.error("Message to be logged at ERROR level");  or console.error("Message to be logged at ERROR level");

For additional details on the Logger plugin, see C:\SAP\MobileSDK3\KapselSDK\docs\api\sap.Logger.html or Using the Logon Plugin.

The following steps will demonstrate this plugin.

  • Follow the steps shown in Configuring a Kapsel App in the Management Cockpitto create an Application with the application id of
    com.mycompany.logger
      This app does not use an OData endpoint so that can be set to a dummy URL such as
    http://mycompany.com
    and add the Basic SSO mechanism.

  • Create the project and add the logger plugin.
    cordova create C:\Kapsel_Projects\LoggerDemo com.mycompany.logger LoggerDemo
    cd LoggerDemo
    cordova platform add android
    cordova plugin add kapsel-plugin-logger --searchpath %KAPSEL_HOME%/plugins

    cordova create ~/Documents/Kapsel_Projects/LoggerDemo com.mycompany.logger LoggerDemo
    cd ~/Documents/Kapsel_Projects/LoggerDemo
    cordova platform add ios
    cordova plugin add kapsel-plugin-logger --searchpath $KAPSEL_HOME/plugins
  • Replace www\index.html with index.html.

  • Prepare, build and deploy the app with the following command.
    cordova run android
    or
    cordova run ios


    Here are the results after setting the log level to INFO, Log using console.log, Log using sap.Logger at WARN level.





    As shown above on Android, a log filter can be used that filters on the Log Tag SMP_LOGGER.

  • Pressing the Upload Log button causes the log file to be uploaded to the server.
    In order for the call to sap.Logger.upload() to succeed, the Log Upload checkbox on the registration ID in the Management Cockpit must be checked.


    Note, the Log Type drop down in the Client Logging dialog can change the log level set for the application when used with the Settings plugin.
    It is possible to set the Log Upload to be enabled for each new registration via the Application > Client Policy > Client Log Policy settings.
    The log once uploaded to the SMP server can be viewed under Logs > Logs & Traces as shown below.



    Note, if an entry is logged, then upload is called, then another entry is logged and then upload is called, there may only be one log upload shown in the management cockpit rather than two log uploads.  In this case both log messages will appear in the first log upload. 

  • Note that the log entries are in the List Log Format 2.0.

  • On Android, by default there are 10 log files with a default size of 100KB.  When all 10 log files are full, the first log file is erased and then written to.  When the log files are uploaded to the SMP server following a call to sap.Logger.upload, the logs on the device are erased.  The number of log files and the file size can be set in a properties file.
    C:\Kapsel_Projects\LoggerDemo\platforms\android\assets\sap-supportability.properties
    clientLogManager=com.sap.smp.client.supportability.log.ClientLogManagerImpl
    e2eTraceManager=com.sap.smp.client.supportability.e2e.E2ETraceManagerImpl
    file_size=102400
    file_count=10
    file_age=604800000
    The file_age is in milliseconds.  The default shown above is 7 days.  After seven days, the logs are erased.

    The log files can become full quickly so it is recommend to increase these settings.  The log files can be viewed on an emulator. 


  • In addition to the application specific log file, there are logs that contain information about registration, deregistration, application settings, proxy, push etc. as shown below.
    Note, that the Status drop down is set to PATH(ALL) which lists all logged messages.  If it is set to debug, only debug messages will be shown and not warning or error level messages.



    Note, in order for this information to be captured, the component log level needs to be set and the Trace set to enabled as shown below.


    Finally there is a log file for the SMP server under the tab Log Files.


Back to Getting Started With Kapsel

3 Comments