Skip to content

Fix log message doesn't appearing correctly in the Bolt log. #756

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 1 commit into from
Jun 11, 2021
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
2 changes: 1 addition & 1 deletion bolt-connection/src/bolt/bolt-protocol-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export default class BoltProtocol {

if (queued) {
if (this._log.isDebugEnabled()) {
this._log.debug(`${this} C: ${message}`)
this._log.debug(`C: ${message}`)
}

this.packer().packStruct(
Expand Down
8 changes: 4 additions & 4 deletions bolt-connection/src/bolt/response-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ export default class ResponseHandler {
switch (msg.signature) {
case RECORD:
if (this._log.isDebugEnabled()) {
this._log.debug(`${this} S: RECORD ${json.stringify(msg)}`)
this._log.debug(`S: RECORD ${json.stringify(msg)}`)
}
this._currentObserver.onNext(payload)
break
case SUCCESS:
if (this._log.isDebugEnabled()) {
this._log.debug(`${this} S: SUCCESS ${json.stringify(msg)}`)
this._log.debug(`S: SUCCESS ${json.stringify(msg)}`)
}
try {
const metadata = this._transformMetadata(payload)
Expand All @@ -109,7 +109,7 @@ export default class ResponseHandler {
break
case FAILURE:
if (this._log.isDebugEnabled()) {
this._log.debug(`${this} S: FAILURE ${json.stringify(msg)}`)
this._log.debug(`S: FAILURE ${json.stringify(msg)}`)
}
try {
const error = newError(payload.message, payload.code)
Expand All @@ -125,7 +125,7 @@ export default class ResponseHandler {
break
case IGNORED:
if (this._log.isDebugEnabled()) {
this._log.debug(`${this} S: IGNORED ${json.stringify(msg)}`)
this._log.debug(`S: IGNORED ${json.stringify(msg)}`)
}
try {
if (this._currentFailure && this._currentObserver.onError) {
Expand Down
35 changes: 26 additions & 9 deletions bolt-connection/src/connection/connection-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
*/

import { Chunker, Dechunker, ChannelConfig, Channel } from '../channel'
import { newError, error, json } from 'neo4j-driver-core'
import { newError, error, json, internal } from 'neo4j-driver-core'
import Connection from './connection'
import Bolt from '../bolt'

const { PROTOCOL_ERROR } = error
const {
logger: { Logger }
} = internal

let idGenerator = 0

Expand Down Expand Up @@ -57,15 +60,14 @@ export function createChannelConnection (
const createProtocol = conn =>
Bolt.create({
version,
connection: conn,
channel,
chunker,
dechunker,
disableLosslessIntegers: config.disableLosslessIntegers,
useBigInt: config.useBigInt,
serversideRouting,
server: conn.server,
log,
log: conn.logger,
observer: {
onError: conn._handleFatalError.bind(conn),
onFailure: conn._resetOnFailure.bind(conn),
Expand Down Expand Up @@ -97,7 +99,6 @@ export function createChannelConnection (
})
)
}

export default class ChannelConnection extends Connection {
/**
* @constructor
Expand Down Expand Up @@ -128,7 +129,7 @@ export default class ChannelConnection extends Connection {
this._disableLosslessIntegers = disableLosslessIntegers
this._ch = channel
this._chunker = chunker
this._log = log
this._log = createConnectionLogger(this, log)
this._serversideRouting = serversideRouting

// connection from the database, returned in response for HELLO message and might not be available
Expand All @@ -145,7 +146,7 @@ export default class ChannelConnection extends Connection {
this._isBroken = false

if (this._log.isDebugEnabled()) {
this._log.debug(`${this} created towards ${address}`)
this._log.debug(`created towards ${address}`)
}
}

Expand Down Expand Up @@ -234,6 +235,10 @@ export default class ChannelConnection extends Connection {
return this._server
}

get logger () {
return this._log
}

/**
* "Fatal" means the connection is dead. Only call this if something
* happens that cannot be recovered from. This will lead to all subscribers
Expand All @@ -247,7 +252,7 @@ export default class ChannelConnection extends Connection {

if (this._log.isErrorEnabled()) {
this._log.error(
`${this} experienced a fatal error ${json.stringify(this._error)}`
`experienced a fatal error ${json.stringify(this._error)}`
)
}

Expand Down Expand Up @@ -322,7 +327,7 @@ export default class ChannelConnection extends Connection {
*/
async close () {
if (this._log.isDebugEnabled()) {
this._log.debug(`${this} closing`)
this._log.debug('closing')
}

if (this._protocol && this.isOpen()) {
Expand All @@ -334,7 +339,7 @@ export default class ChannelConnection extends Connection {
await this._ch.close()

if (this._log.isDebugEnabled()) {
this._log.debug(`${this} closed`)
this._log.debug('closed')
}
}

Expand All @@ -350,3 +355,15 @@ export default class ChannelConnection extends Connection {
return error
}
}

/**
* Creates a log with the connection info as prefix
* @param {Connection} connection The connection
* @param {Logger} logger The logger
* @returns {Logger} The new logger with enriched messages
*/
function createConnectionLogger (connection, logger) {
return new Logger(logger._level, (level, message) =>
logger._loggerFunction(level, `${connection} ${message}`)
)
}
28 changes: 26 additions & 2 deletions test/internal/connection-channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,19 @@ describe('#integration ChannelConnection', () => {
/**
* @return {Promise<Connection>}
*/
function createConnection (url, config, errorCode = null) {
function createConnection (
url,
config,
errorCode = null,
logger = createVerifyConnectionIdLogger()
) {
const _config = config || {}
connection = undefined
return createChannelConnection(
ServerAddress.fromUrl(url),
_config,
new ConnectionErrorHandler(errorCode || SERVICE_UNAVAILABLE),
Logger.noOp()
logger
).then(c => {
connection = c
return connection
Expand All @@ -563,4 +569,22 @@ describe('#integration ChannelConnection', () => {
originalWrite(message, observer, flush)
}
}

function createVerifyConnectionIdLogger () {
return new Logger('debug', (_, message) => {
if (!connection) {
// the connection is not the context, so we could
// only assert if it starts with Connection [
expect(
message.startsWith('Connection ['),
`Log message "${message}" should starts with "Connection ["`
).toBe(true)
return
}
expect(
message.startsWith(`${connection}`),
`Log message "${message}" should starts with "${connection}"`
).toBe(true)
})
}
})