Skip to Content

Logger

The easiest way to log a message is to use one of the console.log methods.

console.log("Coolant level is low, check for coolant leak");
console.warn("Antimatter containment fields fluctuating");
console.error("Warp core ejection in progress");

Note that when using Safari or Chrome to view the logged messages, you must ensure the appropriate filter is set. As shown above, the All filter is set so all logged messages are shown.

For further information on how to debug the JavaScript component of a Kapsel app see Debugging.

The Kapsel Logger plugin persists the logged messages and enables them to be viewed, emailed, or uploaded from the app.

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

The Logger plugin also has a log level so that only messages logged at that log level or higher are persisted.

sap.Logger.setLogLevel(sap.Logger.ERROR)

The log level once set is persistent between application restarts on Android and iOS.

The Kapsel plugins will also use the Logger plugin’s log level when deciding what information to log. The information logged from the Kapsel plugins is from both the JavaScript and native layers.

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

The following application attempts to demonstrate how the Logger plugin can be used.

  • In the folder C:\Kapsel_Projects\KapselGSDemo add the logger plugin.
    cordova plugin add kapsel-plugin-logger --searchpath %KAPSEL_HOME%/plugins
    or
    cordova plugin add kapsel-plugin-logger --searchpath $KAPSEL_HOME/plugins
    
  • Replace www\index.html with with the following content.
    <!DOCTYPE html>
    <html>
        <head>
            <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
            <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
            <script type="text/javascript" charset="utf-8" src="serverContext.js"></script>
            <script>
                applicationContext = null;
                
                window.onerror = onError;
    
                function onError(msg, url, line) {
                    var idx = url.lastIndexOf("/");
                    var file = "unknown";
                    if (idx > -1) {
                        file = url.substring(idx + 1);
                    }
                    alert("An error occurred in " + file + " (at line # " + line + "): " + msg);
                    return false; //suppressErrorAlert;
                }
    
                function init() {
                    if (navigator.notification) { // Override default HTML alert with native dialog. alert is not supported on Windows
                        window.alert = navigator.notification.alert;
                    }
                    getLogLevel();
                    sap.Logon.init(function() { }, function() { alert("Logon Failed"); }, appId, context);
                }
                
                function logMessage() {
                    var logMessage = document.getElementById("logMessage").value;
                    
                    var level = document.getElementById("level").value;
                    if (level === "DEBUG") {
                        console.debug(logMessage);  //could also use sap.Logger.debug
                    }
                    else if (level === "INFO") {
                        console.info(logMessage);
                    }
                    else if (level === "WARN") {
                        console.warn(logMessage);
                    }
                    else if (level === "ERROR") {
                        console.error(logMessage);
                    }
                }
                
                function uploadLog() {
                    sap.Logger.upload(function() {
                        alert("Upload Successful");
                    }, function(e) {
                        alert("Upload Failed. Status: " + e.statusCode + ", Message: " + e.statusMessage);
                    });
                }
                
                function setLogLevel(logLevel) {
                    sap.Logger.setLogLevel(logLevel);
                    console.log("Log level set to " + logLevel);
                    document.getElementById("span1").innerHTML = logLevel;
                }
                
                function getLogLevel() {
                    sap.Logger.getLogLevel(getLogLevelSuccess, getLogLevelFailure);
                }
                                                          
                function getLogLevelSuccess(level) {
                    document.getElementById("span1").innerHTML = level;
                    console.log("Log Level is " + level);
                }
                
                function getLogLevelFailure(error) {
                    alert("Failure in getting log level" + JSON.stringify(error));
                }
            
                function clearLogSuccess() {
                    console.log("Log has been cleared");
                }
            
                function clearLogError(error) {
                    alert("Failure in clearing log. " + JSON.stringify(error));
                }
            
                function emailLogSuccess() {
                    console.log("Log emailed successfully");
                }
            
                function emailLogError(error) {
                    alert("Failure in emailing log. " + JSON.stringify(error));
                }
    
                function getLogEntriesSuccess(logEntries) {
                    var stringToShow = "";
                    var logArray;
                    
                    if (device.platform == "windows") {
                        logArray = logEntries.split("\n");
                        if (logArray.length > 0) {
                            for (var i = 0; i < logArray.length; i++) {
                                stringToShow += logArray[i] + "\n";
                            }
                        }
                    }
                    else if (device.platform == "iOS") {
                    	logArray = logEntries.split("\n");
                        if (logArray.length > 0) {
                            for (var i = 0; i < logArray.length; i++) {
                                logLineEntries = logArray[i].split(" ");
                                for (var j = 7; j < logLineEntries.length; j++) {
                                     stringToShow += logLineEntries[j] + " ";    
                                }
                                stringToShow = stringToShow + "\n";
                            }
                        }
                    }
                    else {  //Android
                       logArray = logEntries.split('#');
                        if (logArray.length > 0) {
                            var numOfMessages = parseInt(logArray.length / 15);
                            for (var i = 0; i < numOfMessages; i++) {
                                stringToShow += logArray[i * 15 + 1] + ": " + logArray[i * 15 + 3] + ": " + logArray[i * 15 + 14] + "\n";
                            }
                        }
                    }
                    alert(stringToShow);
                    console.log("EventLogging: Device Log follows " + stringToShow);
                }
            
                function getLogEntriesError(error) {
                    alert("Failure in getting log entries. " + JSON.stringify(error));
                }
            
                document.addEventListener("deviceready", init, false);
            </script>
            
        </head>
        <body>
            <h1>Logger Sample</h1>
            <button onclick="sap.Logger.clearLog(clearLogSuccess, clearLogError)">Clear the Log</button><br><br>
            Application Log Level: <span id="span1"></span><br><br>
            Set Application Log Level:  <button onclick="setLogLevel(sap.Logger.DEBUG)">DEBUG</button><button onclick="setLogLevel(sap.Logger.INFO)">INFO</button><button onclick="setLogLevel(sap.Logger.WARN)">WARN</button><button onclick="setLogLevel(sap.Logger.ERROR)">ERROR</button><br><br>
    
            Log Message at Level:<select id="level"><option>DEBUG</option><option>INFO</option><option>WARN</option><option>ERROR</option></select><br><br>
            
    
    
            Log a message  <input type="text" id="logMessage" value="Logged using console.log"><br>
            <button id="log" onclick="logMessage()">Log</button><br><br>
    
            Messages logged using sap.Logger can be viewed on the device.<br>
            <button id="getLogEntries" onclick="sap.Logger.getLogEntries(getLogEntriesSuccess, getLogEntriesError)">View Log</button><br><br>
    
            Messages logged using sap.Logger can be sent via email.<br>
            <button id="email" onclick="sap.Logger.emailLog('helpdesk@mycompany.com','The log','Please see the attached log.', emailLogSuccess, emailLogError)">Email Log</button><br><br>
    
            Messages logged using sap.Logger can be uploaded to the SMP 3.0 server.<br>
            <button id="upload" onclick="uploadLog()">Upload Log</button><br><br>
            
        </body>
    </html>​
  • Prepare, build and deploy the app with the following command.
    cordova run android
    or 
    cordova run windows -- --archs=x64 
    or
    cordova run windows --device -- --archs=arm --phone
    or
    cordova run ios

    Set the application log level to be WARN
    Clear the Log
    Log a message at the DEBUG log level
    Log a message at the WARN log level
    Log a message at the ERROR log level
    Click on View Log
    Notice that the message logged at the debug level does not appear in the log.

  • 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.
    A bug has been reported where only the messages that are at the log level specified here or above are uploaded to the server.
    It is possible to set the Log Upload to be enabled for each new registration via the Application > Configure > Client Policies > Log Policy settings.The log once uploaded to the SMP server can be viewed under Logs > Logs & Traces as shown below.
  • 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\KapselGSDemo\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 recommended to increase these settings. The log files can be viewed on an emulator as shown below under data\data\com.kapsel.gs\files .

    Note, the Android 7 and 7.1.1 emulators do not appear to enable viewing of files as noted in this bug report.

Back to Getting Started With Kapsel

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