Skip to content

Commit 3946d21

Browse files
committed
Log level configuration
Previous solution where only the logger function is configured had a potential drawback - all log messages needed to be constructed even when user is only interested in a certain log level. E.g. debug messages that stringify returned values had to be created even if user is only interested in 'info' level. This commit addresses the problem by making the log level configurable. Driver configuration now looks like: ``` const config = { logging: { level: 'debug', logger: (level, message) => console.log(level + ' ' + message) } }; const driver = neo4j.driver( 'bolt://localhost', neo4j.auth.basic('neo4j', 'password'), config ); ``` to enable logging of all currently supported log levels. Levels have priorities like: ``` error: 0 warn: 1 info: 2 debug: 3 ``` Enabling a certain level also enables logging of all levels with lower priorities. When 'info' is enabled, logger will output 'error', 'warn', 'info' but not 'debug'. Level configuration is optional, 'info' is the default value.
1 parent 04caeac commit 3946d21

File tree

6 files changed

+247
-71
lines changed

6 files changed

+247
-71
lines changed

src/v1/index.js

+24-11
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,14 @@ const USER_AGENT = "neo4j-javascript/" + VERSION;
7070

7171
/**
7272
* Object containing predefined logger implementations. These are expected to be used as values of the driver config's <code>logger</code> property.
73-
* @property {function(level: string, message: string)} console the logger function that outputs timestamp, log level and message to <code>console.log()</code>
7473
*/
75-
const logger = {
76-
console: (level, message) => console.log(`${global.Date.now()} ${level.toUpperCase()} ${message}`)
74+
const logging = {
75+
console: level => {
76+
return {
77+
level: level,
78+
logger: (level, message) => console.log(`${global.Date.now()} ${level.toUpperCase()} ${message}`)
79+
};
80+
}
7781
};
7882

7983
/**
@@ -181,12 +185,21 @@ const logger = {
181185
* // in loss of precision in the general case.
182186
* disableLosslessIntegers: false,
183187
*
184-
* // Specify the logger function for this driver. Function should take two arguments:
185-
* // 1) <code>level</code> - string representing the log level. Supported levels are: 'error', 'warn', 'info' and 'debug'
186-
* // 2) <code>message</code> - string representing the log message.
187-
* // Function should not execute any blocking or long-running operations because it is often executed on a hot path.
188-
* // No logging is done by default. See <code>neo4j.logger</code> object that contains predefined logger implementations.
189-
* logger: (level, message) => console.log(level + ' ' + message),
188+
* // Specify the logging configuration for the driver. Object should have two properties <code>level</code> and <code>logger</code>.
189+
* //
190+
* // Property <code>level</code> represents the logging level which should be one of: 'error', 'warn', 'info' or 'debug'. This property is optional and
191+
* // its default value is 'info'. Levels have priorities: 'error': 0, 'warn': 1, 'info': 2, 'debug': 3. Enabling a certain level also enables all
192+
* // levels with lower priority. For example: 'error', 'warn' and 'info' will be logged when 'info' level is configured.
193+
* //
194+
* // Property <code>logger</code> represents the logging function which will be invoked for every log call with an acceptable level. The function should
195+
* // take two string arguments <code>level</code> and <code>message</code>. The function should not execute any blocking or long-running operations
196+
* // because it is often executed on a hot path.
197+
* //
198+
* // No logging is done by default. See <code>neo4j.logging</code> object that contains predefined logging implementations.
199+
* logging: {
200+
* level: 'info',
201+
* logger: (level, message) => console.log(level + ' ' + message)
202+
* },
190203
* }
191204
*
192205
* @param {string} url The URL for the Neo4j database, for instance "bolt://localhost"
@@ -295,7 +308,7 @@ const forExport = {
295308
integer,
296309
Neo4jError,
297310
auth,
298-
logger,
311+
logging,
299312
types,
300313
session,
301314
error,
@@ -317,7 +330,7 @@ export {
317330
integer,
318331
Neo4jError,
319332
auth,
320-
logger,
333+
logging,
321334
types,
322335
session,
323336
error,

src/v1/internal/connector.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class Connection {
145145
this._handleMessage(this._unpacker.unpack(buf));
146146
};
147147

148-
if (this._log.isEnabled()) {
148+
if (this._log.isDebugEnabled()) {
149149
this._log.debug(`${this} created towards ${hostPort}`);
150150
}
151151

@@ -168,7 +168,7 @@ class Connection {
168168
* @private
169169
*/
170170
_initializeProtocol(version, buffer) {
171-
if (this._log.isEnabled()) {
171+
if (this._log.isDebugEnabled()) {
172172
this._log.debug(`${this} negotiated protocol version ${version}`);
173173
}
174174

@@ -196,7 +196,7 @@ class Connection {
196196
this._isBroken = true;
197197
this._error = err;
198198

199-
if (this._log.isEnabled()) {
199+
if (this._log.isErrorEnabled()) {
200200
this._log.error(`${this} experienced a fatal error ${JSON.stringify(err)}`);
201201
}
202202

@@ -222,13 +222,13 @@ class Connection {
222222

223223
switch( msg.signature ) {
224224
case RECORD:
225-
if (this._log.isEnabled()) {
225+
if (this._log.isDebugEnabled()) {
226226
this._log.debug(`${this} S: RECORD ${JSON.stringify(msg)}`);
227227
}
228228
this._currentObserver.onNext( payload );
229229
break;
230230
case SUCCESS:
231-
if (this._log.isEnabled()) {
231+
if (this._log.isDebugEnabled()) {
232232
this._log.debug(`${this} S: SUCCESS ${JSON.stringify(msg)}`);
233233
}
234234
try {
@@ -238,7 +238,7 @@ class Connection {
238238
}
239239
break;
240240
case FAILURE:
241-
if (this._log.isEnabled()) {
241+
if (this._log.isDebugEnabled()) {
242242
this._log.debug(`${this} S: FAILURE ${JSON.stringify(msg)}`);
243243
}
244244
try {
@@ -251,7 +251,7 @@ class Connection {
251251
}
252252
break;
253253
case IGNORED:
254-
if (this._log.isEnabled()) {
254+
if (this._log.isDebugEnabled()) {
255255
this._log.debug(`${this} S: IGNORED ${JSON.stringify(msg)}`);
256256
}
257257
try {
@@ -270,7 +270,7 @@ class Connection {
270270

271271
/** Queue an INIT-message to be sent to the database */
272272
initialize( clientName, token, observer ) {
273-
if (this._log.isEnabled()) {
273+
if (this._log.isDebugEnabled()) {
274274
this._log.debug(`${this} C: INIT ${clientName} ${JSON.stringify(token)}`);
275275
}
276276
const initObserver = this._state.wrap(observer);
@@ -285,7 +285,7 @@ class Connection {
285285

286286
/** Queue a RUN-message to be sent to the database */
287287
run( statement, params, observer ) {
288-
if (this._log.isEnabled()) {
288+
if (this._log.isDebugEnabled()) {
289289
this._log.debug(`${this} C: RUN ${statement} ${JSON.stringify(params)}`);
290290
}
291291
const queued = this._queueObserver(observer);
@@ -298,7 +298,7 @@ class Connection {
298298

299299
/** Queue a PULL_ALL-message to be sent to the database */
300300
pullAll( observer ) {
301-
if (this._log.isEnabled()) {
301+
if (this._log.isDebugEnabled()) {
302302
this._log.debug(`${this} C: PULL_ALL`);
303303
}
304304
const queued = this._queueObserver(observer);
@@ -310,7 +310,7 @@ class Connection {
310310

311311
/** Queue a DISCARD_ALL-message to be sent to the database */
312312
discardAll( observer ) {
313-
if (this._log.isEnabled()) {
313+
if (this._log.isDebugEnabled()) {
314314
this._log.debug(`${this} C: DISCARD_ALL`);
315315
}
316316
const queued = this._queueObserver(observer);
@@ -326,7 +326,7 @@ class Connection {
326326
* @return {Promise<void>} promise resolved when SUCCESS-message response arrives, or failed when other response messages arrives.
327327
*/
328328
resetAndFlush() {
329-
if (this._log.isEnabled()) {
329+
if (this._log.isDebugEnabled()) {
330330
this._log.debug(`${this} C: RESET`);
331331
}
332332
this._ackFailureMuted = true;
@@ -365,7 +365,7 @@ class Connection {
365365
return;
366366
}
367367

368-
if (this._log.isEnabled()) {
368+
if (this._log.isDebugEnabled()) {
369369
this._log.debug(`${this} C: ACK_FAILURE`);
370370
}
371371

@@ -448,7 +448,7 @@ class Connection {
448448
* @param {function} cb - Function to call on close.
449449
*/
450450
close(cb) {
451-
if (this._log.isEnabled()) {
451+
if (this._log.isDebugEnabled()) {
452452
this._log.debug(`${this} closing`);
453453
}
454454
this._ch.close(cb);

0 commit comments

Comments
 (0)