File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,8 @@ export const SeverityLevel = Object.freeze({
53
53
OFF : 'off'
54
54
} as const ) ;
55
55
56
+ /** @internal */
57
+ export const DEFAULT_MAX_DOCUMENT_LENGTH = 1000 ;
56
58
/** @internal */
57
59
export type SeverityLevel = ( typeof SeverityLevel ) [ keyof typeof SeverityLevel ] ;
58
60
@@ -253,6 +255,13 @@ export type LoggableEvent =
253
255
export interface LogConvertible extends Record < string , any > {
254
256
toLog ( ) : Record < string , any > ;
255
257
}
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
+ }
256
265
257
266
/** @internal */
258
267
export type Loggable = LoggableEvent | LogConvertible ;
@@ -494,7 +503,8 @@ export class MongoLogger {
494
503
default : defaultSeverity
495
504
} ,
496
505
maxDocumentLength :
497
- parseUnsignedInteger ( combinedOptions . MONGODB_LOG_MAX_DOCUMENT_LENGTH ) ?? 1000 ,
506
+ parseUnsignedInteger ( combinedOptions . MONGODB_LOG_MAX_DOCUMENT_LENGTH ) ??
507
+ DEFAULT_MAX_DOCUMENT_LENGTH ,
498
508
logDestination : combinedOptions . mongodbLogPath
499
509
} ;
500
510
}
Original file line number Diff line number Diff line change @@ -19,7 +19,9 @@ import {
19
19
CONNECTION_POOL_CREATED ,
20
20
CONNECTION_POOL_READY ,
21
21
CONNECTION_READY ,
22
+ DEFAULT_MAX_DOCUMENT_LENGTH ,
22
23
Log ,
24
+ maybeTruncate ,
23
25
MongoDBLogWritable ,
24
26
MongoLogger ,
25
27
MongoLoggerOptions ,
@@ -1212,3 +1214,24 @@ describe('class MongoLogger', function () {
1212
1214
}
1213
1215
} ) ;
1214
1216
} ) ;
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments