Skip to content

Commit 1d9b2b9

Browse files
committed
start testing
1 parent 4c97ffa commit 1d9b2b9

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/mongo_logger.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export const SeverityLevel = Object.freeze({
5353
OFF: 'off'
5454
} as const);
5555

56+
/** @internal */
57+
export const DEFAULT_MAX_DOCUMENT_LENGTH = 1000;
5658
/** @internal */
5759
export type SeverityLevel = (typeof SeverityLevel)[keyof typeof SeverityLevel];
5860

@@ -253,6 +255,13 @@ export type LoggableEvent =
253255
export interface LogConvertible extends Record<string, any> {
254256
toLog(): Record<string, any>;
255257
}
258+
export function maybeTruncate(ejson: string, maxDocumentLength: number): string {
259+
if (maxDocumentLength === 0) {
260+
return ejson;
261+
}
262+
263+
return ejson.length > maxDocumentLength ? `${ejson.slice(0, maxDocumentLength)}...` : ejson;
264+
}
256265

257266
/** @internal */
258267
export type Loggable = LoggableEvent | LogConvertible;
@@ -494,7 +503,8 @@ export class MongoLogger {
494503
default: defaultSeverity
495504
},
496505
maxDocumentLength:
497-
parseUnsignedInteger(combinedOptions.MONGODB_LOG_MAX_DOCUMENT_LENGTH) ?? 1000,
506+
parseUnsignedInteger(combinedOptions.MONGODB_LOG_MAX_DOCUMENT_LENGTH) ??
507+
DEFAULT_MAX_DOCUMENT_LENGTH,
498508
logDestination: combinedOptions.mongodbLogPath
499509
};
500510
}

test/unit/mongo_logger.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import {
1919
CONNECTION_POOL_CREATED,
2020
CONNECTION_POOL_READY,
2121
CONNECTION_READY,
22+
DEFAULT_MAX_DOCUMENT_LENGTH,
2223
Log,
24+
maybeTruncate,
2325
MongoDBLogWritable,
2426
MongoLogger,
2527
MongoLoggerOptions,
@@ -1212,3 +1214,24 @@ describe('class MongoLogger', function () {
12121214
}
12131215
});
12141216
});
1217+
1218+
describe('maybeTruncate', function () {
1219+
const largeDoc = {};
1220+
before(function () {
1221+
for (let i = 0; i < 1000; i++) {
1222+
largeDoc[`test${i}`] = `Hello_${i}`;
1223+
}
1224+
});
1225+
1226+
context('when maxDocumentLength = 0', function () {
1227+
it('does not truncate document', function () {
1228+
const ejson = EJSON.stringify(largeDoc);
1229+
expect(ejson).to.equal(maybeTruncate(ejson, 0));
1230+
});
1231+
});
1232+
1233+
context('when maxDocumentLength is non-zero', function () {
1234+
context('when document has length greater than maxDocumentLength', function () {});
1235+
context('when document has length less than or equal to maxDocumentLength', function () {});
1236+
});
1237+
});

0 commit comments

Comments
 (0)