Skip to content

feat(nodejs): custom logging #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions docs/Sender.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ <h5>Parameters:</h5>
Optional, defaults to false </li>
<li>jwk: <i>{x: string, y: string, kid: string, kty: string, d: string, crv: string}</i> - JsonWebKey for authentication. <br>
If not provided, client is not authenticated and server might reject the connection depending on configuration.</li>
<li>log: <i>(level: 'error'|'warn'|'info'|'debug', message: string) => void</i> - logging function. <br>
If not provided, default logging is used which writes to the console with logging level 'info'.</li>
</ul>
</p></td>
</tr>
Expand Down Expand Up @@ -168,7 +170,7 @@ <h5>Parameters:</h5>

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


Expand Down Expand Up @@ -325,7 +327,7 @@ <h5>Parameters:</h5>

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


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

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


Expand Down Expand Up @@ -574,7 +576,7 @@ <h5>Parameters:</h5>

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


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

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


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



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



Expand Down Expand Up @@ -880,7 +882,7 @@ <h5>Parameters:</h5>

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


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



<h5>Returns:</h5>


<div class="param-desc">
Resolves to true if client is connected.
</div>



<dl>
<dt>
Type
</dt>
<dd>

<span class="param-type">Promise.&lt;boolean></span>


</dd>
</dl>





Expand Down Expand Up @@ -1040,7 +1064,7 @@ <h5>Parameters:</h5>

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


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

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


Expand Down Expand Up @@ -1333,7 +1357,7 @@ <h5>Parameters:</h5>

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


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

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


Expand Down Expand Up @@ -1605,7 +1629,7 @@ <h5>Parameters:</h5>

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


Expand Down Expand Up @@ -1765,7 +1789,7 @@ <h5>Parameters:</h5>

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


Expand Down Expand Up @@ -1947,7 +1971,7 @@ <h5>Parameters:</h5>

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


Expand Down Expand Up @@ -2106,7 +2130,7 @@ <h5>Parameters:</h5>

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


Expand Down Expand Up @@ -2288,7 +2312,7 @@ <h5>Parameters:</h5>

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


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

<footer>
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)
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)
</footer>

<script> prettyPrint(); </script>
Expand Down
8 changes: 4 additions & 4 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ <h3>Basic API usage</h3>

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

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

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

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

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

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

<footer>
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)
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)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
<br class="clear">

<footer>
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)
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)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/module-@questdb_nodejs-client.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
<br class="clear">

<footer>
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)
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)
</footer>

<script> prettyPrint(); </script>
Expand Down
32 changes: 18 additions & 14 deletions docs/src_sender.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ <h1 class="page-title">Source: src/sender.js</h1>
<pre class="prettyprint source linenums"><code>'use strict';

const { Buffer } = require("buffer");
const { log } = require("./logging");
const { validateTableName, validateColumnName, validateDesignatedTimestamp } = require("./validation");
const net = require("net");
const tls = require("tls");
Expand Down Expand Up @@ -62,6 +63,7 @@ <h1 class="page-title">Source: src/sender.js</h1>
/** @private */ hasTable;
/** @private */ hasSymbols;
/** @private */ hasColumns;
/** @private */ log;

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

return new Promise((resolve, reject) => {
Expand All @@ -156,13 +163,13 @@ <h1 class="page-title">Source: src/sender.js</h1>
resolve(true);
}
} else {
console.warn(`Received unexpected data: ${data}`);
this.log("warn", `Received unexpected data: ${data}`);
}
})
.on("ready", async () => {
console.info(`Successfully connected to ${options.host}:${options.port}`);
this.log("info", `Successfully connected to ${options.host}:${options.port}`);
if (self.jwk) {
console.info(`Authenticating with ${options.host}:${options.port}`);
this.log("info", `Authenticating with ${options.host}:${options.port}`);
await self.socket.write(`${self.jwk.kid}\n`, err => {
if (err) {
reject(err);
Expand All @@ -174,7 +181,7 @@ <h1 class="page-title">Source: src/sender.js</h1>
}
})
.on("error", err => {
console.error(err);
this.log("error", err);
reject(err);
});
});
Expand All @@ -185,13 +192,10 @@ <h1 class="page-title">Source: src/sender.js</h1>
* Data sitting in the Sender's buffer will be lost unless flush() is called before close().
*/
async close() {
return new Promise(resolve => {
const address = this.socket.remoteAddress;
const port = this.socket.remotePort;
this.socket.destroy();
console.info(`Connection to ${address}:${port} is closed`);
resolve();
});
const address = this.socket.remoteAddress;
const port = this.socket.remotePort;
this.socket.destroy();
this.log("info", `Connection to ${address}:${port} is closed`);
}

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

<footer>
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)
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)
</footer>

<script> prettyPrint(); </script>
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@questdb/nodejs-client",
"version": "1.0.1",
"version": "1.0.2",
"description": "QuestDB Node.js Client",
"main": "index.js",
"types": "types/index.d.ts",
Expand Down
Loading