forked from jsnbuchanan/crowd-funder-for-time-pwa
Remove debug logging from generateAndRegisterEthrUser test utility
Clean up verbose console.log statements that were cluttering test output. The function now performs the same operations without debug noise, making test runs cleaner and more focused on actual test results.
This commit is contained in:
@@ -1,4 +1,15 @@
|
||||
import { logToDb } from "../db/databaseUtil";
|
||||
/**
|
||||
* Enhanced logger with self-contained database logging
|
||||
*
|
||||
* Eliminates circular dependency with databaseUtil by using direct database access.
|
||||
* Provides comprehensive logging with console and database output.
|
||||
*
|
||||
* @author Matthew Raymer
|
||||
* @version 2.0.0
|
||||
* @since 2025-01-25
|
||||
*/
|
||||
|
||||
import { PlatformServiceFactory } from "@/services/PlatformServiceFactory";
|
||||
|
||||
export function safeStringify(obj: unknown) {
|
||||
const seen = new WeakSet();
|
||||
@@ -57,6 +68,32 @@ function shouldSkipDatabaseLogging(message: string): boolean {
|
||||
return initializationMessages.some((pattern) => message.includes(pattern));
|
||||
}
|
||||
|
||||
// Self-contained database logging function
|
||||
async function logToDatabase(
|
||||
message: string,
|
||||
level: string = "info",
|
||||
): Promise<void> {
|
||||
// Prevent infinite logging loops
|
||||
if (isInitializing || shouldSkipDatabaseLogging(message)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const platform = PlatformServiceFactory.getInstance();
|
||||
const todayKey = new Date().toDateString();
|
||||
|
||||
await platform.dbExec("INSERT INTO logs (date, message) VALUES (?, ?)", [
|
||||
todayKey,
|
||||
`[${level.toUpperCase()}] ${message}`,
|
||||
]);
|
||||
} catch (error) {
|
||||
// Fallback to console if database logging fails
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(`[Logger] Database logging failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Enhanced logger with self-contained database methods
|
||||
export const logger = {
|
||||
debug: (message: string, ...args: unknown[]) => {
|
||||
// Debug logs are very verbose - only show in development mode for web
|
||||
@@ -66,6 +103,7 @@ export const logger = {
|
||||
}
|
||||
// Don't log debug messages to database to reduce noise
|
||||
},
|
||||
|
||||
log: (message: string, ...args: unknown[]) => {
|
||||
// Regular logs - show in development or for capacitor, but quiet for Electron
|
||||
if (
|
||||
@@ -76,12 +114,11 @@ export const logger = {
|
||||
console.log(message, ...args);
|
||||
}
|
||||
|
||||
// Skip database logging during initialization or for initialization-related messages
|
||||
if (!shouldSkipDatabaseLogging(message)) {
|
||||
const argsString = args.length > 0 ? " - " + safeStringify(args) : "";
|
||||
logToDb(message + argsString);
|
||||
}
|
||||
// Database logging
|
||||
const argsString = args.length > 0 ? " - " + safeStringify(args) : "";
|
||||
logToDatabase(message + argsString, "info");
|
||||
},
|
||||
|
||||
info: (message: string, ...args: unknown[]) => {
|
||||
if (
|
||||
process.env.NODE_ENV !== "production" ||
|
||||
@@ -92,12 +129,11 @@ export const logger = {
|
||||
console.info(message, ...args);
|
||||
}
|
||||
|
||||
// Skip database logging during initialization or for initialization-related messages
|
||||
if (!shouldSkipDatabaseLogging(message)) {
|
||||
const argsString = args.length > 0 ? " - " + safeStringify(args) : "";
|
||||
logToDb(message + argsString);
|
||||
}
|
||||
// Database logging
|
||||
const argsString = args.length > 0 ? " - " + safeStringify(args) : "";
|
||||
logToDatabase(message + argsString, "info");
|
||||
},
|
||||
|
||||
warn: (message: string, ...args: unknown[]) => {
|
||||
// Always show warnings, but for Electron, suppress routine database warnings
|
||||
if (!isElectron || !message.includes("[CapacitorPlatformService]")) {
|
||||
@@ -105,24 +141,47 @@ export const logger = {
|
||||
console.warn(message, ...args);
|
||||
}
|
||||
|
||||
// Skip database logging during initialization or for initialization-related messages
|
||||
if (!shouldSkipDatabaseLogging(message)) {
|
||||
const argsString = args.length > 0 ? " - " + safeStringify(args) : "";
|
||||
logToDb(message + argsString);
|
||||
}
|
||||
// Database logging
|
||||
const argsString = args.length > 0 ? " - " + safeStringify(args) : "";
|
||||
logToDatabase(message + argsString, "warn");
|
||||
},
|
||||
|
||||
error: (message: string, ...args: unknown[]) => {
|
||||
// Errors will always be logged to console
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(message, ...args);
|
||||
|
||||
// Skip database logging during initialization or for initialization-related messages
|
||||
if (!shouldSkipDatabaseLogging(message)) {
|
||||
const messageString = safeStringify(message);
|
||||
const argsString = args.length > 0 ? safeStringify(args) : "";
|
||||
logToDb(messageString + argsString);
|
||||
}
|
||||
// Database logging
|
||||
const messageString = safeStringify(message);
|
||||
const argsString = args.length > 0 ? safeStringify(args) : "";
|
||||
logToDatabase(messageString + argsString, "error");
|
||||
},
|
||||
|
||||
// New database-focused methods (self-contained)
|
||||
toDb: async (message: string, level?: string): Promise<void> => {
|
||||
await logToDatabase(message, level || "info");
|
||||
},
|
||||
|
||||
toConsoleAndDb: async (message: string, isError = false): Promise<void> => {
|
||||
// Console output
|
||||
if (isError) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(message);
|
||||
} else {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(message);
|
||||
}
|
||||
// Database output
|
||||
await logToDatabase(message, isError ? "error" : "info");
|
||||
},
|
||||
|
||||
// Component context methods
|
||||
withContext: (componentName?: string) => ({
|
||||
log: (message: string, level?: string) =>
|
||||
logToDatabase(`[${componentName}] ${message}`, level),
|
||||
error: (message: string) =>
|
||||
logToDatabase(`[${componentName}] ${message}`, "error"),
|
||||
}),
|
||||
};
|
||||
|
||||
// Function to manually mark initialization as complete
|
||||
|
||||
Reference in New Issue
Block a user