Skip to content

Commit 41b7a89

Browse files
Real time log watcher (#886)
* Preview context record during approval While approving any request it was very hard until now to preview the record for which the approval was required. This UI action created on sysapproval_approver table will enable previewing the record before approval so that the approver can make an easy informed decision. * Create README.md * Delete UI Actions/Preview context record during approval directory * Real time log watcher * Create README.md
1 parent 3bc43ee commit 41b7a89

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This code-snippet will give a new experience how you see logs while debugging. This UI Page can be navigated by https://<instance_name>.service-now.com/log_watcher.do and will give an real time log capture which will help to make debugging easy for developers. You can also search with the keyword to find your desired log.
2+
3+
The below GIF is a short demo for reference.
4+
5+
![Log Watcher](https://github.com/abhrajyotikanrar/code-snippets/assets/25823899/ec51bfd2-ec48-40a4-9ae7-8b9fc37fbd56)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var TestScriptInclude = Class.create();
2+
TestScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
3+
4+
getLogs: function() {
5+
var query = this.getParameter("sysparm_query");
6+
var initialFetch = this.getParameter("sysparm_initialFetch");
7+
var startTime = this.getParameter("sysparm_startTime");
8+
var response = [];
9+
if (initialFetch == "true") {
10+
startTime = new GlideDateTime().getDisplayValue().toString();
11+
}
12+
var date = startTime.split(" ")[0];
13+
var time = startTime.split(" ")[1];
14+
var grLog = new GlideRecord("syslog");
15+
grLog.addEncodedQuery(query);
16+
grLog.addEncodedQuery("sys_created_on>=javascript:gs.dateGenerate('" + date.split("-").reverse().join("-") + "','" + time + "')");
17+
grLog.orderByDesc("sys_created_on");
18+
grLog.query();
19+
while (grLog.next()) {
20+
response.push({
21+
"message": grLog.message.toString(),
22+
"sys_created_on": grLog.sys_created_on.getDisplayValue()
23+
});
24+
}
25+
26+
return JSON.stringify({
27+
"startTime": startTime,
28+
"response": response
29+
});
30+
},
31+
32+
type: 'TestScriptInclude'
33+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
******HTML*****
2+
<?xml version="1.0" encoding="utf-8" ?>
3+
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
4+
5+
<input type="text" hidden="true" id="initialFetch" name="initialFetch" value="true" />
6+
<input type="text" hidden="true" id="startTime" name="startTime" />
7+
8+
<div style="padding: 2rem 20rem;">
9+
<div class="input-group mb-3">
10+
<input type="text" style="padding:2rem;" class="form-control" id="filterText" placeholder="Enter your filter" aria-label="Recipient's username" aria-describedby="basic-addon2" />
11+
<div class="input-group-append">
12+
<button style="padding:2rem;" class="btn btn-primary" type="button" onClick="startLogs()">Start Logs</button>
13+
</div>
14+
</div>
15+
</div>
16+
17+
<div id="log_container" style="padding: 5rem;"></div>
18+
19+
</j:jelly>
20+
****************
21+
22+
**Client Script**
23+
function startLogs() {
24+
setInterval(function() {
25+
var queryText = $j("#filterText").val();
26+
var query = "messageSTARTSWITH" + queryText;
27+
getLogs(query);
28+
}, 1000);
29+
}
30+
31+
function getLogs(query) {
32+
var initialFetch = $j("#initialFetch").val();
33+
var startTime = $j("#startTime").val();
34+
35+
var ga = new GlideAjax("TestScriptInclude");
36+
ga.addParam("sysparm_name", "getLogs");
37+
ga.addParam("sysparm_initialFetch", initialFetch);
38+
ga.addParam("sysparm_startTime", startTime);
39+
ga.addParam("sysparm_query", query);
40+
ga.getXMLAnswer(function(response) {
41+
var answer = JSON.parse(response);
42+
43+
gel('initialFetch').value = "false";
44+
gel('startTime').value = answer.startTime;
45+
46+
var data = answer.response;
47+
$j("#log_container").empty();
48+
data.forEach(function(item) {
49+
var elm = "<div><b>" + item.sys_created_on + "</b>: " + item.message + "</div>";
50+
$j("#log_container").append(elm);
51+
});
52+
53+
});
54+
}

0 commit comments

Comments
 (0)