-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathLogHelper.js
More file actions
291 lines (240 loc) · 5.44 KB
/
LogHelper.js
File metadata and controls
291 lines (240 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
var LogHelper = Class.create();
/**
* Server side helper class for logging purposes. This class supports a debug mode.
* If enabled via system property log statements are written to Sys Log.
*
* @class LogHelper
* @author Maik Skoddow
* @see https://www.servicenow.com/community/developer-articles/just-another-log-helper/ta-p/2315453
*/
LogHelper.prototype = {
initialize: function(
strTableName
) {
this._strTableName = strTableName;
if (!gs.tableExists(strTableName)) {
throw new Error(
'Table "' + strTableName + '" does not exist in that instance!'
);
}
var _strAbsoluteBase =
String(GlideDBObjectManager.get().getAbsoluteBase(strTableName));
if (_strAbsoluteBase !== 'syslog') {
throw new Error(
'Table "' + strTableName + '" is not a child of the "syslog" table!'
);
}
},
debug: function(
strSource,
strMessage
) {
if (gs.getProperty(LogHelper.DEBUG_PROPERTY, "false") === "true") {
return LogHelper._insertLogRow(
this._strTableName,
LogHelper.LOG_LEVELS.DEBUG,
strSource,
LogHelper._getMessageStr(arguments) || strMessage
);
}
return '';
},
info: function(
strSource,
strMessage
) {
return LogHelper._insertLogRow(
this._strTableName,
LogHelper.LOG_LEVELS.INFO,
strSource,
LogHelper._getMessageStr(arguments) || strMessage
);
},
warn: function(
strSource,
strMessage
) {
return LogHelper._insertLogRow(
this._strTableName,
LogHelper.LOG_LEVELS.WARN,
strSource,
LogHelper._getMessageStr(arguments) || strMessage
);
},
error: function(
strSource,
strMessage
) {
return LogHelper._insertLogRow(
this._strTableName,
LogHelper.LOG_LEVELS.ERROR,
strSource,
LogHelper._getMessageStr(arguments) || strMessage
);
},
fatal: function(
strSource,
strMessage
) {
return LogHelper._insertLogRow(
this._strTableName,
LogHelper.LOG_LEVELS.FATAL,
strSource,
LogHelper._getMessageStr(arguments) || strMessage
);
},
type: 'LogHelper'
};
//name for system property that is checked whether debug mode is enabled
LogHelper.DEBUG_PROPERTY = 'loghelper.enable.debug';
//target tabe if used with static methods
LogHelper.STANDARD_LOG_TABLE = 'syslog';
LogHelper.LOG_LEVELS = {
DEBUG : '-1',
INFO : '0',
WARN : '1',
ERROR : '2',
FATAL : '3'
};
LogHelper.debug =
function(
strSource,
strMessage
) {
if (gs.getProperty(LogHelper.DEBUG_PROPERTY, "false") === "true") {
return LogHelper._insertLogRow(
LogHelper.STANDARD_LOG_TABLE,
LogHelper.LOG_LEVELS.DEBUG,
strSource,
LogHelper._getMessageStr(arguments) || strMessage
);
}
return '';
};
LogHelper.info =
function(
strSource,
strMessage
) {
return LogHelper._insertLogRow(
LogHelper.STANDARD_LOG_TABLE,
LogHelper.LOG_LEVELS.INFO,
strSource,
LogHelper._getMessageStr(arguments) || strMessage
);
};
LogHelper.warn =
function(
strSource,
strMessage
) {
return LogHelper._insertLogRow(
LogHelper.STANDARD_LOG_TABLE,
LogHelper.LOG_LEVELS.WARN,
strSource,
LogHelper._getMessageStr(arguments) || strMessage
);
};
LogHelper.error =
function(
strSource,
strMessage
) {
return LogHelper._insertLogRow(
LogHelper.STANDARD_LOG_TABLE,
LogHelper.LOG_LEVELS.ERROR,
strSource,
LogHelper._getMessageStr(arguments) || strMessage
);
};
LogHelper.fatal =
function(
strSource,
strMessage
) {
return LogHelper._insertLogRow(
LogHelper.STANDARD_LOG_TABLE,
LogHelper.LOG_LEVELS.FATAL,
strSource,
LogHelper._getMessageStr(arguments) || strMessage
);
};
LogHelper._getMessageStr =
function(
arrArguments
) {
var _args = [];
var _arrMessage = [];
var _e;
for (var numIndex = 1; numIndex < arrArguments.length; numIndex++) {
if (arrArguments[numIndex] !== null &&
arrArguments[numIndex] !== 'undefined'
) {
if (arrArguments[numIndex] instanceof Error ||
String(arrArguments[numIndex]).startsWith('org.mozilla.javascript.')
) {
_e = arrArguments[numIndex];
}
else {
_args.push(arrArguments[numIndex]);
}
}
}
var _strMessage =
_args.length < 1 ?
'???' :
_args[0].toString();
for (var _numIndex = 1; _numIndex < _args.length; _numIndex++) {
var _strValue =
JSUtil.nil(_args[_numIndex]) ?
'' :
_args[_numIndex].toString();
_strMessage =
_strMessage.replace(
new RegExp('{' + (_numIndex - 1) + '}', 'g'),
_strValue
);
}
_arrMessage.push(_strMessage);
if (_e) {
_arrMessage.push('\n--> ');
if (_e instanceof Error) {
_arrMessage.push(_e.message);
_arrMessage.push('\n');
if (_e.stack) {
_arrMessage.push(
_e.stack
.split('\n')
.slice(0, 4)
.filter(function(line) {
return line.trim().length > 0;
})
.map(function (line) {
return '---> ' + line.trim();
})
.join('\n')
);
}
}
else {
_arrMessage.push(String(_e));
}
}
return _arrMessage.join('');
};
LogHelper._insertLogRow =
function(
strTableName,
strLevel,
strSource,
strMessage
) {
var _grLog = new GlideRecord(strTableName);
var _strNextSeq = GlideCounter.next(strTableName + '::sequence');
_grLog.setValue('level', strLevel);
_grLog.setValue('source', strSource);
_grLog.setValue('message', strMessage);
_grLog.setValue('sequence', _strNextSeq);
_grLog.insert();
return strMessage;
};