Skip to content

Commit

Permalink
Exposing ctx for the log event.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed May 12, 2017
1 parent 6d5758e commit 5dc56d8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: node_js
node_js:
- "0.10"
- "0.12"
- "4"
- "6"
- "7"
Expand Down
33 changes: 18 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ var monitor = {
}
var d = (detailed === undefined) ? monitor.detailed : !!detailed;
if (d) {
print(event, cct.cn("connect(") + cct.value(cp.user + "@" + cp.database) + cct.cn(")"));
print(null, event, cct.cn("connect(") + cct.value(cp.user + "@" + cp.database) + cct.cn(")"));
} else {
print(event, cct.cn("connect"));
print(null, event, cct.cn("connect"));
}
},

Expand All @@ -45,9 +45,9 @@ var monitor = {
var d = (detailed === undefined) ? monitor.detailed : !!detailed;
if (d) {
// report user@database details;
print(event, cct.cn("disconnect(") + cct.value(cp.user + "@" + cp.database) + cct.cn(")"));
print(null, event, cct.cn("disconnect(") + cct.value(cp.user + "@" + cp.database) + cct.cn(")"));
} else {
print(event, cct.cn("disconnect"));
print(null, event, cct.cn("disconnect"));
}
},

Expand Down Expand Up @@ -103,13 +103,13 @@ var monitor = {
qText = cct.tx(prefix + ": ") + qText;
}
}
print(event, qText);
print(e, event, qText);
if (e.params) {
var p = e.params;
if (typeof p !== 'string') {
p = JSON.stringify(p);
}
print(event, timeGap + cct.paramTitle("params: ") + cct.value(p), true);
print(e, event, timeGap + cct.paramTitle("params: ") + cct.value(p), true);
}
},

Expand All @@ -136,7 +136,7 @@ var monitor = {
var duration = formatDuration(e.ctx.finish - e.ctx.start);
msg += cct.tx("; duration: ") + cct.value(duration) + cct.tx(", success: ") + cct.value(!!e.ctx.success);
}
print(event, msg);
print(e, event, msg);
},

///////////////////////////////////////////////
Expand All @@ -162,7 +162,7 @@ var monitor = {
var duration = formatDuration(e.ctx.finish - e.ctx.start);
msg += cct.tx("; duration: ") + cct.value(duration) + cct.tx(", success: ") + cct.value(!!e.ctx.success);
}
print(event, msg);
print(e, event, msg);
},

///////////////////////////////////////////////
Expand All @@ -177,7 +177,7 @@ var monitor = {
if (!e || typeof e !== 'object') {
throw new TypeError(errors.redirectParams(event));
}
print(event, cct.errorTitle("error: ") + cct.error(errMsg));
print(e, event, cct.errorTitle("error: ") + cct.error(errMsg));
var q = e.query;
if (q !== undefined && typeof q !== 'string') {
if (typeof q === 'object' && ('name' in q || 'text' in q)) {
Expand All @@ -194,25 +194,25 @@ var monitor = {
}
if (e.cn) {
// a connection issue;
print(event, timeGap + cct.paramTitle("connection: ") + cct.value(JSON.stringify(e.cn)), true);
print(e, event, timeGap + cct.paramTitle("connection: ") + cct.value(JSON.stringify(e.cn)), true);
} else {
if (q !== undefined) {
var d = (detailed === undefined) ? monitor.detailed : !!detailed;
if (d && e.ctx) {
// transaction details are to be reported;
var sTag = getTagName(e), prefix = e.ctx.isTX ? "tx" : "task";
if (sTag) {
print(event, timeGap + cct.paramTitle(prefix + "(") + cct.value(sTag) + cct.paramTitle("): ") + cct.value(q), true);
print(e, event, timeGap + cct.paramTitle(prefix + "(") + cct.value(sTag) + cct.paramTitle("): ") + cct.value(q), true);
} else {
print(event, timeGap + cct.paramTitle(prefix + ": ") + cct.value(q), true);
print(e, event, timeGap + cct.paramTitle(prefix + ": ") + cct.value(q), true);
}
} else {
print(event, timeGap + cct.paramTitle("query: ") + cct.value(q), true);
print(e, event, timeGap + cct.paramTitle("query: ") + cct.value(q), true);
}
}
}
if (e.params) {
print(event, timeGap + cct.paramTitle("params: ") + cct.value(JSON.stringify(e.params)), true);
print(e, event, timeGap + cct.paramTitle("params: ") + cct.value(JSON.stringify(e.params)), true);
}
},

Expand Down Expand Up @@ -417,7 +417,7 @@ var monitor = {

// prints the text on screen, optionally
// notifying the client of the log events;
function print(event, text, isExtraLine) {
function print(e, event, text, isExtraLine) {
var t = null, s = text;
if (!isExtraLine) {
t = new Date();
Expand All @@ -431,6 +431,9 @@ function print(event, text, isExtraLine) {
time: t,
text: removeColors(text).trim()
};
if (e && e.ctx) {
info.ctx = e.ctx;
}
log(removeColors(s), info);
display = info.display === undefined || !!info.display;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-monitor",
"version": "0.7.2",
"version": "0.8.0",
"description": "Event monitor for pg-promise.",
"main": "lib/index.js",
"typings": "typescript/pg-monitor.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion typescript/pg-monitor.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
////////////////////////////////////////
// Requires pg-monitor v0.6.0 or later.
// Requires pg-monitor v0.8.0 or later.
////////////////////////////////////////

interface IColorTheme {
Expand Down

0 comments on commit 5dc56d8

Please sign in to comment.