Skip to content

Commit aae924b

Browse files
committed
Logger
1 parent f942480 commit aae924b

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

index.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,55 @@ module.exports = class TDatabase {
111111
///
112112
/// Constructor
113113
///
114-
constructor(config) {
114+
constructor(config, params) {
115115
this.config = config;
116+
this.params = params;
116117
this.connection = undefined;
117118
this.last_identity = 0;
118119
this.last_fields = {}
119120
if(this.config.hasOwnProperty('connectionLimit'))
120121
this.pool = mysql.createPool(this.config);
121122
}
122123

124+
///
125+
/// Log Error
126+
///
127+
logError(message) {
128+
if(this.params && this.params.logger)
129+
this.params.logger.error(message);
130+
}
131+
132+
///
133+
/// Log Info
134+
///
135+
logInfo(message) {
136+
if(this.params && this.params.logger)
137+
this.params.logger.info(message);
138+
}
139+
140+
///
141+
/// Log Debug
142+
///
143+
logDebug(message) {
144+
if(this.params && this.params.logger)
145+
this.params.logger.debug(message);
146+
}
147+
123148
///
124149
/// Connect to the database
125150
///
126151
connect() {
127152
return new Promise((resolve, reject) => {
128153
this.connection = mysql.createConnection(this.config);
129154
this.connection.connect((err) => {
130-
if (err)
155+
if (err) {
156+
this.logError(err.message);
131157
reject(err);
132-
else
158+
}
159+
else {
160+
this.logInfo('TDatabase.Connected');
133161
resolve();
162+
}
134163
});
135164
});
136165
}
@@ -171,6 +200,10 @@ module.exports = class TDatabase {
171200
else {
172201
if (this.connection) {
173202
this.connection.end((err) => {
203+
if(err)
204+
this.logError(err.message);
205+
else
206+
this.logDebug('TDatabase.Disconnected')
174207
});
175208
}
176209
this.connection = undefined;
@@ -198,13 +231,20 @@ module.exports = class TDatabase {
198231
/// Query
199232
///
200233
query(sql, params) {
201-
var self = this;
202-
return new Promise(function(resolve, reject) {
203-
self.connection.query(sql, params, function(err, results, fields) {
204-
if(err)
234+
this.logDebug('TDatabase.query: ' + sql);
235+
if(params)
236+
params.forEach((param) => this.logDebug(` - ${param}`));
237+
let hrstart = process.hrtime();
238+
return new Promise((resolve, reject) => {
239+
this.connection.query(sql, params, (err, results, fields) => {
240+
if(err) {
241+
this.logError(err.message);
205242
reject(err);
243+
}
206244
else {
207-
self.last_fields = fields;
245+
let hrend = process.hrtime(hrstart);
246+
this.logDebug(`Count: ${results.length}, (${hrend[0]}s ${hrend[1] / 1000000 | 0}ms)`);
247+
this.last_fields = fields;
208248
resolve(results);
209249
}
210250
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mysql-functions",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)