diff --git a/Client-Side Components/Client Scripts/Logger Utility/LoggerUtility.js b/Client-Side Components/Client Scripts/Logger Utility/LoggerUtility.js new file mode 100644 index 0000000000..38b8a83526 --- /dev/null +++ b/Client-Side Components/Client Scripts/Logger Utility/LoggerUtility.js @@ -0,0 +1,28 @@ +//UI Script +(function() { + 'use strict'; + + window.Logger = (function() { + const isDebugEnabled = true; // change it to 'true' for production when needed + + function formatMessage(level, ...args) { + const timestamp = new Date().toISOString(); + return [`[${timestamp}] [${level}]`, ...args]; + } + + return { + info: (...args) => isDebugEnabled && console.info(...formatMessage('INFO', ...args)), + debug: (...args) => isDebugEnabled && console.debug(...formatMessage('DEBUG', ...args)), + warn: (...args) => isDebugEnabled && console.warn(...formatMessage('WARN', ...args)), + error: (...args) => console.error(...formatMessage('ERROR', ...args)) // always log errors + }; + })(); +})(); + + +Client Side Usage: + + * Logger.info('Form loaded successfully'); + * Logger.debug('Field value:', g_form.getValue('short_description')); + * Logger.warn('Potential issue detected'); + * Logger.error('An error occurred', errorObject); diff --git a/Client-Side Components/Client Scripts/Logger Utility/README.md b/Client-Side Components/Client Scripts/Logger Utility/README.md new file mode 100644 index 0000000000..aa888a6154 --- /dev/null +++ b/Client-Side Components/Client Scripts/Logger Utility/README.md @@ -0,0 +1,20 @@ +# ServiceNow Client-Side Logger Utility + +A lightweight, reusable JavaScript utility for client-side logging within the ServiceNow platform. + +# Purpose + +This utility provides developers a standardised, timestamped, and easily toggleable logging mechanism for client-side scripts in ServiceNow. +It improves debugging efficiency and code consistency while minimising console noise in production. + + +# Features +1. Timestamped log messages [YYYY-MM-DDTHH:MM:SS] +2. Log levels: INFO, DEBUG, WARN, and ERROR +3. Easily enable/disable debug mode globally +4. Lightweight and ECMAScript 6–compliant +5. Works in Client Scripts, UI Scripts, and Service Portal widgets + +# Usage in ServiceNow instance +Create the UI Script with the code provided in LoggerUtility.js +And follow the usage described in the same file for any client side logging