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

9
src/main.electron.ts

@ -90,7 +90,7 @@ if (typeof window !== "undefined" && window.require) {
.getRegistrations()
.then(function (registrations) {
for (const registration of registrations) {
console.log(
logger.log(
"[Electron] Unregistering service worker:",
registration.scope,
);
@ -98,14 +98,11 @@ if (typeof window !== "undefined" && window.require) {
}
})
.catch((error) => {
console.log(
"[Electron] Failed to unregister service workers:",
error,
);
logger.log("[Electron] Failed to unregister service workers:", 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

41
src/registerSQLWorker.js

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

3
src/services/PlatformServiceFactory.ts

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

35
src/services/platforms/WebPlatformService.ts

@ -72,8 +72,6 @@ export class WebPlatformService implements PlatformService {
*/
private async initializeWorker(): Promise<void> {
try {
// logger.log("[WebPlatformService] Initializing SQL worker..."); // DISABLED
this.worker = new Worker(
new URL("../../registerSQLWorker.js", import.meta.url),
{ type: "module" },
@ -91,7 +89,7 @@ export class WebPlatformService implements PlatformService {
);
initBackend(this.worker);
} catch (error) {
console.error(
logger.error(
"[WebPlatformService] Failed to import/call initBackend:",
error,
);
@ -99,7 +97,7 @@ export class WebPlatformService implements PlatformService {
}
} else {
// We're in a worker context - skip initBackend call
console.log(
logger.log(
"[WebPlatformService] Skipping initBackend call in worker context",
);
}
@ -109,19 +107,15 @@ export class WebPlatformService implements PlatformService {
};
this.worker.onerror = (error) => {
// logger.error("[WebPlatformService] Worker error:", error); // DISABLED
console.error("[WebPlatformService] Worker error:", error);
logger.error("[WebPlatformService] Worker error:", error);
this.workerReady = false;
};
// Send ping to verify worker is ready
await this.sendWorkerMessage({ type: "ping" });
this.workerReady = true;
// logger.log("[WebPlatformService] SQL worker initialized successfully"); // DISABLED
} catch (error) {
// logger.error("[WebPlatformService] Failed to initialize worker:", error); // DISABLED
console.error("[WebPlatformService] Failed to initialize worker:", error);
logger.error("[WebPlatformService] Failed to initialize worker:", error);
this.workerReady = false;
this.workerInitPromise = null;
throw new Error("Failed to initialize database worker");
@ -140,21 +134,13 @@ export class WebPlatformService implements PlatformService {
}
if (!id) {
// logger.warn("[WebPlatformService] Received message without ID:", message); // DISABLED
console.warn(
"[WebPlatformService] Received message without ID:",
message,
);
logger.warn("[WebPlatformService] Received message without ID:", message);
return;
}
const pending = this.pendingMessages.get(id);
if (!pending) {
// logger.warn( // DISABLED
// "[WebPlatformService] Received response for unknown message ID:",
// id,
// );
console.warn(
logger.warn(
"[WebPlatformService] Received response for unknown message ID:",
id,
);
@ -188,8 +174,7 @@ export class WebPlatformService implements PlatformService {
break;
default:
// logger.warn("[WebPlatformService] Unknown response type:", type); // DISABLED
console.warn("[WebPlatformService] Unknown response type:", type);
logger.warn("[WebPlatformService] Unknown response type:", type);
pending.resolve(message);
break;
}
@ -220,9 +205,6 @@ export class WebPlatformService implements PlatformService {
timeout,
});
// logger.log( // DISABLED
// `[WebPlatformService] Sending message: ${request.type} (${id})`,
// );
this.worker!.postMessage(fullRequest);
});
}
@ -245,8 +227,7 @@ export class WebPlatformService implements PlatformService {
await this.sendWorkerMessage<boolean>({ type: "ping" });
this.workerReady = true;
} catch (error) {
// logger.error("[WebPlatformService] Worker not ready:", error); // DISABLED
console.error("[WebPlatformService] Worker not ready:", error);
logger.error("[WebPlatformService] Worker not ready:", error);
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 { MASTER_SETTINGS_KEY } from "@/db/tables/settings";
import * as databaseUtil from "@/db/databaseUtil";
import { logger } from "@/utils/logger";
// =================================================
// CACHING INFRASTRUCTURE
@ -194,7 +195,7 @@ export const PlatformServiceMixin = {
try {
return await (this as any).platformService.dbQuery(sql, params);
} catch (error) {
console.error(
logger.error(
`[${(this as any).$options.name}] Database query failed:`,
{
sql,
@ -213,14 +214,11 @@ export const PlatformServiceMixin = {
try {
return await (this as any).platformService.dbExec(sql, params);
} catch (error) {
console.error(
`[${(this as any).$options.name}] Database exec failed:`,
{
logger.error(`[${(this as any).$options.name}] Database exec failed:`, {
sql,
params,
error,
},
);
});
throw error;
}
},
@ -232,7 +230,7 @@ export const PlatformServiceMixin = {
try {
return await (this as any).platformService.dbGetOneRow(sql, params);
} catch (error) {
console.error(
logger.error(
`[${(this as any).$options.name}] Database single row query failed:`,
{
sql,
@ -271,7 +269,7 @@ export const PlatformServiceMixin = {
return settings;
} catch (error) {
console.error(
logger.error(
`[${(this as any).$options.name}] Failed to get settings:`,
{
key,
@ -336,7 +334,7 @@ export const PlatformServiceMixin = {
return mergedSettings;
} catch (error) {
console.error(
logger.error(
`[${(this as any).$options.name}] Failed to get merged settings:`,
{
defaultKey,

Loading…
Cancel
Save