feat: logging
This commit is contained in:
@@ -22,7 +22,8 @@
|
||||
"express": "^5.2.1",
|
||||
"pg": "^8.20.0",
|
||||
"pg-hstore": "^2.3.4",
|
||||
"sequelize": "^6.37.7"
|
||||
"sequelize": "^6.37.7",
|
||||
"winston": "^3.19.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/cors": "^2.8.19",
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
import { Sequelize } from 'sequelize';
|
||||
|
||||
// Initialize Sequelize
|
||||
const db = new Sequelize({
|
||||
dialect: 'postgres',
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
port: parseInt(process.env.DB_PORT || '5432'),
|
||||
database: process.env.DB_NAME || 'postgres',
|
||||
username: process.env.DB_USER || 'postgres',
|
||||
password: process.env.DB_PASSWORD || 'pass',
|
||||
logging: false, // Disable logging in production
|
||||
});
|
||||
|
||||
// Test the connection
|
||||
async function testConnection() {
|
||||
try {
|
||||
await db.authenticate();
|
||||
console.log('✅ Database connection established.');
|
||||
} catch (err) {
|
||||
console.error('❌ Unable to connect to the database:', err);
|
||||
process.exit(1); // Exit if DB connection fails
|
||||
}
|
||||
}
|
||||
|
||||
// Export Sequelize instance and models
|
||||
export { db, testConnection };
|
||||
@@ -1,11 +1,7 @@
|
||||
import express, { Express, Request, Response } from "express";
|
||||
import cors from "cors";
|
||||
import * as dotenv from "dotenv";
|
||||
import transactionsRouter from './routes/transactions';
|
||||
import { db, testConnection } from "./db";
|
||||
|
||||
|
||||
dotenv.config();
|
||||
import { db, testConnection, logger } from "./util";
|
||||
|
||||
const app: Express = express();
|
||||
app.use(cors());
|
||||
@@ -27,10 +23,10 @@ async function startServer() {
|
||||
await db.sync({ alter: true }); // Use { force: true } to drop and recreate tables (development only!)
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`🚀 Backend Server running on http://localhost:${PORT}`);
|
||||
logger.info(`🚀 Backend Server running on http://localhost:${PORT}`);
|
||||
});
|
||||
}
|
||||
startServer().catch((err) => {
|
||||
console.error('Failed to start server:', err);
|
||||
logger.error('Failed to start server:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
43
server/src/util.ts
Normal file
43
server/src/util.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { Sequelize } from 'sequelize';
|
||||
import winston, { format } from "winston";
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
const logger = winston.createLogger({
|
||||
level:'info',
|
||||
})
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
logger.add(new winston.transports.Console({
|
||||
format: format.combine(
|
||||
format.colorize({all: true}),
|
||||
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
||||
format.printf((info) => `${info.timestamp} [${info.level}]: ${info.message}`),
|
||||
)
|
||||
|
||||
}));
|
||||
}
|
||||
|
||||
// Initialize Sequelize
|
||||
const db = new Sequelize({
|
||||
dialect: 'postgres',
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
port: parseInt(process.env.DB_PORT || '5432'),
|
||||
database: process.env.DB_NAME || 'postgres',
|
||||
username: process.env.DB_USER || 'postgres',
|
||||
password: process.env.DB_PASSWORD || 'pass',
|
||||
logging: false, // Disable logging in production
|
||||
});
|
||||
|
||||
// Test the connection
|
||||
async function testConnection() {
|
||||
try {
|
||||
await db.authenticate();
|
||||
logger.info('✅ Database connection established.');
|
||||
} catch (err) {
|
||||
logger.error('❌ Unable to connect to the database:', err);
|
||||
process.exit(1); // Exit if DB connection fails
|
||||
}
|
||||
}
|
||||
|
||||
// Export Sequelize instance and models
|
||||
export { logger, db, testConnection };
|
||||
Reference in New Issue
Block a user