Browse Source

Replace console statements with structured logger calls

- Replace 30 console.log/error/warn statements with appropriate logger calls
- Add logger imports where missing (PlatformServiceFactory, PlatformServiceMixin)
- Maintain eslint-disable comments for critical error logging in worker context
- Improve error handling consistency across platform services
- Clean up commented-out logger statements in WebPlatformService

Files affected:
- src/db/databaseUtil.ts: 4 console statements → logger calls
- src/main.electron.ts: 3 console statements → logger calls
- src/registerSQLWorker.js: 9 console statements → logger calls
- src/services/PlatformServiceFactory.ts: 1 console statement → logger call
- src/services/platforms/WebPlatformService.ts: 8 console statements → logger calls
- src/utils/PlatformServiceMixin.ts: 5 console statements → logger calls

Reduces ESLint warnings from 111 to 82 (eliminates all no-console warnings)
pull/142/head
Matthew Raymer 3 days ago
parent
commit
e39f6a2d37
  1. 6
      src/db/databaseUtil.ts
  2. 9
      src/main.electron.ts
  3. 41
      src/registerSQLWorker.js
  4. 3
      src/services/PlatformServiceFactory.ts
  5. 35
      src/services/platforms/WebPlatformService.ts
  6. 16
      src/utils/PlatformServiceMixin.ts

6
src/db/databaseUtil.ts

