@ -23,6 +23,40 @@ export function safeStringify(obj: unknown) {
const isElectron = process . env . VITE_PLATFORM === "electron" ;
const isElectron = process . env . VITE_PLATFORM === "electron" ;
const isProduction = process . env . NODE_ENV === "production" ;
const isProduction = process . env . NODE_ENV === "production" ;
// Track initialization state to prevent circular dependencies
let isInitializing = true ;
// Mark initialization as complete after a delay to allow all services to start
setTimeout ( ( ) = > {
isInitializing = false ;
} , 2000 ) ; // 2 second delay to allow all services to initialize
// Function to check if we should skip database logging during initialization
function shouldSkipDatabaseLogging ( message : string ) : boolean {
// Skip during initialization phase
if ( isInitializing ) {
return true ;
}
// Skip specific initialization-related messages even after initialization
const initializationMessages = [
"[PlatformServiceFactory]" ,
"[SQLWorker]" ,
"[WebPlatformService]" ,
"[CapacitorPlatformService]" ,
"[CapacitorMigration]" ,
"[DB-Integrity]" ,
"[Migration]" ,
"[IndexedDBMigrationService]" ,
"Creating singleton instance" ,
"Worker loaded" ,
"Worker initialized" ,
"Platform service" ,
] ;
return initializationMessages . some ( ( pattern ) = > message . includes ( pattern ) ) ;
}
export const logger = {
export const logger = {
debug : ( message : string , . . . args : unknown [ ] ) = > {
debug : ( message : string , . . . args : unknown [ ] ) = > {
// Debug logs are very verbose - only show in development mode for web
// Debug logs are very verbose - only show in development mode for web
@ -42,15 +76,8 @@ export const logger = {
console . log ( message , . . . args ) ;
console . log ( message , . . . args ) ;
}
}
// Only log to database for important messages (not routine operations)
// Skip database logging during initialization or for initialization-related messages
// Skip database logging for migration messages to avoid circular dependency
if ( ! shouldSkipDatabaseLogging ( message ) ) {
if (
! message . includes ( "[CapacitorPlatformService]" ) &&
! message . includes ( "[CapacitorMigration]" ) &&
! message . includes ( "[DB-Integrity]" ) &&
! message . includes ( "[Migration]" ) &&
! message . includes ( "[IndexedDBMigrationService]" )
) {
const argsString = args . length > 0 ? " - " + safeStringify ( args ) : "" ;
const argsString = args . length > 0 ? " - " + safeStringify ( args ) : "" ;
logToDb ( message + argsString ) ;
logToDb ( message + argsString ) ;
}
}
@ -63,6 +90,10 @@ export const logger = {
) {
) {
// eslint-disable-next-line no-console
// eslint-disable-next-line no-console
console . info ( message , . . . args ) ;
console . info ( message , . . . args ) ;
}
// Skip database logging during initialization or for initialization-related messages
if ( ! shouldSkipDatabaseLogging ( message ) ) {
const argsString = args . length > 0 ? " - " + safeStringify ( args ) : "" ;
const argsString = args . length > 0 ? " - " + safeStringify ( args ) : "" ;
logToDb ( message + argsString ) ;
logToDb ( message + argsString ) ;
}
}
@ -74,26 +105,44 @@ export const logger = {
console . warn ( message , . . . args ) ;
console . warn ( message , . . . args ) ;
}
}
// Log warnings to database, but filter out routine operation s
// Skip database logging during initialization or for initialization-related message s
if ( ! message . includes ( "[CapacitorPlatformService]" ) ) {
if ( ! shouldSkipDatabaseLogging ( message ) ) {
const argsString = args . length > 0 ? " - " + safeStringify ( args ) : "" ;
const argsString = args . length > 0 ? " - " + safeStringify ( args ) : "" ;
logToDb ( message + argsString ) ;
logToDb ( message + argsString ) ;
}
}
} ,
} ,
error : ( message : string , . . . args : unknown [ ] ) = > {
error : ( message : string , . . . args : unknown [ ] ) = > {
// Errors will always be logged
// Errors will always be logged to console
// eslint-disable-next-line no-console
// eslint-disable-next-line no-console
console . error ( message , . . . args ) ;
console . error ( message , . . . args ) ;
const messageString = safeStringify ( message ) ;
const argsString = args . length > 0 ? safeStringify ( args ) : "" ;
// Skip database logging during initialization or for initialization-related messages
logToDb ( messageString + argsString ) ;
if ( ! shouldSkipDatabaseLogging ( message ) ) {
const messageString = safeStringify ( message ) ;
const argsString = args . length > 0 ? safeStringify ( args ) : "" ;
logToDb ( messageString + argsString ) ;
}
} ,
} ,
} ;
} ;
// Function to manually mark initialization as complete
export function markInitializationComplete ( ) : void {
isInitializing = false ;
}
// Function to check initialization status
export function isInitializationPhase ( ) : boolean {
return isInitializing ;
}
// Add CommonJS export for Electron
// Add CommonJS export for Electron
if ( typeof module !== "undefined" && module .exports ) {
if ( typeof module !== "undefined" && module .exports ) {
module .exports = { logger } ;
module .exports = {
logger ,
markInitializationComplete ,
isInitializationPhase ,
} ;
}
}
// Add default export for ESM
// Add default export for ESM
export default { logger } ;
export default { logger , markInitializationComplete , isInitializationPhase } ;