Skip to content

Commit 2c8e071

Browse files
authored
Log utils (#1995)
* Clean Script Include Framework This is a script include framework that I utilize in my day to day work to help reduce the number of extraneous logs that live on our instances as well as keep the code clean and readable so that new developers on the projects can quickly understand the purpose of the methods and how to utilize them. I've added some mock methods to the script include in code.js and at the bottom of the file I added an example of what it looks like calling the script include from a server script. * Delete Server-Side Components/Script Includes/Clean Framework directory * Added a Log Utils to the Script Includes Directory
1 parent d2f5d82 commit 2c8e071

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var log_utils = Class.create();
2+
log_utils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
3+
initialize: function(debug, noDebugMethods) {
4+
this.debug = false; // Flag to allow debug or not on this script include
5+
this.noDebugMethods = []; // Array of methods to not log from
6+
7+
if (debug) {
8+
this.debug = debug;
9+
}
10+
11+
if (noDebugMethods) {
12+
this.noDebugMethods = noDebugMethods.split(',');
13+
}
14+
15+
// Global Variables For Use In Script
16+
17+
},
18+
19+
/**
20+
* Description: Takes in a method name and message and logs the message in gs.info if debug and method isn't in noDebugMethods
21+
* Parameters: [string] - methodName: name of method calling log.
22+
[string] - msg: message being called in log.
23+
* Returns: None.
24+
*/
25+
26+
log: function(methodName, msg) {
27+
if (this.debug && this.noDebugMethods.indexOf(methodName) === -1) {
28+
gs.debug('[Log Utils - ' + methodName + '] ' + msg);
29+
}
30+
},
31+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
This is a lightweight utility for managing logging within Script Includes.
2+
It provides a simple, configurable way to enable or disable debug logs during development and to exclude specific methods from logging when necessary.
3+
4+
This approach helps maintain clean logs, reduces noise during debugging, and allows for quick toggling of logging behavior once development is complete.
5+
6+
Usage:
7+
You can call the log method anywhere inside your Script Include to record a log message:
8+
this.log(<method_name>, <message>);
9+
10+
Example:
11+
this.log('processData', 'Starting data processing...');
12+
13+
Initialization Parameters:
14+
Logging behavior is controlled through the parameters passed to the initialize method when creating the Script Include instance.
15+
16+
var utils = new scope.utils(true, 'methodName1,methodName2');
17+
18+
Parameters:
19+
======================================================================================================================
20+
|debug | Boolean false | Enables or disables logging. Set to true to allow logs, false to suppress them. |
21+
|noDebugMethods | String "" | A comma-separated list of method names that should not produce logs. |
22+
======================================================================================================================
23+
24+
Example Use Case:
25+
If you’re developing a new method that depends on existing, stable methods, you can temporarily disable logging for the known-good methods.
26+
For example:
27+
28+
var utils = new scope.utils(true, 'validatedMethod');
29+
30+
This enables logs for all methods except validatedMethod, keeping your system logs focused on what you’re currently debugging.
31+
32+
When development is complete, you can remove the parameters (or set debug to false) to disable all logs.
33+
34+
Notes:
35+
The script uses gs.debug() for logging to keep logs easily filterable within the system logs.
36+
You can switch to gs.info(), gs.warn(), or gs.error() depending on your needs.
37+
38+
Logging should typically remain disabled (debug = false) in production to avoid unnecessary system overhead.
39+
40+
This utility is flexible and can be dropped into almost any Script Include with minimal setup.
41+
42+
Example in Context:
43+
44+
var example_utils = Class.create();
45+
example_utils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
46+
initialize: function(debug, noDebugMethods) {
47+
this.logger = new log_utils(debug, noDebugMethods);
48+
},
49+
50+
processData: function() {
51+
this.logger.log('processData', 'Starting process...');
52+
// ...your logic here...
53+
this.logger.log('processData', 'Process completed successfully.');
54+
}
55+
});
56+

0 commit comments

Comments
 (0)