Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//UI Script
(function() {
'use strict';

window.Logger = (function() {
const isDebugEnabled = true; // change it to 'true' for production when needed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would require a code change every time


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);
20 changes: 20 additions & 0 deletions Client-Side Components/Client Scripts/Logger Utility/README.md
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the instructions, this does not work for any of these. If you set Global, it will not work on portal as Global are limited to desktop. When I test this creating the UI Script in the global scope, I note that the UI Script attempts to compile the interpolated variables in the string literal and so these come through as empty


# 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
Loading