-
Notifications
You must be signed in to change notification settings - Fork 906
Client-side logging utility for ServiceNow #2525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
|
||
| 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); | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
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