Updated Logging System to Include Application Logs

This commit is contained in:
2026-06-08 12:25:47 -05:00
parent 15e93c1e45
commit 9a9f890494
13 changed files with 323 additions and 12 deletions

View File

@@ -5,6 +5,7 @@ moment.locale('en');
const express = require('express');
const path = require('path');
const { DB_PATH, initialize } = require('./db');
const { logEvent } = require('./logger');
const { createCorsMiddleware } = require('./middleware/cors');
const { requireAuth } = require('./middleware/auth');
const { resolveCompany } = require('./middleware/company');
@@ -40,6 +41,23 @@ function createApp() {
app.use(express.json({ limit: '12mb' }));
console.log(`${moment().format()} ✅ Express app initialized with JSON body parsing and CORS middleware`);
// HTTP access log — one event per API request (method, path, status, duration). Skips the health
// probe and the log-viewer endpoint itself so the viewer doesn't generate noise about itself.
app.use((req, res, next) => {
if (!req.path.startsWith('/api') || req.path === '/api/health' || req.path.startsWith('/api/app-logs')) return next();
const started = Date.now();
res.on('finish', () => {
logEvent('http', `${req.method} ${req.originalUrl.split('?')[0]}${res.statusCode}`, {
method: req.method,
path: req.originalUrl.split('?')[0],
status: res.statusCode,
duration_ms: Date.now() - started,
ip: req.ip
}, res.statusCode >= 500 ? 'error' : (res.statusCode >= 400 ? 'warn' : 'info'));
});
return next();
});
app.get('/api/health', (req, res) => {
res.json({ ok: true, app: 'DepreCore', database: DB_PATH });
});