@ -204,6 +204,7 @@ export async function logToDb(
// Prevent infinite logging loops - if we're already trying to log to database, // Prevent infinite logging loops - if we're already trying to log to database,
// just log to console instead to break circular dependency // just log to console instead to break circular dependency
if (isLoggingToDatabase) { if (isLoggingToDatabase) {
// eslint-disable-next-line no-console
console.log(`[DB-PREVENTED-${level.toUpperCase()}] ${message}`); console.log(`[DB-PREVENTED-${level.toUpperCase()}] ${message}`);
return; return;
} }
@ -238,6 +239,7 @@ export async function logToDb(
lastCleanupDate = todayKey; lastCleanupDate = todayKey;
} }
} catch (error) { } catch (error) {
// eslint-disable-next-line no-console
console.error( console.error(
"Error logging to database:", "Error logging to database:",
error, error,
@ -257,9 +259,9 @@ export async function logConsoleAndDb(
isError = false, isError = false,
): Promise<void> { ): Promise<void> {
if (isError) { if (isError) {
console.error(message); logger.error(message);
} else { } else {
console.log(message); logger.log(message);
} }
await logToDb(message, isError ? "error" : "info"); await logToDb(message, isError ? "error" : "info");

9
src/main.electron.ts

@ -90,7 +90,7 @@ if (typeof window !== "undefined" && window.require) {
.getRegistrations() .getRegistrations()
.then(function (registrations) { .then(function (registrations) {
for (const registration of registrations) { for (const registration of registrations) {
console.log( logger.log(
"[Electron] Unregistering service worker:", "[Electron] Unregistering service worker:",
registration.scope, registration.scope,
); );
@ -98,14 +98,11 @@ if (typeof window !== "undefined" && window.require) {
} }
}) })
.catch((error) => { .catch((error) => {
console.log( logger.log("[Electron] Failed to unregister service workers:", error);
"[Electron] Failed to unregister service workers:",
error,
);
}); });
} }
} catch (error) { } catch (error) {
console.log("[Electron] Service worker cleanup not available:", error); logger.log("[Electron] Service worker cleanup not available:", error);
} }
// Add any Electron-specific initialization here // Add any Electron-specific initialization here

41
src/registerSQLWorker.js

@ -14,7 +14,7 @@
* @since 2025-07-02 * @since 2025-07-02
*/ */
// import { logger } from "./utils/logger"; // DISABLED FOR DEBUGGING import { logger } from "./utils/logger";
/** /**
* Worker state management * Worker state management
@ -65,14 +65,11 @@ async function initializeDatabase() {
initializationPromise = (async () => { initializationPromise = (async () => {
try { try {
// logger.log("[SQLWorker] Starting database initialization..."); // DISABLED
const dbService = await getDatabaseService(); const dbService = await getDatabaseService();
await dbService.initialize(); await dbService.initialize();
isInitialized = true; isInitialized = true;
// logger.log("[SQLWorker] Database initialization completed successfully"); // DISABLED
} catch (error) { } catch (error) {
// logger.error("[SQLWorker] Database initialization failed:", error); // DISABLED logger.error("[SQLWorker] Database initialization failed:", error);
console.error("[SQLWorker] Database initialization failed:", error); // Keep only critical errors
isInitialized = false; isInitialized = false;
initializationPromise = null; initializationPromise = null;
throw error; throw error;
@ -88,16 +85,13 @@ async function initializeDatabase() {
async function handleQuery(id, sql, params = []) { async function handleQuery(id, sql, params = []) {
try { try {
await initializeDatabase(); await initializeDatabase();
// logger.log(`[SQLWorker] Executing query: ${sql}`, params); // DISABLED
const dbService = await getDatabaseService(); const dbService = await getDatabaseService();
const result = await dbService.query(sql, params); const result = await dbService.query(sql, params);
// logger.log(`[SQLWorker] Query completed successfully`); // DISABLED
sendResponse(id, "success", { result }); sendResponse(id, "success", { result });
} catch (error) { } catch (error) {
// logger.error(`[SQLWorker] Query failed:`, error); // DISABLED logger.error(`[SQLWorker] Query failed:`, error);
console.error(`[SQLWorker] Query failed:`, error); // Keep only critical errors
sendResponse(id, "error", null, { sendResponse(id, "error", null, {
message: error.message, message: error.message,
stack: error.stack, stack: error.stack,
@ -111,16 +105,13 @@ async function handleQuery(id, sql, params = []) {
async function handleExec(id, sql, params = []) { async function handleExec(id, sql, params = []) {
try { try {
await initializeDatabase(); await initializeDatabase();
// logger.log(`[SQLWorker] Executing statement: ${sql}`, params); // DISABLED
const dbService = await getDatabaseService(); const dbService = await getDatabaseService();
const result = await dbService.run(sql, params); const result = await dbService.run(sql, params);
// logger.log(`[SQLWorker] Statement executed successfully:`, result); // DISABLED
sendResponse(id, "success", result); sendResponse(id, "success", result);
} catch (error) { } catch (error) {
// logger.error(`[SQLWorker] Statement execution failed:`, error); // DISABLED logger.error(`[SQLWorker] Statement execution failed:`, error);
console.error(`[SQLWorker] Statement execution failed:`, error); // Keep only critical errors
sendResponse(id, "error", null, { sendResponse(id, "error", null, {
message: error.message, message: error.message,
stack: error.stack, stack: error.stack,
@ -134,17 +125,14 @@ async function handleExec(id, sql, params = []) {
async function handleGetOneRow(id, sql, params = []) { async function handleGetOneRow(id, sql, params = []) {
try { try {
await initializeDatabase(); await initializeDatabase();
// logger.log(`[SQLWorker] Executing getOneRow: ${sql}`, params); // DISABLED
const dbService = await getDatabaseService(); const dbService = await getDatabaseService();
const result = await dbService.query(sql, params); const result = await dbService.query(sql, params);
const oneRow = result?.[0]?.values?.[0]; const oneRow = result?.[0]?.values?.[0];
// logger.log(`[SQLWorker] GetOneRow completed successfully`); // DISABLED
sendResponse(id, "success", oneRow); sendResponse(id, "success", oneRow);
} catch (error) { } catch (error) {
// logger.error(`[SQLWorker] GetOneRow failed:`, error); // DISABLED logger.error(`[SQLWorker] GetOneRow failed:`, error);
console.error(`[SQLWorker] GetOneRow failed:`, error); // Keep only critical errors
sendResponse(id, "error", null, { sendResponse(id, "error", null, {
message: error.message, message: error.message,
stack: error.stack, stack: error.stack,
@ -158,11 +146,9 @@ async function handleGetOneRow(id, sql, params = []) {
async function handleInit(id) { async function handleInit(id) {
try { try {
await initializeDatabase(); await initializeDatabase();
// logger.log("[SQLWorker] Initialization request completed"); // DISABLED
sendResponse(id, "init-complete"); sendResponse(id, "init-complete");
} catch (error) { } catch (error) {
// logger.error("[SQLWorker] Initialization request failed:", error); // DISABLED logger.error("[SQLWorker] Initialization request failed:", error);
console.error("[SQLWorker] Initialization request failed:", error); // Keep only critical errors
sendResponse(id, "error", null, { sendResponse(id, "error", null, {
message: error.message, message: error.message,
stack: error.stack, stack: error.stack,
@ -174,7 +160,6 @@ async function handleInit(id) {
* Handle ping request for health check * Handle ping request for health check
*/ */
function handlePing(id) { function handlePing(id) {
// logger.log("[SQLWorker] Ping received"); // DISABLED
sendResponse(id, "pong"); sendResponse(id, "pong");
} }
@ -185,13 +170,10 @@ onmessage = function (event) {
const { id, type, sql, params } = event.data; const { id, type, sql, params } = event.data;
if (!id || !type) { if (!id || !type) {
// logger.error("[SQLWorker] Invalid message received:", event.data); // DISABLED logger.error("[SQLWorker] Invalid message received:", event.data);
console.error("[SQLWorker] Invalid message received:", event.data);
return; return;
} }
// logger.log(`[SQLWorker] Received message: ${type} (${id})`); // DISABLED
switch (type) { switch (type) {
case "query": case "query":
handleQuery(id, sql, params); handleQuery(id, sql, params);
@ -214,8 +196,7 @@ onmessage = function (event) {
break; break;
default: default:
// logger.error(`[SQLWorker] Unknown message type: ${type}`); // DISABLED logger.error(`[SQLWorker] Unknown message type: ${type}`);
console.error(`[SQLWorker] Unknown message type: ${type}`);
sendResponse(id, "error", null, { sendResponse(id, "error", null, {
message: `Unknown message type: ${type}`, message: `Unknown message type: ${type}`,
}); });
@ -227,13 +208,11 @@ onmessage = function (event) {
* Handle worker errors * Handle worker errors
*/ */
onerror = function (error) { onerror = function (error) {
// logger.error("[SQLWorker] Worker error:", error); // DISABLED logger.error("[SQLWorker] Worker error:", error);
console.error("[SQLWorker] Worker error:", error);
}; };
/** /**
* Auto-initialize on worker startup (removed to prevent circular dependency) * Auto-initialize on worker startup (removed to prevent circular dependency)
* Initialization now happens on first database operation * Initialization now happens on first database operation
*/ */
// logger.log("[SQLWorker] Worker loaded, ready to receive messages"); // DISABLED logger.log("[SQLWorker] Worker loaded, ready to receive messages");
console.log("[SQLWorker] Worker loaded, ready to receive messages");

3
src/services/PlatformServiceFactory.ts

@ -1,6 +1,7 @@
import { PlatformService } from "./PlatformService"; import { PlatformService } from "./PlatformService";
import { WebPlatformService } from "./platforms/WebPlatformService"; import { WebPlatformService } from "./platforms/WebPlatformService";
import { CapacitorPlatformService } from "./platforms/CapacitorPlatformService"; import { CapacitorPlatformService } from "./platforms/CapacitorPlatformService";
import { logger } from "../utils/logger";
/** /**
* Factory class for creating platform-specific service implementations. * Factory class for creating platform-specific service implementations.
@ -41,7 +42,7 @@ export class PlatformServiceFactory {
const platform = process.env.VITE_PLATFORM || "web"; const platform = process.env.VITE_PLATFORM || "web";
if (!PlatformServiceFactory.creationLogged) { if (!PlatformServiceFactory.creationLogged) {
console.log( logger.log(
`[PlatformServiceFactory] Creating singleton instance for platform: ${platform}`, `[PlatformServiceFactory] Creating singleton instance for platform: ${platform}`,
); );
PlatformServiceFactory.creationLogged = true; PlatformServiceFactory.creationLogged = true;

35
src/services/platforms/WebPlatformService.ts

@ -72,8 +72,6 @@ export class WebPlatformService implements PlatformService {
*/ */
private async initializeWorker(): Promise<void> { private async initializeWorker(): Promise<void> {
try { try {
// logger.log("[WebPlatformService] Initializing SQL worker..."); // DISABLED
this.worker = new Worker( this.worker = new Worker(
new URL("../../registerSQLWorker.js", import.meta.url), new URL("../../registerSQLWorker.js", import.meta.url),
{ type: "module" }, { type: "module" },
@ -91,7 +89,7 @@ export class WebPlatformService implements PlatformService {
); );
initBackend(this.worker); initBackend(this.worker);
} catch (error) { } catch (error) {
console.error( logger.error(
"[WebPlatformService] Failed to import/call initBackend:", "[WebPlatformService] Failed to import/call initBackend:",
error, error,
); );
@ -99,7 +97,7 @@ export class WebPlatformService implements PlatformService {
} }
} else { } else {
// We're in a worker context - skip initBackend call // We're in a worker context - skip initBackend call
console.log( logger.log(
"[WebPlatformService] Skipping initBackend call in worker context", "[WebPlatformService] Skipping initBackend call in worker context",
); );
} }
@ -109,19 +107,15 @@ export class WebPlatformService implements PlatformService {
}; };
this.worker.onerror = (error) => { this.worker.onerror = (error) => {
// logger.error("[WebPlatformService] Worker error:", error); // DISABLED logger.error("[WebPlatformService] Worker error:", error);
console.error("[WebPlatformService] Worker error:", error);
this.workerReady = false; this.workerReady = false;
}; };
// Send ping to verify worker is ready // Send ping to verify worker is ready
await this.sendWorkerMessage({ type: "ping" }); await this.sendWorkerMessage({ type: "ping" });
this.workerReady = true; this.workerReady = true;
// logger.log("[WebPlatformService] SQL worker initialized successfully"); // DISABLED
} catch (error) { } catch (error) {
// logger.error("[WebPlatformService] Failed to initialize worker:", error); // DISABLED logger.error("[WebPlatformService] Failed to initialize worker:", error);
console.error("[WebPlatformService] Failed to initialize worker:", error);
this.workerReady = false; this.workerReady = false;
this.workerInitPromise = null; this.workerInitPromise = null;
throw new Error("Failed to initialize database worker"); throw new Error("Failed to initialize database worker");
@ -140,21 +134,13 @@ export class WebPlatformService implements PlatformService {
} }
if (!id) { if (!id) {
// logger.warn("[WebPlatformService] Received message without ID:", message); // DISABLED logger.warn("[WebPlatformService] Received message without ID:", message);
console.warn(
"[WebPlatformService] Received message without ID:",
message,
);
return; return;
} }
const pending = this.pendingMessages.get(id); const pending = this.pendingMessages.get(id);
if (!pending) { if (!pending) {
// logger.warn( // DISABLED logger.warn(
// "[WebPlatformService] Received response for unknown message ID:",
// id,
// );
console.warn(
"[WebPlatformService] Received response for unknown message ID:", "[WebPlatformService] Received response for unknown message ID:",
id, id,
); );
@ -188,8 +174,7 @@ export class WebPlatformService implements PlatformService {
break; break;
default: default:
// logger.warn("[WebPlatformService] Unknown response type:", type); // DISABLED logger.warn("[WebPlatformService] Unknown response type:", type);
console.warn("[WebPlatformService] Unknown response type:", type);
pending.resolve(message); pending.resolve(message);
break; break;
} }
@ -220,9 +205,6 @@ export class WebPlatformService implements PlatformService {
timeout, timeout,
}); });
// logger.log( // DISABLED
// `[WebPlatformService] Sending message: ${request.type} (${id})`,
// );
this.worker!.postMessage(fullRequest); this.worker!.postMessage(fullRequest);
}); });
} }
@ -245,8 +227,7 @@ export class WebPlatformService implements PlatformService {
await this.sendWorkerMessage<boolean>({ type: "ping" }); await this.sendWorkerMessage<boolean>({ type: "ping" });
this.workerReady = true; this.workerReady = true;
} catch (error) { } catch (error) {
// logger.error("[WebPlatformService] Worker not ready:", error); // DISABLED logger.error("[WebPlatformService] Worker not ready:", error);
console.error("[WebPlatformService] Worker not ready:", error);
throw new Error("Database worker not ready"); throw new Error("Database worker not ready");
} }
} }

