@@ -113,12 +113,70 @@ export function withMongoMetrics(options: MongoClientOptions = {}): MongoClientO
113113 } ;
114114}
115115
116+ /**
117+ * Format filter/update parameters for logging
118+ * @param params - Parameters to format
119+ * @returns Formatted string
120+ */
121+ function formatParams ( params : any ) : string {
122+ if ( ! params || Object . keys ( params ) . length === 0 ) {
123+ return '' ;
124+ }
125+
126+ try {
127+ return JSON . stringify ( params ) ;
128+ } catch ( e ) {
129+ return String ( params ) ;
130+ }
131+ }
132+
133+ /**
134+ * Log MongoDB command details to console
135+ * Format: [requestId] db.collection.command(params)
136+ * @param event - MongoDB command event
137+ */
138+ function logCommandStarted ( event : any ) : void {
139+ const collectionRaw = extractCollectionFromCommand ( event . command , event . commandName ) ;
140+ const collection = normalizeCollectionName ( collectionRaw ) ;
141+ const db = event . databaseName || 'unknown db' ;
142+ const filter = event . command . filter ;
143+ const update = event . command . update ;
144+ const pipeline = event . command . pipeline ;
145+ const projection = event . command . projection ;
146+ const params = filter || update || pipeline ;
147+ const paramsStr = formatParams ( params ) ;
148+
149+ console . log ( `[${ event . requestId } ] ${ db } .${ collection } .${ event . commandName } (${ paramsStr } ) ${ projection ? `projection: ${ formatParams ( projection ) } ` : '' } ` ) ;
150+ }
151+
152+ /**
153+ * Log MongoDB command success to console
154+ * Format: [requestId] commandName ✓ duration
155+ * @param event - MongoDB command event
156+ */
157+ function logCommandSucceeded ( event : any ) : void {
158+ console . log ( `[${ event . requestId } ] ${ event . commandName } ✓ ${ event . duration } ms` ) ;
159+ }
160+
161+ /**
162+ * Log MongoDB command failure to console
163+ * Format: [requestId] ✗ error
164+ * @param event - MongoDB command event
165+ */
166+ function logCommandFailed ( event : any ) : void {
167+ const errorMsg = event . failure ?. message || event . failure ?. errmsg || 'Unknown error' ;
168+
169+ console . error ( `[${ event . requestId } ] ${ event . commandName } ✗ ${ errorMsg } (${ event . duration } ms)` ) ;
170+ }
171+
116172/**
117173 * Setup MongoDB metrics monitoring on a MongoClient
118174 * @param client - MongoDB client to monitor
119175 */
120176export function setupMongoMetrics ( client : MongoClient ) : void {
121177 client . on ( 'commandStarted' , ( event ) => {
178+ logCommandStarted ( event ) ;
179+
122180 // Store start time and metadata for this command
123181 const metadataKey = `${ event . requestId } ` ;
124182
@@ -139,6 +197,8 @@ export function setupMongoMetrics(client: MongoClient): void {
139197 } ) ;
140198
141199 client . on ( 'commandSucceeded' , ( event ) => {
200+ logCommandSucceeded ( event ) ;
201+
142202 const metadataKey = `${ event . requestId } ` ;
143203 // eslint-disable-next-line @typescript-eslint/no-explicit-any
144204 const metadata = ( client as any ) [ metadataKey ] ;
@@ -157,6 +217,8 @@ export function setupMongoMetrics(client: MongoClient): void {
157217 } ) ;
158218
159219 client . on ( 'commandFailed' , ( event ) => {
220+ logCommandFailed ( event ) ;
221+
160222 const metadataKey = `${ event . requestId } ` ;
161223 // eslint-disable-next-line @typescript-eslint/no-explicit-any
162224 const metadata = ( client as any ) [ metadataKey ] ;
0 commit comments