Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 83c7b19

Browse files
authoredJan 5, 2023
feat(nodejs): custom logging (#15)
* feat(nodejs): custom logging * feat(nodejs): custom logging - ts and doc * feat(nodejs): custom logging - v1.0.2 * chore(nodejs): improved doc * Sender::close() to return Promise<void>
1 parent 455d2ef commit 83c7b19

15 files changed

+231
-62
lines changed
 

‎docs/Sender.html

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ <h5>Parameters:</h5>
126126
Optional, defaults to false </li>
127127
<li>jwk: <i>{x: string, y: string, kid: string, kty: string, d: string, crv: string}</i> - JsonWebKey for authentication. <br>
128128
If not provided, client is not authenticated and server might reject the connection depending on configuration.</li>
129+
<li>log: <i>(level: 'error'|'warn'|'info'|'debug', message: string) => void</i> - logging function. <br>
130+
If not provided, default logging is used which writes to the console with logging level 'info'.</li>
129131
</ul>
130132
</p></td>
131133
</tr>
@@ -168,7 +170,7 @@ <h5>Parameters:</h5>
168170

169171
<dt class="tag-source">Source:</dt>
170172
<dd class="tag-source"><ul class="dummy"><li>
171-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line24">line 24</a>
173+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line25">line 25</a>
172174
</li></ul></dd>
173175

174176

@@ -325,7 +327,7 @@ <h5>Parameters:</h5>
325327

326328
<dt class="tag-source">Source:</dt>
327329
<dd class="tag-source"><ul class="dummy"><li>
328-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line351">line 351</a>
330+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line355">line 355</a>
329331
</li></ul></dd>
330332

331333

@@ -414,7 +416,7 @@ <h4 class="name" id="atNow"><span class="type-signature"></span>atNow<span class
414416

415417
<dt class="tag-source">Source:</dt>
416418
<dd class="tag-source"><ul class="dummy"><li>
417-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line370">line 370</a>
419+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line374">line 374</a>
418420
</li></ul></dd>
419421

420422

@@ -574,7 +576,7 @@ <h5>Parameters:</h5>
574576

575577
<dt class="tag-source">Source:</dt>
576578
<dd class="tag-source"><ul class="dummy"><li>
577-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line282">line 282</a>
579+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line286">line 286</a>
578580
</li></ul></dd>
579581

580582

@@ -685,7 +687,7 @@ <h4 class="name" id="close"><span class="type-signature">(async) </span>close<sp
685687

686688
<dt class="tag-source">Source:</dt>
687689
<dd class="tag-source"><ul class="dummy"><li>
688-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line159">line 159</a>
690+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line166">line 166</a>
689691
</li></ul></dd>
690692

691693

@@ -721,7 +723,7 @@ <h4 class="name" id="close"><span class="type-signature">(async) </span>close<sp
721723

722724

723725

724-
<h4 class="name" id="connect"><span class="type-signature">(async) </span>connect<span class="signature">(options, secure<span class="signature-attributes">opt</span>)</span><span class="type-signature"></span></h4>
726+
<h4 class="name" id="connect"><span class="type-signature"></span>connect<span class="signature">(options, secure<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {Promise.&lt;boolean>}</span></h4>
725727

726728

727729

@@ -880,7 +882,7 @@ <h5>Parameters:</h5>
880882

881883
<dt class="tag-source">Source:</dt>
882884
<dd class="tag-source"><ul class="dummy"><li>
883-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line104">line 104</a>
885+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line111">line 111</a>
884886
</li></ul></dd>
885887

886888

@@ -905,6 +907,28 @@ <h5>Parameters:</h5>
905907

906908

907909

910+
<h5>Returns:</h5>
911+
912+
913+
<div class="param-desc">
914+
Resolves to true if client is connected.
915+
</div>
916+
917+
918+
919+
<dl>
920+
<dt>
921+
Type
922+
</dt>
923+
<dd>
924+
925+
<span class="param-type">Promise.&lt;boolean></span>
926+
927+
928+
</dd>
929+
</dl>
930+
931+
908932

909933

910934

@@ -1040,7 +1064,7 @@ <h5>Parameters:</h5>
10401064

10411065
<dt class="tag-source">Source:</dt>
10421066
<dd class="tag-source"><ul class="dummy"><li>
1043-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line297">line 297</a>
1067+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line301">line 301</a>
10441068
</li></ul></dd>
10451069

10461070

@@ -1151,7 +1175,7 @@ <h4 class="name" id="flush"><span class="type-signature">(async) </span>flush<sp
11511175

11521176
<dt class="tag-source">Source:</dt>
11531177
<dd class="tag-source"><ul class="dummy"><li>
1154-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line175">line 175</a>
1178+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line179">line 179</a>
11551179
</li></ul></dd>
11561180

11571181

@@ -1333,7 +1357,7 @@ <h5>Parameters:</h5>
13331357

13341358
<dt class="tag-source">Source:</dt>
13351359
<dd class="tag-source"><ul class="dummy"><li>
1336-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line313">line 313</a>
1360+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line317">line 317</a>
13371361
</li></ul></dd>
13381362

13391363

@@ -1444,7 +1468,7 @@ <h4 class="name" id="reset"><span class="type-signature"></span>reset<span class
14441468

14451469
<dt class="tag-source">Source:</dt>
14461470
<dd class="tag-source"><ul class="dummy"><li>
1447-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line92">line 92</a>
1471+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line97">line 97</a>
14481472
</li></ul></dd>
14491473

14501474

@@ -1605,7 +1629,7 @@ <h5>Parameters:</h5>
16051629

16061630
<dt class="tag-source">Source:</dt>
16071631
<dd class="tag-source"><ul class="dummy"><li>
1608-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line77">line 77</a>
1632+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line82">line 82</a>
16091633
</li></ul></dd>
16101634

16111635

@@ -1765,7 +1789,7 @@ <h5>Parameters:</h5>
17651789

17661790
<dt class="tag-source">Source:</dt>
17671791
<dd class="tag-source"><ul class="dummy"><li>
1768-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line265">line 265</a>
1792+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line269">line 269</a>
17691793
</li></ul></dd>
17701794

17711795

@@ -1947,7 +1971,7 @@ <h5>Parameters:</h5>
19471971

19481972
<dt class="tag-source">Source:</dt>
19491973
<dd class="tag-source"><ul class="dummy"><li>
1950-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line240">line 240</a>
1974+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line244">line 244</a>
19511975
</li></ul></dd>
19521976

19531977

@@ -2106,7 +2130,7 @@ <h5>Parameters:</h5>
21062130

21072131
<dt class="tag-source">Source:</dt>
21082132
<dd class="tag-source"><ul class="dummy"><li>
2109-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line219">line 219</a>
2133+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line223">line 223</a>
21102134
</li></ul></dd>
21112135

21122136

@@ -2288,7 +2312,7 @@ <h5>Parameters:</h5>
22882312

22892313
<dt class="tag-source">Source:</dt>
22902314
<dd class="tag-source"><ul class="dummy"><li>
2291-
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line333">line 333</a>
2315+
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line337">line 337</a>
22922316
</li></ul></dd>
22932317

22942318

@@ -2362,7 +2386,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
23622386
<br class="clear">
23632387

23642388
<footer>
2365-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Sep 21 2022 10:00:52 GMT+0100 (British Summer Time)
2389+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Jan 04 2023 19:03:57 GMT+0000 (Greenwich Mean Time)
23662390
</footer>
23672391

23682392
<script> prettyPrint(); </script>

‎docs/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ <h3>Basic API usage</h3>
5858

5959
// connect to QuestDB
6060
// host and port are required in connect options
61-
await sender.connect({port: 9009, host: &quot;127.0.0.1&quot;});
61+
await sender.connect({port: 9009, host: &quot;localhost&quot;});
6262

6363
// add rows to the buffer of the sender
6464
sender.table(&quot;prices&quot;).symbol(&quot;instrument&quot;, &quot;EURUSD&quot;)
@@ -107,7 +107,7 @@ <h3>Authentication and secure connection</h3>
107107

108108
// connect() takes an optional second argument
109109
// if 'true' passed the connection is secured with TLS encryption
110-
await sender.connect({port: 9009, host: &quot;127.0.0.1&quot;}, true);
110+
await sender.connect({port: 9009, host: &quot;localhost&quot;}, true);
111111

112112
// send the data over the authenticated and secure connection
113113
sender.table(&quot;prices&quot;).symbol(&quot;instrument&quot;, &quot;EURUSD&quot;)
@@ -146,7 +146,7 @@ <h3>TypeScript example</h3>
146146

147147
// connect() takes an optional second argument
148148
// if 'true' passed the connection is secured with TLS encryption
149-
await sender.connect({port: 9009, host: &quot;127.0.0.1&quot;}, true);
149+
await sender.connect({port: 9009, host: &quot;localhost&quot;}, true);
150150

151151
// send the data over the authenticated and secure connection
152152
sender.table(&quot;prices&quot;).symbol(&quot;instrument&quot;, &quot;EURUSD&quot;)
@@ -176,7 +176,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
176176
<br class="clear">
177177

178178
<footer>
179-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Sep 21 2022 10:00:52 GMT+0100 (British Summer Time)
179+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Jan 04 2023 19:03:57 GMT+0000 (Greenwich Mean Time)
180180
</footer>
181181

182182
<script> prettyPrint(); </script>

‎docs/index.js.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
5353
<br class="clear">
5454

5555
<footer>
56-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Sep 21 2022 10:00:52 GMT+0100 (British Summer Time)
56+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Jan 04 2023 19:03:57 GMT+0000 (Greenwich Mean Time)
5757
</footer>
5858

5959
<script> prettyPrint(); </script>

‎docs/module-@questdb_nodejs-client.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
156156
<br class="clear">
157157

158158
<footer>
159-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Sep 21 2022 10:00:52 GMT+0100 (British Summer Time)
159+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Jan 04 2023 19:03:57 GMT+0000 (Greenwich Mean Time)
160160
</footer>
161161

162162
<script> prettyPrint(); </script>

‎docs/src_sender.js.html

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ <h1 class="page-title">Source: src/sender.js</h1>
2929
<pre class="prettyprint source linenums"><code>'use strict';
3030

3131
const { Buffer } = require("buffer");
32+
const { log } = require("./logging");
3233
const { validateTableName, validateColumnName, validateDesignatedTimestamp } = require("./validation");
3334
const net = require("net");
3435
const tls = require("tls");
@@ -62,6 +63,7 @@ <h1 class="page-title">Source: src/sender.js</h1>
6263
/** @private */ hasTable;
6364
/** @private */ hasSymbols;
6465
/** @private */ hasColumns;
66+
/** @private */ log;
6567

6668
/**
6769
* Creates an instance of Sender.
@@ -77,6 +79,8 @@ <h1 class="page-title">Source: src/sender.js</h1>
7779
* Optional, defaults to false &lt;/li>
7880
* &lt;li>jwk: &lt;i>{x: string, y: string, kid: string, kty: string, d: string, crv: string}&lt;/i> - JsonWebKey for authentication. &lt;br>
7981
* If not provided, client is not authenticated and server might reject the connection depending on configuration.&lt;/li>
82+
* &lt;li>log: &lt;i>(level: 'error'|'warn'|'info'|'debug', message: string) => void&lt;/i> - logging function. &lt;br>
83+
* If not provided, default logging is used which writes to the console with logging level 'info'.&lt;/li>
8084
* &lt;/ul>
8185
* &lt;/p>
8286
*/
@@ -90,7 +94,8 @@ <h1 class="page-title">Source: src/sender.js</h1>
9094
: resolve => {
9195
compact(this);
9296
resolve(true);
93-
}
97+
};
98+
this.log = options &amp;&amp; options.log ? options.log : log;
9499
this.resize(options &amp;&amp; options.bufferSize ? options.bufferSize : DEFAULT_BUFFER_SIZE);
95100
this.reset();
96101
}
@@ -128,8 +133,10 @@ <h1 class="page-title">Source: src/sender.js</h1>
128133
*
129134
* @param {net.NetConnectOpts | tls.ConnectionOptions} options - Connection options, host and port are required.
130135
* @param {boolean} [secure = false] - If true connection will use TLS encryption.
136+
*
137+
* @return {Promise&lt;boolean>} Resolves to true if client is connected.
131138
*/
132-
async connect(options, secure = false) {
139+
connect(options, secure = false) {
133140
let self = this;
134141

135142
return new Promise((resolve, reject) => {
@@ -156,13 +163,13 @@ <h1 class="page-title">Source: src/sender.js</h1>
156163
resolve(true);
157164
}
158165
} else {
159-
console.warn(`Received unexpected data: ${data}`);
166+
this.log("warn", `Received unexpected data: ${data}`);
160167
}
161168
})
162169
.on("ready", async () => {
163-
console.info(`Successfully connected to ${options.host}:${options.port}`);
170+
this.log("info", `Successfully connected to ${options.host}:${options.port}`);
164171
if (self.jwk) {
165-
console.info(`Authenticating with ${options.host}:${options.port}`);
172+
this.log("info", `Authenticating with ${options.host}:${options.port}`);
166173
await self.socket.write(`${self.jwk.kid}\n`, err => {
167174
if (err) {
168175
reject(err);
@@ -174,7 +181,7 @@ <h1 class="page-title">Source: src/sender.js</h1>
174181
}
175182
})
176183
.on("error", err => {
177-
console.error(err);
184+
this.log("error", err);
178185
reject(err);
179186
});
180187
});
@@ -185,13 +192,10 @@ <h1 class="page-title">Source: src/sender.js</h1>
185192
* Data sitting in the Sender's buffer will be lost unless flush() is called before close().
186193
*/
187194
async close() {
188-
return new Promise(resolve => {
189-
const address = this.socket.remoteAddress;
190-
const port = this.socket.remotePort;
191-
this.socket.destroy();
192-
console.info(`Connection to ${address}:${port} is closed`);
193-
resolve();
194-
});
195+
const address = this.socket.remoteAddress;
196+
const port = this.socket.remotePort;
197+
this.socket.destroy();
198+
this.log("info", `Connection to ${address}:${port} is closed`);
195199
}
196200

197201
/**
@@ -535,7 +539,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
535539
<br class="clear">
536540

537541
<footer>
538-
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Sep 21 2022 10:00:52 GMT+0100 (British Summer Time)
542+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Jan 04 2023 19:03:57 GMT+0000 (Greenwich Mean Time)
539543
</footer>
540544

541545
<script> prettyPrint(); </script>

‎package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@questdb/nodejs-client",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "QuestDB Node.js Client",
55
"main": "index.js",
66
"types": "types/index.d.ts",

‎src/logging.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const LOG_LEVELS = {
4+
error: { log: console.error, criticality: 3 },
5+
warn: { log: console.warn, criticality: 2 },
6+
info: { log: console.info, criticality: 1 },
7+
debug: { log: console.debug, criticality: 0 }
8+
};
9+
10+
const DEFAULT_CRITICALITY = LOG_LEVELS.info.criticality;
11+
12+
/**
13+
* Simple logger to write log messages to the console. <br>
14+
* Supported logging levels are `error`, `warn`, `info` and `debug`. <br>
15+
* Throws an error if logging level is invalid.
16+
*
17+
* @param {'error'|'warn'|'info'|'debug'} level - The log level of the message.
18+
* @param {string} message - The log message.
19+
*/
20+
function log(level, message) {
21+
const logLevel = LOG_LEVELS[level];
22+
if (!logLevel) {
23+
throw new Error(`Invalid log level: '${level}'`);
24+
}
25+
if (logLevel.criticality >= DEFAULT_CRITICALITY) {
26+
logLevel.log(message);
27+
}
28+
}
29+
30+
exports.log = log;

‎src/sender.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const { Buffer } = require("buffer");
4+
const { log } = require("./logging");
45
const { validateTableName, validateColumnName, validateDesignatedTimestamp } = require("./validation");
56
const net = require("net");
67
const tls = require("tls");
@@ -34,6 +35,7 @@ class Sender {
3435
/** @private */ hasTable;
3536
/** @private */ hasSymbols;
3637
/** @private */ hasColumns;
38+
/** @private */ log;
3739

3840
/**
3941
* Creates an instance of Sender.
@@ -49,6 +51,8 @@ class Sender {
4951
* Optional, defaults to false </li>
5052
* <li>jwk: <i>{x: string, y: string, kid: string, kty: string, d: string, crv: string}</i> - JsonWebKey for authentication. <br>
5153
* If not provided, client is not authenticated and server might reject the connection depending on configuration.</li>
54+
* <li>log: <i>(level: 'error'|'warn'|'info'|'debug', message: string) => void</i> - logging function. <br>
55+
* If not provided, default logging is used which writes to the console with logging level 'info'.</li>
5256
* </ul>
5357
* </p>
5458
*/
@@ -62,7 +66,8 @@ class Sender {
6266
: resolve => {
6367
compact(this);
6468
resolve(true);
65-
}
69+
};
70+
this.log = options && options.log ? options.log : log;
6671
this.resize(options && options.bufferSize ? options.bufferSize : DEFAULT_BUFFER_SIZE);
6772
this.reset();
6873
}
@@ -100,6 +105,8 @@ class Sender {
100105
*
101106
* @param {net.NetConnectOpts | tls.ConnectionOptions} options - Connection options, host and port are required.
102107
* @param {boolean} [secure = false] - If true connection will use TLS encryption.
108+
*
109+
* @return {Promise<boolean>} Resolves to true if client is connected.
103110
*/
104111
connect(options, secure = false) {
105112
let self = this;
@@ -128,13 +135,13 @@ class Sender {
128135
resolve(true);
129136
}
130137
} else {
131-
console.warn(`Received unexpected data: ${data}`);
138+
this.log("warn", `Received unexpected data: ${data}`);
132139
}
133140
})
134141
.on("ready", async () => {
135-
console.info(`Successfully connected to ${options.host}:${options.port}`);
142+
this.log("info", `Successfully connected to ${options.host}:${options.port}`);
136143
if (self.jwk) {
137-
console.info(`Authenticating with ${options.host}:${options.port}`);
144+
this.log("info", `Authenticating with ${options.host}:${options.port}`);
138145
await self.socket.write(`${self.jwk.kid}\n`, err => {
139146
if (err) {
140147
reject(err);
@@ -146,7 +153,7 @@ class Sender {
146153
}
147154
})
148155
.on("error", err => {
149-
console.error(err);
156+
this.log("error", err);
150157
reject(err);
151158
});
152159
});
@@ -157,13 +164,10 @@ class Sender {
157164
* Data sitting in the Sender's buffer will be lost unless flush() is called before close().
158165
*/
159166
async close() {
160-
return new Promise(resolve => {
161-
const address = this.socket.remoteAddress;
162-
const port = this.socket.remotePort;
163-
this.socket.destroy();
164-
console.info(`Connection to ${address}:${port} is closed`);
165-
resolve();
166-
});
167+
const address = this.socket.remoteAddress;
168+
const port = this.socket.remotePort;
169+
this.socket.destroy();
170+
this.log("info", `Connection to ${address}:${port} is closed`);
167171
}
168172

169173
/**

‎test/logging.test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
describe('Default logging suite', function () {
4+
let error, warn, info, debug;
5+
let log;
6+
7+
beforeAll(() => {
8+
error = jest.spyOn(console, "error").mockImplementation(() => {});
9+
warn = jest.spyOn(console, "warn").mockImplementation(() => {});
10+
info = jest.spyOn(console, "info").mockImplementation(() => {});
11+
debug = jest.spyOn(console, "debug").mockImplementation(() => {});
12+
log = require("../src/logging").log;
13+
});
14+
15+
afterAll(() => {
16+
error.mockReset();
17+
warn.mockReset();
18+
info.mockReset();
19+
debug.mockReset();
20+
});
21+
22+
afterEach(() => {
23+
error.mockClear();
24+
warn.mockClear();
25+
info.mockClear();
26+
debug.mockClear();
27+
});
28+
29+
it('can log error level messages', function () {
30+
const testMessage = "ERROR ERROR ERROR";
31+
log("error", testMessage);
32+
expect(error).toHaveBeenCalledTimes(1);
33+
expect(warn).toHaveBeenCalledTimes(0);
34+
expect(info).toHaveBeenCalledTimes(0);
35+
expect(debug).toHaveBeenCalledTimes(0);
36+
expect(error).toHaveBeenCalledWith(testMessage);
37+
});
38+
39+
it('can log warn level messages', function () {
40+
const testMessage = "WARN WARN WARN";
41+
log("warn", testMessage);
42+
expect(error).toHaveBeenCalledTimes(0);
43+
expect(warn).toHaveBeenCalledTimes(1);
44+
expect(info).toHaveBeenCalledTimes(0);
45+
expect(debug).toHaveBeenCalledTimes(0);
46+
expect(warn).toHaveBeenCalledWith(testMessage);
47+
});
48+
49+
it('can log info level messages', function () {
50+
const testMessage = "INFO INFO INFO";
51+
log("info", testMessage);
52+
expect(error).toHaveBeenCalledTimes(0);
53+
expect(warn).toHaveBeenCalledTimes(0);
54+
expect(info).toHaveBeenCalledTimes(1);
55+
expect(debug).toHaveBeenCalledTimes(0);
56+
expect(info).toHaveBeenCalledWith(testMessage);
57+
});
58+
59+
it('cannot log debug level messages', function () {
60+
const testMessage = "DEBUG DEBUG DEBUG";
61+
log("debug", testMessage);
62+
expect(error).toHaveBeenCalledTimes(0);
63+
expect(warn).toHaveBeenCalledTimes(0);
64+
expect(info).toHaveBeenCalledTimes(0);
65+
expect(debug).toHaveBeenCalledTimes(0);
66+
});
67+
68+
it('throws exception if log level is not supported', function () {
69+
expect(
70+
() => log("trace", "TRACE TRACE TRACE")
71+
).toThrow("Invalid log level: 'trace'");
72+
});
73+
});

‎test/sender.test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const proxyOptions = {
2828
const PRIVATE_KEY = "9b9x5WhJywDEuo1KGQWSPNxtX-6X6R2BRCKhYMMY6n8"
2929
const PUBLIC_KEY = {
3030
x: "aultdA0PjhD_cWViqKKyL5chm6H1n-BiZBo_48T-uqc",
31-
y:"__ptaol41JWSpTTL525yVEfzmY8A6Vi_QrW1FjKcHMg"
31+
y: "__ptaol41JWSpTTL525yVEfzmY8A6Vi_QrW1FjKcHMg"
3232
}
3333
const JWK = {
3434
...PUBLIC_KEY,
@@ -168,6 +168,24 @@ describe('Sender connection suite', function () {
168168
await assertSentData(proxy, true, "testapp\n");
169169
await proxy.stop();
170170
});
171+
172+
it('supports custom logger', async function () {
173+
const expectedMessages = [
174+
"Successfully connected to 127.0.0.1:9099",
175+
"Connection to 127.0.0.1:9099 is closed"
176+
];
177+
const log = (level, message) => {
178+
expect(level).toBe("info");
179+
expect(message).toBe(expectedMessages.shift());
180+
};
181+
const proxy = await createProxy();
182+
const sender = new Sender({bufferSize: 1024, log: log});
183+
await sender.connect(senderOptions);
184+
await sendData(sender);
185+
await sender.close();
186+
await assertSentData(proxy, false, "test,location=us temperature=17.1 1658484765000000000\n");
187+
await proxy.stop();
188+
});
171189
});
172190

173191
describe('Client interop test suite', function () {

‎types/src/logging.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Simple logger to write log messages to the console. <br>
3+
* Supported logging levels are `error`, `warn`, `info` and `debug`. <br>
4+
* Throws an error if logging level is invalid.
5+
*
6+
* @param {'error'|'warn'|'info'|'debug'} level - The log level of the message.
7+
* @param {string} message - The log message.
8+
*/
9+
export function log(level: 'error' | 'warn' | 'info' | 'debug', message: string): void;
10+
//# sourceMappingURL=logging.d.ts.map

‎types/src/logging.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎types/src/sender.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export class Sender {
2929
* Optional, defaults to false </li>
3030
* <li>jwk: <i>{x: string, y: string, kid: string, kty: string, d: string, crv: string}</i> - JsonWebKey for authentication. <br>
3131
* If not provided, client is not authenticated and server might reject the connection depending on configuration.</li>
32+
* <li>log: <i>(level: 'error'|'warn'|'info'|'debug', message: string) => void</i> - logging function. <br>
33+
* If not provided, default logging is used which writes to the console with logging level 'info'.</li>
3234
* </ul>
3335
* </p>
3436
*/
@@ -44,6 +46,7 @@ export class Sender {
4446
/** @private */ private hasTable;
4547
/** @private */ private hasSymbols;
4648
/** @private */ private hasColumns;
49+
/** @private */ private log;
4750
/**
4851
* Extends the size of the sender's buffer. <br>
4952
* Can be used to increase the size of buffer if overflown.
@@ -64,13 +67,15 @@ export class Sender {
6467
*
6568
* @param {net.NetConnectOpts | tls.ConnectionOptions} options - Connection options, host and port are required.
6669
* @param {boolean} [secure = false] - If true connection will use TLS encryption.
70+
*
71+
* @return {Promise<boolean>} Resolves to true if client is connected.
6772
*/
68-
connect(options: net.NetConnectOpts | tls.ConnectionOptions, secure?: boolean): Promise<any>;
73+
connect(options: net.NetConnectOpts | tls.ConnectionOptions, secure?: boolean): Promise<boolean>;
6974
/**
7075
* Closes the connection to the database. <br>
7176
* Data sitting in the Sender's buffer will be lost unless flush() is called before close().
7277
*/
73-
close(): Promise<any>;
78+
close(): Promise<void>;
7479
/**
7580
* Sends the buffer's content to the database and compacts the buffer.
7681
* If the last row is not finished it stays in the sender's buffer.

‎types/src/sender.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.