-
Notifications
You must be signed in to change notification settings - Fork 217
Tags
Everything in the library is exposed via a flexible system of events, each providing complete context. Tags extend such context for tasks and transactions, so they can be easily tracked and logged with complete details. Module pg-monitor makes the best use of tags and other details reported by pg-promise
events.
A tag can be set either explicitly, as an option for the corresponding method, or implicitly, with a named callback function. The explicit declaration has the priority and overrides the implicit one.
Explicit direct tagging
An explicit tag can be passed in directly, when it is a simple string or number:
db.task('my-task', t => {
});
db.tx('my-transaction', t => {
});
db.txIf('my-task-or-transaction', t => {
});
Explicit property tagging
The tag can be set explicitly as an option for the method:
db.task({tag: 'my-task'}, t => {
});
db.tx({tag: 'my-transaction'}, t => {
});
db.txIf({tag: 'my-task-or-transaction'}, t => {
});
Implicit tagging
Name of the callback function implicitly sets the tag name:
db.task(function myTask(t) {
});
db.tx(function myTransaction(t) {
});
db.txIf(function myTaskOrTransaction(t) {
});
When handling events task, transact, query and receive you are given the complete context, including the tag
, to allow detailed, context-linked logging.
Example
// Initialization Options;
const options = {
query(e) {
if (e.ctx) {
// e.ctx.tag - is the tag set;
// the query is part of a task or transaction;
if (e.ctx.isTX) {
// log the transaction details;
} else {
// log the task details;
}
}
console.log('QUERY:', e.query);
},
task(e) {
if (e.ctx.finish) {
console.log('Task/Finish:', e.ctx.tag);
} else {
console.log('Task/Start:', e.ctx.tag);
}
},
transact(e) {
if (e.ctx.finish) {
console.log('TX/Finish:', e.ctx.tag);
} else {
console.log('TX/Start:', e.ctx.tag);
}
}
};
In most cases for your tag
you would use a text string for logging, but the tag
can be of any type, the library doesn't care what the tag
really is.
And if you want to use a custom tag
object, while allowing pg-monitor to log it as a text string, add toString
function to the object to return the tag's name for logging.
Handling and formatting all query-related events can be a daunting task, and therefore it is recommended that you use pg-monitor for that.
pg-promise