feat: logging

This commit is contained in:
eneller
2026-03-06 01:33:58 +01:00
parent 599ed86c52
commit fb0fc59240
5 changed files with 327 additions and 35 deletions

View File

@@ -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 };

View File

@@ -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
View 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 };