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:
@@ -1,3 +1,5 @@
|
||||
import logger from '../utils/logger.js';
|
||||
|
||||
export default async function migration001(db) {
|
||||
await db.runAsync('BEGIN TRANSACTION');
|
||||
|
||||
@@ -88,5 +90,5 @@ export default async function migration001(db) {
|
||||
)`);
|
||||
|
||||
await db.runAsync('COMMIT');
|
||||
console.log('Migration 001: Initial schema created');
|
||||
logger.info('Migration 001: Initial schema created');
|
||||
}
|
||||
|
||||
@@ -1,33 +1,35 @@
|
||||
import logger from '../utils/logger.js';
|
||||
|
||||
export default async function migration002(db, checkColumnExists) {
|
||||
const balanceExists = await checkColumnExists('crypto_wallets', 'balance');
|
||||
if (!balanceExists) {
|
||||
await db.runAsync(`ALTER TABLE crypto_wallets ADD COLUMN balance REAL DEFAULT 0`);
|
||||
console.log('Migration 002: Column balance added to crypto_wallets');
|
||||
logger.info('Migration 002: Column balance added to crypto_wallets');
|
||||
}
|
||||
|
||||
const userIdExists = await checkColumnExists('transactions', 'user_id');
|
||||
if (!userIdExists) {
|
||||
await db.runAsync(`ALTER TABLE transactions ADD COLUMN user_id INTEGER NOT NULL`);
|
||||
console.log('Migration 002: Column user_id added to transactions');
|
||||
logger.info('Migration 002: Column user_id added to transactions');
|
||||
}
|
||||
|
||||
const walletTypeExists = await checkColumnExists('transactions', 'wallet_type');
|
||||
if (!walletTypeExists) {
|
||||
await db.runAsync(`ALTER TABLE transactions ADD COLUMN wallet_type TEXT NOT NULL`);
|
||||
console.log('Migration 002: Column wallet_type added to transactions');
|
||||
logger.info('Migration 002: Column wallet_type added to transactions');
|
||||
}
|
||||
|
||||
const txHashExists = await checkColumnExists('transactions', 'tx_hash');
|
||||
if (!txHashExists) {
|
||||
await db.runAsync(`ALTER TABLE transactions ADD COLUMN tx_hash TEXT NOT NULL`);
|
||||
console.log('Migration 002: Column tx_hash added to transactions');
|
||||
logger.info('Migration 002: Column tx_hash added to transactions');
|
||||
}
|
||||
|
||||
const statusExists = await checkColumnExists('purchases', 'status');
|
||||
if (!statusExists) {
|
||||
await db.runAsync(`ALTER TABLE purchases ADD COLUMN status TEXT DEFAULT 'pending'`);
|
||||
console.log('Migration 002: Column status added to purchases');
|
||||
logger.info('Migration 002: Column status added to purchases');
|
||||
}
|
||||
|
||||
console.log('Migration 002: Column additions complete');
|
||||
logger.info('Migration 002: Column additions complete');
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import logger from '../utils/logger.js';
|
||||
|
||||
export default async function migration003(db) {
|
||||
await db.runAsync(`CREATE INDEX IF NOT EXISTS idx_users_telegram_id ON users(telegram_id)`);
|
||||
await db.runAsync(`CREATE INDEX IF NOT EXISTS idx_crypto_wallets_user_type ON crypto_wallets(user_id, wallet_type)`);
|
||||
@@ -5,5 +7,5 @@ export default async function migration003(db) {
|
||||
await db.runAsync(`CREATE INDEX IF NOT EXISTS idx_purchases_user_product ON purchases(user_id, product_id)`);
|
||||
await db.runAsync(`CREATE INDEX IF NOT EXISTS idx_purchases_status ON purchases(status)`);
|
||||
await db.runAsync(`CREATE INDEX IF NOT EXISTS idx_products_location_category ON products(location_id, category_id)`);
|
||||
console.log('Migration 003: Indexes created');
|
||||
logger.info('Migration 003: Indexes created');
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user