@@ -2,7 +2,6 @@ import { isProd } from '../constants';
22import { getLogger , infra } from '../logger' ;
33import { env } from '../env' ;
44
5- const logger = getLogger ( infra . mongo ) ;
65import type { Model , Schema } from 'mongoose' ;
76import { Mongoose } from 'mongoose' ;
87
@@ -35,10 +34,11 @@ const addCommonMiddleware = (schema: Schema) => {
3534 } ) ;
3635
3736 schema . post ( op , function ( this : any , result : any , next : ( ) => void ) {
37+ const logger = getLogger ( infra . mongo ) ;
3838 if ( this . _startTime ) {
3939 const duration = Date . now ( ) - this . _startTime ;
4040 if ( duration > 1000 ) {
41- logger . warn ( ' Slow operation ${duration}ms on ${this.collection?.name}' ) ;
41+ logger . warn ( ` Slow operation ${ duration } ms on ${ this . collection ?. name } ` ) ;
4242 }
4343 }
4444 next ( ) ;
@@ -50,7 +50,10 @@ const addCommonMiddleware = (schema: Schema) => {
5050
5151export const getMongoModel = < T extends Schema > ( name : string , schema : T ) : Model < any > => {
5252 if ( connectionMongo . models [ name ] ) return connectionMongo . model ( name ) as Model < any > ;
53- if ( ! isProd ) logger . info ( 'Load model: ${name}' ) ;
53+ if ( ! isProd ) {
54+ const logger = getLogger ( infra . mongo ) ;
55+ logger . info ( `Load model: ${ name } ` ) ;
56+ }
5457 addCommonMiddleware ( schema ) ;
5558
5659 const model = connectionMongo . model ( name , schema ) ;
@@ -65,23 +68,26 @@ const syncMongoIndex = async (model: Model<any>) => {
6568 try {
6669 model . syncIndexes ( { background : true } ) ;
6770 } catch ( error : any ) {
68- logger . error ( 'Create index error' , { error } ) ;
71+ const logger = getLogger ( infra . mongo ) ;
72+ logger . error ( `Create index error: ${ JSON . stringify ( error , null , 2 ) } ` , { error } ) ;
6973 }
7074 }
7175} ;
7276
7377export const ReadPreference = connectionMongo . mongo . ReadPreference ;
7478
7579export async function connectMongo ( db : Mongoose , url : string ) : Promise < Mongoose > {
80+ const logger = getLogger ( infra . mongo ) ;
7681 if ( db . connection . readyState !== 0 ) {
7782 return db ;
7883 }
7984
8085 if ( ! url || typeof url !== 'string' ) {
86+ logger . error ( `Invalid MongoDB connection URL: ${ url } ` ) ;
8187 throw new Error ( `Invalid MongoDB connection URL: ${ url } ` ) ;
8288 }
8389
84- console . log ( `connecting to ${ isProd ? 'MongoDB' : url } `) ;
90+ logger . info ( `try to connect to ${ isProd ? 'MongoDB' : url } `) ;
8591
8692 try {
8793 db . connection . removeAllListeners ( 'error' ) ;
@@ -90,15 +96,15 @@ export async function connectMongo(db: Mongoose, url: string): Promise<Mongoose>
9096
9197 // Log errors but don't reconnect here to avoid duplicate reconnection
9298 db . connection . on ( 'error' , ( error : any ) => {
93- console . error ( 'mongo error' , error ) ;
99+ logger . error ( `Mongo error: ${ JSON . stringify ( error , null , 2 ) } ` , { error } ) ;
94100 } ) ;
95101
96102 db . connection . on ( 'connected' , ( ) => {
97- console . log ( 'mongo connected') ;
103+ logger . info ( 'Mongo connected') ;
98104 } ) ;
99105 // Handle reconnection on disconnect
100106 db . connection . on ( 'disconnected' , async ( ) => {
101- console . error ( 'mongo disconnected') ;
107+ logger . warn ( 'Mongo disconnected') ;
102108 } ) ;
103109
104110 await db . connect ( url , {
@@ -117,7 +123,7 @@ export async function connectMongo(db: Mongoose, url: string): Promise<Mongoose>
117123 } ) ;
118124 return db ;
119125 } catch ( error ) {
120- logger . error ( ' Mongo connect error' , { error } ) ;
126+ logger . error ( ` Mongo connect error: ${ JSON . stringify ( error , null , 2 ) } ` , { error } ) ;
121127 await db . disconnect ( ) ;
122128 await delay ( 1000 ) ;
123129 return connectMongo ( db , url ) ;
0 commit comments