Getting Started with Kapsel – Part 6 — Logger
Logger
If you are using SP 13 or higher please see Logger SP13+
The easiest way to log a message in a Kapsel app that is being run on a device or emulator attached to an Android IDE or Xcode, is to simply use the log method.
console.log("Log this");
This method works in Kapsel apps and in apps being debugged on the desktop using Chrome.
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. These restrictions are not present on Android.
The console plugin can be added with the cordova plugin add command.
cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-console.git
Note that the console plugin appears to log messages twice on Android CB-1973.
A log tag filter can be applied to only show messages logged from chromium by editing the message filter named com.mycompany.logger.
Note, in the Eclipse LogCat view, it is 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 logcat > C:\temp\log.txt
On iOS simulators and devices, the output from console.log can be seen in the All Output view.
On iOS devices, the console log can also be viewed by connecting the device to a Mac and in Xcode choosing Window > Organizer > Device > Console as shown below. Note that the console log file does not stay on the device for very long.
The Kapsel logger plugin enables messages to be logged at one of four levels (Debug, Info, Warn or Error) using
sap.Logger.debug("Message to be logged at DEBUG level"); sap.Logger.info("Message to be logged at INFO level"); sap.Logger.warn("Message to be logged at WARN level"); sap.Logger.error("Message to be logged at ERROR level");
It also provides a method,
sap.Logger.upload()
that when called can upload the log to the SMP 3.0 server.
The default log level is ERROR. If the log level is set to WARN, then messages logged at WARN or ERROR would appear in the log.
The log level is set by calling
sap.Logger.setLogLevel(sap.Logger.DEBUG);
Note, this is a transient setting on iOS and is in effect as long as the application remains in memory.
In addition to the above methods, the Kapsel Logger as of SP05 captures the output of console.log and includes it in the log that can be uploaded to the SMP 3.0 server. When the Kapsel Logger plugin is included, there is no need for the console plugin so it can be removed by running the below command.
cordova remove org.apache.cordova.console
The console.log entries appear on Android three times one of which is with the log tag of SMP_LOGGER. The logged entry SMP_LOGGER only appears if the Logger plugin’s log level is set to be INFO or DEBUG. For additional details see the JavaScript file in a project that includes this plugin at
project_name\www\plugins\com.sap.mp.cordova.plugins.logger\www\logger.js
or the JS Documentation at Kapsel Logger API Reference.
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.
cordova -d create C:\Kapsel_Projects\LoggerDemo com.mycompany.logger LoggerDemo "{\"plugin_search_path\":\"C:/SAP/MobileSDK3/KapselSDK/plugins/\"}" cd LoggerDemo cordova -d platform add android cordova -d create ~/Documents/Kapsel_Projects/LoggerDemo com.mycompany.logger LoggerDemo "{\"plugin_search_path\":\"/Users/i826567/SAP/MobileSDK3/KapselSDK/plugins/\"}" cd ~/Documents/Kapsel_Projects/LoggerDemo cordova -d platform add ios
- Add the logger plugin.
cordova -d plugin add com.sap.mp.cordova.plugins.logger
- Replace www\index.html with index.html.
- Copy the files to the platform directory by running
cordova -d prepare
- Use the Android IDE or Xcode to deploy and run the project.
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.
Note as of SMP Server 3.0 SP04 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 that the log entries are in the List Log Format 2.0.
- Note, if an entry is logged, then upload is called, then another entry is logged and then upload is called, there will only be one log shown in the management cockpit rather than two logs.
- 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 show 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.
Updated for SMP 3.0 SP03
Updated for SMP SDK 3.0 SP04
Updated for SMP SDK 3.0 SP05
Updated for SMP SP06 SDK and SP05 Server.
Hi Daniel
I'm using AuthProxy (mutual authentication) to register / unRegister SMP server.
I added the Logon plugin as well but i don't use it explicitly.
Problem is, when i try to upload the log with the Logger plugin i get an error.
After debugging things up i manage to locate the error, it is on line 358 on Logger.js file,
sap.Logon.core.getState function.
sap.Logon.core is undefined.
Is this somehow connected to the fact that i didn't used the Logon Plugin?
I believe so. The Logger plugin depends on the Logon plugin.
See C:\SAP\MobileSDK3\KapselSDK\plugins\logger\plugin.xml.
Regards,
Dan van Leeuwen
so how can i use the Logger plugin along with the AuthProxy?
Perhaps you could use the Logon plugin to do the registration with software such as Afaria that provisions the client certificate to the device or the Logon plugin with the X. 509 Certificate provider interface (See the security section) if you are not using Afaria.
I suppose if you did wish to continue to not have the Logon plugin screens be seen, you could look at the technique described in the Logon plugin section. Search for
This code will enable the Registration screen and the passcode screen to be skipped.
Regards,
Dan van Leeuwen
Can't see the Code (search for.....) in your reply, it's missing.
What I meant was look at the Logon plugin section. http://scn.sap.com/docs/DOC-49524#custom
Then search for the text "This code will enable the Registration screen and the passcode screen to be skipped."
Hi Daniel
We are using AuthProxy for Mutual authentication (certificate from store), we are not using Logon.init method at all.
After registering we have the appCID (application connection id), is there any way to use the logger with the appCID?
Otherwise i will have to abort the use in AuthProxy and only use the Logon for Registering.
The Logon plugin is required by many of the other Kapsel plugins. I would suggest using it to register with the SMP server.
OK, thanks.
Where can i find the documentation for the SMP server side settings using the Logon plugin with a certificate (x509)?
I believe the Logon plugin can either use Afaria or the X 509 Certificate Provider.
See
Getting Started with Kapsel - Appendix I -- Afaria and Kapsel
and
http://scn.sap.com/docs/DOC-60990#provider
If you are just using Android, I believe you can simply use the Logon plugin with the AuthProxy setting SAPKapselHandleHttpRequests set to true as shown in the AuthProxy section of the guide. I just updated the AuthProxy section of the guide to demonstrate this. It is the last example shown.
Regards,
Dan van Leeuwen
Hi
I am using ios as well.
Security advice:
please make sure you're only logging non-business related data. In case of lost or stolen devices log data can be retrieved and disclose business data or personal data.
Same goes for any kind of authentication data (user ID, password, cookies, keys)
hi Daniel,
can you help me?
i use datajs (reference: http://scn.sap.com/servlet/JiveServlet/download/49524-17-347900/register.html.zip) for get APPCID into smp server.
can i use plugin logger without plugin logon?
thanks,
The Logger plugin has a dependency on the Logon plugin so there may be issues if you try to use the Logger plugin without the Logon plugin. What errors are you seeing?
Regards,
Dan van Leeuwen
Hello Daniel,
I have a doubt about the scope of this plugin and I would like to know his applications in a development.
My scenario is:
Our purpose should be to know the errors or crashes occurred in the application (as message log) individually for each user, for example, displaying info errors.. is that possible? Currently we have a ignorance about "deep" bugs because it is needed to debug in the console (independently of the platform) to know info errors included in my "console.log()" but you know this is not possible in a "production" environment. Any suggestion for this implementation, maybe I am wrong in the real environment this plugin is used and its purposes.
Thank you so much.
Óscar