Skip to content

Commit d409d6e

Browse files
authored
Create LogUtilsNonProd.js
Utility function for log the messages in non prod instances
1 parent 5748f61 commit d409d6e

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var LoggingUtils = Class.create();
2+
LoggingUtils.prototype = {
3+
initialize: function() {
4+
},
5+
6+
/**
7+
* @param {string} message The message to log.
8+
* @param {string} [type='info'] The type of log. Options: 'info', 'warn', 'error'.
9+
* @returns {void}
10+
*
11+
* This utility function logs a message only if the current instance is non-production.
12+
* Use it in other server-side scripts like this:
13+
* var logUtil = new LoggingUtils();
14+
* logUtil.log('This message will only show in dev and test environments.');
15+
*/
16+
log: function(message, type) {
17+
// Get the current instance name from a system property
18+
var instanceName = gs.getProperty('instance_name');
19+
20+
// You must define your production instance name.
21+
// For example, if your production instance is 'mycompanyprod'.
22+
var productionInstanceName = 'mycompanyprod';
23+
24+
// Check if the current instance name is NOT the production instance name
25+
if (instanceName && instanceName != productionInstanceName) {
26+
type = type || 'info';
27+
28+
// Determine the correct logging function based on the specified type
29+
switch (type) {
30+
case 'warn':
31+
gs.warn(message);
32+
break;
33+
case 'error':
34+
gs.error(message);
35+
break;
36+
default:
37+
gs.log(message);
38+
break;
39+
}
40+
}
41+
},
42+
43+
type: 'LoggingUtils'
44+
};

0 commit comments

Comments
 (0)