From 57d2893999752eda7afc96e0310914e9d98c64b1 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Tue, 5 Mar 2024 11:13:06 -0500 Subject: [PATCH 1/2] ready for review --- src/mongo_logger.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mongo_logger.ts b/src/mongo_logger.ts index 27fcbf8d308..be345eaeff7 100644 --- a/src/mongo_logger.ts +++ b/src/mongo_logger.ts @@ -220,7 +220,8 @@ export function createStdioLogger(stream: { }): MongoDBLogWritable { return { write: promisify((log: Log, cb: (error?: Error) => void): unknown => { - stream.write(inspect(log, { compact: true, breakLength: Infinity }), 'utf-8', cb); + const logLine = inspect(log, { compact: true, breakLength: Infinity }); + stream.write(`${logLine}\n`, 'utf-8', cb); return; }) }; From 1d4c3677faca96c6d29328dfe104e687c05dc7aa Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Tue, 5 Mar 2024 11:52:13 -0500 Subject: [PATCH 2/2] fixed unit tests --- test/unit/connection_string.test.ts | 6 ++++-- test/unit/mongo_client.test.js | 15 ++++++-------- test/unit/mongo_logger.test.ts | 32 ++++++++++++----------------- 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/test/unit/connection_string.test.ts b/test/unit/connection_string.test.ts index 30ba650d6d2..00069ced53a 100644 --- a/test/unit/connection_string.test.ts +++ b/test/unit/connection_string.test.ts @@ -896,7 +896,8 @@ describe('Connection String', function () { }); const log: Log = { t: new Date(), c: 'ConnectionStringStdErr', s: 'error' }; client.options.mongoLoggerOptions.logDestination.write(log); - expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true })); + const logLine = inspect(log, { breakLength: Infinity, compact: true }); + expect(stderrStub.write).calledWith(`${logLine}\n`); }); }); @@ -907,7 +908,8 @@ describe('Connection String', function () { }); const log: Log = { t: new Date(), c: 'ConnectionStringStdOut', s: 'error' }; client.options.mongoLoggerOptions.logDestination.write(log); - expect(stdoutStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true })); + const logLine = inspect(log, { breakLength: Infinity, compact: true }); + expect(stdoutStub.write).calledWith(`${logLine}\n`); }); }); diff --git a/test/unit/mongo_client.test.js b/test/unit/mongo_client.test.js index 3cd8ef52988..2fb5121b1e6 100644 --- a/test/unit/mongo_client.test.js +++ b/test/unit/mongo_client.test.js @@ -852,9 +852,8 @@ describe('MongoClient', function () { }); const log = { t: new Date(), c: 'constructorStdErr', s: 'error' }; client.options.mongoLoggerOptions.logDestination.write(log); - expect(stderrStub.write).calledWith( - inspect(log, { breakLength: Infinity, compact: true }) - ); + const logLine = inspect(log, { breakLength: Infinity, compact: true }); + expect(stderrStub.write).calledWith(`${logLine}\n`); }); }); @@ -882,9 +881,8 @@ describe('MongoClient', function () { }); const log = { t: new Date(), c: 'constructorStdOut', s: 'error' }; client.options.mongoLoggerOptions.logDestination.write(log); - expect(stdoutStub.write).calledWith( - inspect(log, { breakLength: Infinity, compact: true }) - ); + const logLine = inspect(log, { breakLength: Infinity, compact: true }); + expect(stdoutStub.write).calledWith(`${logLine}\n`); }); }); @@ -939,9 +937,8 @@ describe('MongoClient', function () { }); const log = { t: new Date(), c: 'constructorStdErr', s: 'error' }; client.options.mongoLoggerOptions.logDestination.write(log); - expect(stderrStub.write).calledWith( - inspect(log, { breakLength: Infinity, compact: true }) - ); + const logLine = inspect(log, { breakLength: Infinity, compact: true }); + expect(stderrStub.write).calledWith(`${logLine}\n`); }); }); }); diff --git a/test/unit/mongo_logger.test.ts b/test/unit/mongo_logger.test.ts index 2e9cecf66d6..454e7b1cc79 100644 --- a/test/unit/mongo_logger.test.ts +++ b/test/unit/mongo_logger.test.ts @@ -443,9 +443,8 @@ describe('class MongoLogger', async function () { const log: Log = { t: new Date(), c: 'command', s: 'error' }; options.logDestination.write(log); - expect(stderrStub.write).to.have.been.calledOnceWith( - inspect(log, { breakLength: Infinity, compact: true }) - ); + const logLine = inspect(log, { breakLength: Infinity, compact: true }); + expect(stderrStub.write).to.have.been.calledOnceWith(`${logLine}\n`); }); } } @@ -465,9 +464,8 @@ describe('class MongoLogger', async function () { const log: Log = { t: new Date(), c: 'command', s: 'error' }; options.logDestination.write(log); - expect(stderrStub.write).to.have.been.calledOnceWith( - inspect(log, { breakLength: Infinity, compact: true }) - ); + const logLine = inspect(log, { breakLength: Infinity, compact: true }); + expect(stderrStub.write).to.have.been.calledOnceWith(`${logLine}\n`); }); } } @@ -512,9 +510,8 @@ describe('class MongoLogger', async function () { const log: Log = { t: new Date(), c: 'command', s: 'error' }; options.logDestination.write(log); - expect(stderrStub.write).to.have.been.calledOnceWith( - inspect(log, { breakLength: Infinity, compact: true }) - ); + const logLine = inspect(log, { breakLength: Infinity, compact: true }); + expect(stderrStub.write).to.have.been.calledOnceWith(`${logLine}\n`); }); } } @@ -536,9 +533,8 @@ describe('class MongoLogger', async function () { const log: Log = { t: new Date(), c: 'command', s: 'error' }; options.logDestination.write(log); - expect(stderrStub.write).to.have.been.calledOnceWith( - inspect(log, { breakLength: Infinity, compact: true }) - ); + const logLine = inspect(log, { breakLength: Infinity, compact: true }); + expect(stderrStub.write).to.have.been.calledOnceWith(`${logLine}\n`); }); } } @@ -1399,9 +1395,8 @@ describe('class MongoLogger', async function () { logger.debug('client', 'random message'); let stderrStubCall = stderrStub.write.getCall(0).args[0]; stderrStubCall = stderrStubCall.slice(stderrStubCall.search('c:')); - expect(stderrStubCall).to.equal( - `c: 'client', s: 'error', message: 'User input for mongodbLogPath is now invalid. Logging is halted.', error: 'This writable always throws' }` - ); + const expectedLogLine1 = `c: 'client', s: 'error', message: 'User input for mongodbLogPath is now invalid. Logging is halted.', error: 'This writable always throws' }`; + expect(stderrStubCall).to.equal(`${expectedLogLine1}\n`); // logging is halted logger.debug('client', 'random message 2'); @@ -1450,9 +1445,8 @@ describe('class MongoLogger', async function () { // stderr now contains the error message let stderrStubCall = stderrStub.write.getCall(0).args[0]; stderrStubCall = stderrStubCall.slice(stderrStubCall.search('c:')); - expect(stderrStubCall).to.equal( - `c: 'client', s: 'error', message: 'User input for mongodbLogPath is now invalid. Logging is halted.', error: 'This writable always throws, but only after at least 500ms' }` - ); + const expectedLogLine1 = `c: 'client', s: 'error', message: 'User input for mongodbLogPath is now invalid. Logging is halted.', error: 'This writable always throws, but only after at least 500ms' }`; + expect(stderrStubCall).to.equal(`${expectedLogLine1}\n`); // no more logging in the future logger.debug('client', 'random message 2'); @@ -1480,7 +1474,7 @@ describe('class MongoLogger', async function () { let stderrStubCall = stderrStub.write.getCall(0).args[0]; stderrStubCall = stderrStubCall.slice(stderrStubCall.search('c:')); expect(stderrStubCall).to.equal( - `c: 'client', s: 'error', message: 'User input for mongodbLogPath is now invalid. Logging is halted.', error: 'I am stdout and do not work' }` + `c: 'client', s: 'error', message: 'User input for mongodbLogPath is now invalid. Logging is halted.', error: 'I am stdout and do not work' }\n` ); // logging is halted