Skip to content

Commit

Permalink
refactor(NODE-5903): add newline to stdio logging (#4018)
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB authored Mar 5, 2024
1 parent 443835e commit 7eaf2c8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/mongo_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
};
Expand Down
6 changes: 4 additions & 2 deletions test/unit/connection_string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
});

Expand All @@ -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`);
});
});

Expand Down
15 changes: 6 additions & 9 deletions test/unit/mongo_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
});

Expand Down Expand Up @@ -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`);
});
});

Expand Down Expand Up @@ -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`);
});
});
});
Expand Down
32 changes: 13 additions & 19 deletions test/unit/mongo_logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
}
}
Expand All @@ -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`);
});
}
}
Expand Down Expand Up @@ -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`);
});
}
}
Expand All @@ -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`);
});
}
}
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7eaf2c8

Please sign in to comment.