gs.log() method in ServiceNow

7

gs.log() method  is used to log messages to the system logs. It is mainly used for debugging and auditing while development script or troubleshooting.

Syntax

gs.log(message, source);

Parameters

Parameter Description Required
message The message you want to log (string). Yes
source (Optional) A custom tag or source string to identify where the log came from. No

 

Examples

1. Simple Log Message

gs.log(“This is a simple log message.”);

Output in System Logs:

Message: This is a simple log message.
Source: GlideSystem

2. Log with a Custom Source

gs.log(“Incident record created”, “IncidentLogger”);

Output in System Logs:

Message: Incident record created

Source: IncidentLogger

3. Using Dynamic Data:

gs.log("Current user: " + gs.getUserName(), "UserLogger");

Output in System Logs:

Message: Current user: admin
Source: UserLogger

 

Alternatives

  • gs.info(): Logs informational messages (similar to gs.log()).
  • gs.warn(): Logs warnings in the logs.
  • gs.error(): Logs error messages and highlights them in the logs for easier identification.
  • gs.debug(): Logs debugging messages (only if debugging is enabled for the instance).
gs.info("This is an informational message.", "InfoLogger");
gs.warn("This is a warning message.", "WarningLogger");
gs.error("This is an error message.", "ErrorLogger");

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here