16
src/utils/PlatformServiceMixin.ts

@ -39,6 +39,7 @@ import type { PlatformService } from "@/services/PlatformService";
import { mapColumnsToValues, parseJsonField } from "@/db/databaseUtil"; import { mapColumnsToValues, parseJsonField } from "@/db/databaseUtil";
import { MASTER_SETTINGS_KEY } from "@/db/tables/settings"; import { MASTER_SETTINGS_KEY } from "@/db/tables/settings";
import * as databaseUtil from "@/db/databaseUtil"; import * as databaseUtil from "@/db/databaseUtil";
import { logger } from "@/utils/logger";
// ================================================= // =================================================
// CACHING INFRASTRUCTURE // CACHING INFRASTRUCTURE
@ -194,7 +195,7 @@ export const PlatformServiceMixin = {
try { try {
return await (this as any).platformService.dbQuery(sql, params); return await (this as any).platformService.dbQuery(sql, params);
} catch (error) { } catch (error) {
console.error( logger.error(
`[${(this as any).$options.name}] Database query failed:`, `[${(this as any).$options.name}] Database query failed:`,
{ {
sql, sql,
@ -213,14 +214,11 @@ export const PlatformServiceMixin = {
try { try {
return await (this as any).platformService.dbExec(sql, params); return await (this as any).platformService.dbExec(sql, params);
} catch (error) { } catch (error) {
console.error( logger.error(`[${(this as any).$options.name}] Database exec failed:`, {
`[${(this as any).$options.name}] Database exec failed:`,
{
sql, sql,
params, params,
error, error,
}, });
);
throw error; throw error;
} }
}, },
@ -232,7 +230,7 @@ export const PlatformServiceMixin = {
try { try {
return await (this as any).platformService.dbGetOneRow(sql, params); return await (this as any).platformService.dbGetOneRow(sql, params);
} catch (error) { } catch (error) {
console.error( logger.error(
`[${(this as any).$options.name}] Database single row query failed:`, `[${(this as any).$options.name}] Database single row query failed:`,
{ {
sql, sql,
@ -271,7 +269,7 @@ export const PlatformServiceMixin = {
return settings; return settings;
} catch (error) { } catch (error) {
console.error( logger.error(
`[${(this as any).$options.name}] Failed to get settings:`, `[${(this as any).$options.name}] Failed to get settings:`,
{ {
key, key,
@ -336,7 +334,7 @@ export const PlatformServiceMixin = {
return mergedSettings; return mergedSettings;
} catch (error) { } catch (error) {
console.error( logger.error(
`[${(this as any).$options.name}] Failed to get merged settings:`, `[${(this as any).$options.name}] Failed to get merged settings:`,
{ {
defaultKey, defaultKey,

Loading…
Cancel
Save