Skip to content

Commit 83c7b19

Browse files
authored
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

+41-17
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

+4-4
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

+1-1
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

+1-1
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

+18-14
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

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
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",

0 commit comments

Comments
 (0)