(SM) Support Tip: Implementing custom Logging

This article outlines different approaches to write custom log information.
This may be used for integrating logging aspect into custom implementations as well for temporarilly adding logging statements for issue analysis.

  1. Log file

    The default log file is the log file for the servlet. Writing to this file can be done like this:

    System language:

           $success=rtecall( "log", $L.rc, "My message" )                         

    SM JavaScript:

           var rc;
       var success = system.functions.rtecall( "log", rc, "My message" );
    SM JavaScript also implements the

           print( "My message" );

    function to write to the message log in the current session. This information appears in the message view of the client. These messages are printed into the servlet log as well, when the servlet was started with parameter


    Now, sometimes you want to write into a log file separated from the servlet log file. This can be done using writeFile() function:
           writeFile( "c:\\SM\\logs\\mylog.log", "a", "My message");                                

     In JavaScript string the backslash is used as escape character, hence to use the backslash character itself, it has to appear twice.

  2. Converting variables to string to print to log file 

    Variables can be converted into string for logging:

    System language:

            $s=str( '1 00:00:00' )

    SM JavaScript:

        // converting a JavaScript value into string

        var s=1.toString();  

        // JavaScript implictly uses toString() method to convert a value to string             


        // converting a value by System Language function into string   

        var s=system.functions.str(1);   

    For compound data types, JavaScript function toString() returns frequently just the string representation of the data type, which is not really helpful. Following statements can be used to convert complex objects into a string:
    Converting SCFile object to XML string:               
        var f = new SCFile( "dbdict" );
        var rc = f.doSelect('name="pmstatus"');

        // like system language str() function - does not include fieldnames
        var s1 = f.getText();

        // output as XML document - this includes tags with fieldnames
        var s2 = f.getXML();

    Converting a JS object to JSON string:

            var s = lib.JSON.json().stringify( ["a", 1, { field1: "abc", field2: 2 } ] );

  3. Writing a custom logging function in SM JavaScript 

    When designing a logging function, you may want to implement a log level: Some message should only be logged when debugging, but not during normal operations.

    A typical log function design is:

       var loglevel = "INFO";
       function logDebug( s )
          if (loglevel == "DEBUG") print( s );

    When looking at the call of this function, we seen an issue with this design:

           logDebug( "Record: " record );
    At time of the call, the object "record" is converted into a string. However, when the global variables "loglevel" is not "DEBUG", the message is not printed at all. So depending on the size of "record", and the frequency of this call (i.e. in a loop), this may cause significant memory consumption and execution time even without requirement to.

    Therefore consider a design that takes the

         var loglevel = "INFO";
       function logDebug( )
         if (loglevel == "DEBUG") print( Array.prototype.join.call(arguments) );

       logDebug( "Record: ", record);
    Now, the reference to object "record" is handed to the logDebug() function and the conversion to string takes only place if the variable "loglevel" is equal "DEBUG".

  4. OOB Loggers for JavaScript

    Using ScriptLibrary log4js:

    The ScriptLibrary "log4js" supports logging in Log4J style to message log. 

    // Create a logger object set to log level "INFO"

    var myLogger=new lib.log4js.Log(lib.log4js.Log.INFO);

    myLogger.info("an info");

    Read more in Help server:
    Develop > Programming Guide > Where can I use JavaScript in Service Manager? > What is the ScriptLibrary? > The log4js script

    Using RTE-supported logger:


    Using the getLog() global JavaScript function SM RTE provides logger support.

    // Get a logger for name "MyLog"

    var mylog = getLog("MyLog");

    // Set the log level


    // Log a message for log level "trace"

    mylog.trace('trace message');

    Read more in Help server:
    Develop > Programming Guide > JavaScript and Service Manager reference > List: JavaScript global methods > JavaScript global method: getLog

    There is GUI-support for this kind of logger (using ScriptLibrary "jlog"): On search form for ScriptLibrary, you select the option "JavaScript Logger" and you get to a dialog where you can set the log level for loggers by name.