Skip to content

Commit 0a86183

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

File tree

6 files changed

+250
-72
lines changed

6 files changed

+250
-72
lines changed

src/v1/index.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,17 @@ const auth = {
6969
const USER_AGENT = "neo4j-javascript/" + VERSION;
7070

7171
/**
72-
* 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>
72+
* Object containing predefined logging configurations. These are expected to be used as values of the driver config's <code>logging</code> property.
73+
* @property {function(level: ?string): object} console the function to create a logging config that prints all messages to <code>console.log</code> with
74+
* timestamp, level and message. It takes an optional <code>level</code> parameter which represents the maximum log level to be logged. Default value is 'info'.
7475
*/
75-
const logger = {
76-
console: (level, message) => console.log(`${global.Date.now()} ${level.toUpperCase()} ${message}`)
76+
const logging = {
77+
console: level => {
78+
return {
79+
level: level,
80+
logger: (level, message) => console.log(`${global.Date.now()} ${level.toUpperCase()} ${message}`)
81+
};
82+
}
7783
};
7884

7985
/**
@@ -181,12 +187,21 @@ const logger = {
181187
* // in loss of precision in the general case.
182188
* disableLosslessIntegers: false,
183189
*
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),
190+
* // Specify the logging configuration for the driver. Object should have two properties <code>level</code> and <code>logger</code>.
191+
* //
192+
* // Property <code>level</code> represents the logging level which should be one of: 'error', 'warn', 'info' or 'debug'. This property is optional and
193+
* // its default value is 'info'. Levels have priorities: 'error': 0, 'warn': 1, 'info': 2, 'debug': 3. Enabling a certain level also enables all
194+
* // levels with lower priority. For example: 'error', 'warn' and 'info' will be logged when 'info' level is configured.
195+
* //
196+
* // Property <code>logger</code> represents the logging function which will be invoked for every log call with an acceptable level. The function should
197+
* // take two string arguments <code>level</code> and <code>message</code>. The function should not execute any blocking or long-running operations
198+
* // because it is often executed on a hot path.
199+
* //
200+
* // No logging is done by default. See <code>neo4j.logging</code> object that contains predefined logging implementations.
201+
* logging: {
202+
* level: 'info',
203+
* logger: (level, message) => console.log(level + ' ' + message)
204+
* },
190205
* }
191206
*
192207
* @param {string} url The URL for the Neo4j database, for instance "bolt://localhost"
@@ -295,7 +310,7 @@ const forExport = {
295310
integer,
296311
Neo4jError,
297312
auth,
298-
logger,
313+
logging,
299314
types,
300315
session,
301316
error,
@@ -317,7 +332,7 @@ export {
317332
integer,
318333
Neo4jError,
319334
auth,
320-
logger,
335+
logging,
321336
types,
322337
session,
323338
error,

src/v1/internal/connector.js

Lines changed: 14 additions & 14 deletions
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)