feat(logging): replace 207 console.log/error/warn with pino structured logger (#58)

- Add pino + pino-pretty dependencies
- Create src/utils/logger.js with env-based LOG_LEVEL
- Replace all 207 console.log/error/warn calls across 46 source files
- Remove [DEBUG], [ERROR] string prefixes (levels convey this)
- Add pino redact for sensitive fields (mnemonic, privateKey, token, etc.)
- Structured logging with context objects instead of string interpolation
- NODE_ENV=production disables pino-pretty transport

49 files changed, 5601 insertions, 6056 deletions
This commit is contained in:
NW
2026-06-22 01:42:47 +01:00
parent ba80784ae7
commit ce1b6003cb
49 changed files with 5624 additions and 6079 deletions

View File

@@ -1,4 +1,5 @@
import db from '../config/database.js';
import logger from '../utils/logger.js';
const ALLOWED_TABLES = new Set([
'users', 'crypto_wallets', 'transactions', 'products',
@@ -13,7 +14,7 @@ export const checkColumnExists = async (tableName, columnName) => {
const result = await db.allAsync(`PRAGMA table_info(${tableName})`);
return result.some(column => column.name === columnName);
} catch (error) {
console.error(`Error checking column ${columnName} in table ${tableName}:`, error);
logger.error({ err: error, tableName, columnName }, 'Error checking column');
return false;
}
};
@@ -21,9 +22,9 @@ export const checkColumnExists = async (tableName, columnName) => {
export const cleanUpInvalidForeignKeys = async () => {
try {
await db.runAsync(`DELETE FROM crypto_wallets WHERE user_id NOT IN (SELECT id FROM users)`);
console.log('Cleaned up invalid foreign key references in crypto_wallets table');
logger.info('Cleaned up invalid foreign key references in crypto_wallets table');
} catch (error) {
console.error('Error cleaning up invalid foreign key references:', error);
logger.error({ err: error }, 'Error cleaning up invalid foreign key references');
}
};
@@ -40,7 +41,7 @@ export async function runMigrations() {
];
for (let i = currentVersion; i < migrations.length; i++) {
console.log(`Running migration ${i + 1}/${migrations.length}...`);
logger.info({ migration: i + 1, total: migrations.length }, 'Running migration');
if (i === 1) {
await migrations[i](db, checkColumnExists);
} else {
@@ -52,5 +53,5 @@ export async function runMigrations() {
);
}
console.log(`Migrations complete. Schema version: ${migrations.length}`);
logger.info({ schemaVersion: migrations.length }, 'Migrations complete');
}