Highlighted
Micro Focus Expert
Micro Focus Expert
133 views

(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

          msglog:1

    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             

        print(1);

        // 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

    mylog.setLevel('trace');

    // 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.  

Labels (1)
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.