18
18
*/
19
19
20
20
import { Chunker , Dechunker , ChannelConfig , Channel } from '../channel'
21
- import { newError , error , json } from 'neo4j-driver-core'
21
+ import { newError , error , json , internal } from 'neo4j-driver-core'
22
22
import Connection from './connection'
23
23
import Bolt from '../bolt'
24
24
25
25
const { PROTOCOL_ERROR } = error
26
+ const {
27
+ logger : { Logger }
28
+ } = internal
26
29
27
30
let idGenerator = 0
28
31
@@ -57,15 +60,14 @@ export function createChannelConnection (
57
60
const createProtocol = conn =>
58
61
Bolt . create ( {
59
62
version,
60
- connection : conn ,
61
63
channel,
62
64
chunker,
63
65
dechunker,
64
66
disableLosslessIntegers : config . disableLosslessIntegers ,
65
67
useBigInt : config . useBigInt ,
66
68
serversideRouting,
67
69
server : conn . server ,
68
- log,
70
+ log : conn . logger ,
69
71
observer : {
70
72
onError : conn . _handleFatalError . bind ( conn ) ,
71
73
onFailure : conn . _resetOnFailure . bind ( conn ) ,
@@ -97,7 +99,6 @@ export function createChannelConnection (
97
99
} )
98
100
)
99
101
}
100
-
101
102
export default class ChannelConnection extends Connection {
102
103
/**
103
104
* @constructor
@@ -128,7 +129,7 @@ export default class ChannelConnection extends Connection {
128
129
this . _disableLosslessIntegers = disableLosslessIntegers
129
130
this . _ch = channel
130
131
this . _chunker = chunker
131
- this . _log = log
132
+ this . _log = createConnectionLogger ( this , log )
132
133
this . _serversideRouting = serversideRouting
133
134
134
135
// connection from the database, returned in response for HELLO message and might not be available
@@ -145,7 +146,7 @@ export default class ChannelConnection extends Connection {
145
146
this . _isBroken = false
146
147
147
148
if ( this . _log . isDebugEnabled ( ) ) {
148
- this . _log . debug ( `${ this } created towards ${ address } ` )
149
+ this . _log . debug ( `created towards ${ address } ` )
149
150
}
150
151
}
151
152
@@ -234,6 +235,10 @@ export default class ChannelConnection extends Connection {
234
235
return this . _server
235
236
}
236
237
238
+ get logger ( ) {
239
+ return this . _log
240
+ }
241
+
237
242
/**
238
243
* "Fatal" means the connection is dead. Only call this if something
239
244
* happens that cannot be recovered from. This will lead to all subscribers
@@ -247,7 +252,7 @@ export default class ChannelConnection extends Connection {
247
252
248
253
if ( this . _log . isErrorEnabled ( ) ) {
249
254
this . _log . error (
250
- `${ this } experienced a fatal error ${ json . stringify ( this . _error ) } `
255
+ `experienced a fatal error ${ json . stringify ( this . _error ) } `
251
256
)
252
257
}
253
258
@@ -322,7 +327,7 @@ export default class ChannelConnection extends Connection {
322
327
*/
323
328
async close ( ) {
324
329
if ( this . _log . isDebugEnabled ( ) ) {
325
- this . _log . debug ( ` ${ this } closing` )
330
+ this . _log . debug ( ' closing' )
326
331
}
327
332
328
333
if ( this . _protocol && this . isOpen ( ) ) {
@@ -334,7 +339,7 @@ export default class ChannelConnection extends Connection {
334
339
await this . _ch . close ( )
335
340
336
341
if ( this . _log . isDebugEnabled ( ) ) {
337
- this . _log . debug ( ` ${ this } closed` )
342
+ this . _log . debug ( ' closed' )
338
343
}
339
344
}
340
345
@@ -350,3 +355,15 @@ export default class ChannelConnection extends Connection {
350
355
return error
351
356
}
352
357
}
358
+
359
+ /**
360
+ * Creates a log with the connection info as prefix
361
+ * @param {Connection } connection The connection
362
+ * @param {Logger } logger The logger
363
+ * @returns {Logger } The new logger with enriched messages
364
+ */
365
+ function createConnectionLogger ( connection , logger ) {
366
+ return new Logger ( logger . _level , ( level , message ) =>
367
+ logger . _loggerFunction ( level , `${ connection } ${ message } ` )
368
+ )
369
+ }
0 commit comments