Add request and SQL performance logging features to backend

- Implement request timing middleware in app.js to log slow requests and all requests based on environment variables.
- Enhance sequelize.js with optional SQL query timing and logging capabilities, allowing for performance monitoring of database queries.
This commit is contained in:
Torsten Schulz (local)
2025-12-20 16:35:30 +01:00
parent c5804f764c
commit aaeaeeed24
2 changed files with 41 additions and 0 deletions

View File

@@ -3,6 +3,25 @@ import dotenv from 'dotenv';
dotenv.config();
// Optionales Performance-Logging (aktivierbar per ENV)
// - SQL_BENCHMARK=1: Sequelize liefert Query-Timing (ms) an logger
// - SQL_SLOW_MS=200: Logge nur Queries ab dieser Dauer (wenn SQL_LOG_ALL nicht gesetzt)
// - SQL_LOG_ALL=1: Logge alle Queries (auch ohne benchmark)
const SQL_BENCHMARK = process.env.SQL_BENCHMARK === '1';
const SQL_LOG_ALL = process.env.SQL_LOG_ALL === '1';
const SQL_SLOW_MS = Number.parseInt(process.env.SQL_SLOW_MS || '200', 10);
const sqlLogger = (sql, timing) => {
// Sequelize ruft logging(sql) oder logging(sql, timing) abhängig von benchmark auf.
if (!SQL_BENCHMARK) {
if (SQL_LOG_ALL) console.log(sql);
return;
}
const ms = typeof timing === 'number' ? timing : 0;
if (SQL_LOG_ALL || ms >= SQL_SLOW_MS) {
console.log(`🛢️ SQL ${ms}ms: ${sql}`);
}
};
// Validiere Umgebungsvariablen
const dbName = process.env.DB_NAME;
const dbUser = process.env.DB_USER;
@@ -26,6 +45,8 @@ const sequelize = new Sequelize(dbName, dbUser, dbPass, {
timestamps: false,
underscored: true // WICHTIG: Alle Datenbankfelder im snake_case Format
},
benchmark: SQL_BENCHMARK,
logging: sqlLogger,
});
const createSchemas = async () => {