@@ -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 */
5759export type SeverityLevel = ( typeof SeverityLevel ) [ keyof typeof SeverityLevel ] ;
5860
@@ -254,6 +256,15 @@ export interface LogConvertible extends Record<string, any> {
254256 toLog ( ) : Record < string , any > ;
255257}
256258
259+ /** @internal */
260+ export function stringifyWithMaxLen ( value : any , maxDocumentLength : number ) : string {
261+ const ejson = EJSON . stringify ( value ) ;
262+
263+ return maxDocumentLength !== 0 && ejson . length > maxDocumentLength
264+ ? `${ ejson . slice ( 0 , maxDocumentLength ) } ...`
265+ : ejson ;
266+ }
267+
257268/** @internal */
258269export type Loggable = LoggableEvent | LogConvertible ;
259270
@@ -292,22 +303,23 @@ function attachConnectionFields(
292303}
293304
294305function defaultLogTransform (
295- logObject : LoggableEvent | Record < string , any >
306+ logObject : LoggableEvent | Record < string , any > ,
307+ maxDocumentLength : number = DEFAULT_MAX_DOCUMENT_LENGTH
296308) : Omit < Log , 's' | 't' | 'c' > {
297309 let log : Omit < Log , 's' | 't' | 'c' > = Object . create ( null ) ;
298310
299311 switch ( logObject . name ) {
300312 case COMMAND_STARTED :
301313 log = attachCommandFields ( log , logObject ) ;
302314 log . message = 'Command started' ;
303- log . command = EJSON . stringify ( logObject . command ) ;
315+ log . command = stringifyWithMaxLen ( logObject . command , maxDocumentLength ) ;
304316 log . databaseName = logObject . databaseName ;
305317 return log ;
306318 case COMMAND_SUCCEEDED :
307319 log = attachCommandFields ( log , logObject ) ;
308320 log . message = 'Command succeeded' ;
309321 log . durationMS = logObject . duration ;
310- log . reply = EJSON . stringify ( logObject . reply ) ;
322+ log . reply = stringifyWithMaxLen ( logObject . reply , maxDocumentLength ) ;
311323 return log ;
312324 case COMMAND_FAILED :
313325 log = attachCommandFields ( log , logObject ) ;
@@ -452,7 +464,7 @@ export class MongoLogger {
452464 if ( isLogConvertible ( message ) ) {
453465 logMessage = { ...logMessage , ...message . toLog ( ) } ;
454466 } else {
455- logMessage = { ...logMessage , ...defaultLogTransform ( message ) } ;
467+ logMessage = { ...logMessage , ...defaultLogTransform ( message , this . maxDocumentLength ) } ;
456468 }
457469 }
458470 this . logDestination . write ( logMessage ) ;
0 commit comments