Better Logging with GSLog - ben-vargas/servicenow-wiki GitHub Wiki

Quick Copy/Paste Code
(function(){ 
   var TAG = '[LOCATION: NAME] ';
   var log = new GSLog('tp13.logging.verbosity', TAG.trim());

   log.logDebug(TAG + current.user_name + ' - current.mobile_phone: ' + current.mobile_phone);
})();

Better Logging with GSLog

Effective logging is essential for debugging and monitoring in ServiceNow development. While gs.log() is commonly used, it lacks flexibility in controlling log output levels. GSLog provides a more robust solution by allowing developers to manage log verbosity through system properties, ensuring that debug statements can remain in the code without cluttering production logs.

Why Use GSLog?

  • Centralized Control: Adjust logging behavior across your instance by modifying a single system property.
  • Flexible Verbosity Levels: Enable or suppress logs dynamically without code changes.
  • Cleaner Codebase: Maintain debug statements in your code without the risk of exposing them unintentionally in production.

Understanding GSLog

GSLog is a script include that simplifies script logging and debugging by implementing multiple levels of log output, selectable by per-caller identified system properties. Logs can be at various levels, such as Debug, Info, Notice, Warning, Err, or Crit, following the BSD syslog conventions. The default logging level is Notice, so levels should be chosen accordingly.

How to Implement GSLog

  1. Define a System Property:

    Create a system property to control the logging level. This property can be named according to your organization's naming conventions; for example, using a common abbreviation for your company or client. In this guide, we'll use tp13.logging.verbosity as an example. Set its value based on the desired log level:

    • debug: Logs detailed debug messages.
    • info: Logs informational messages.
    • notice: Logs normal but significant conditions.
    • warning: Logs warning conditions.
    • err: Logs error conditions.
    • crit: Logs critical conditions.
  2. Initialize GSLog in Your Script:

    Incorporate GSLog into your server-side scripts using an immediately-invoked function expression (IIFE) to maintain scope and prevent global variable conflicts.

    (function() {
        var TAG = '[Your Script Identifier] ';  // Customize this tag to identify your script
        var log = new GSLog('tp13.logging.verbosity', TAG.trim()); // Initialize GSLog with the system property
    
        // Example log statements:
        log.logDebug('This is a debug message.');
        log.logInfo('This is an info message.');
        log.logWarning('This is a warning message.');
        log.logErr('This is an error message.');
        log.logCrit('This is a critical message.');
    })();
  3. Adjust Logging Levels as Needed:

    Modify the value of your system property (e.g., tp13.logging.verbosity) to control the verbosity of logs without altering the codebase. This allows for dynamic adjustment of logging levels in different environments (e.g., development, testing, production).

Example Usage

Scenario: You have a Business Rule that triggers on incident updates, and you want to log the user's name and mobile phone for debugging purposes.

(function() {
    var TAG = '[BR: Incident Update] ';
    var log = new GSLog('tp13.logging.verbosity', TAG.trim());

    // Ensure 'current' is defined and has the necessary fields
    if (typeof current !== 'undefined' && current.user_name && current.mobile_phone) {
        log.logDebug(TAG + 'User: ' + current.user_name + ' - Mobile: ' + current.mobile_phone);
    } else {
        log.logWarning(TAG + 'Current record is undefined or missing required fields.');
    }
})();

Steps:

  1. Set the System Property: Ensure your logging verbosity system property (e.g., tp13.logging.verbosity) is set to the desired level (e.g., debug) in System Properties > All Properties.
  2. Insert the Script: Place the above code in the appropriate Business Rule script field.
  3. Test the Functionality: Update an incident record to trigger the Business Rule and observe the logs.
  4. Review Logs: Navigate to System Logs > All to verify that the log entries appear as expected based on the set verbosity level.

Benefits of Using GSLog

  • Dynamic Logging Control: Change log levels without modifying code, facilitating easier debugging and monitoring.
  • Enhanced Log Management: Filter logs based on severity, improving the efficiency of log analysis.
  • Reduced Risk: Minimize the chance of exposing sensitive debug information in production environments.

By adopting GSLog for logging in ServiceNow, you enhance your ability to manage and control log output effectively, leading to more maintainable and secure applications.

For more detailed information, refer to the official ServiceNow documentation on GSLog.

⚠️ **GitHub.com Fallback** ⚠️