Two cleanups in one pass for the v1.14.0 'works on any platform' theme: 1. Response helpers — merged src/utils/responses.js and the root-level response-helpers.js into a single module at src/utils/responses.js. The old module had a richer set (created, noContent, validationError, unauthorized, forbidden, notFound, conflict) and is now re-exported from the new location. Updated 15 routes to import from src/utils/responses and deleted the root response-helpers.js. 2. Error logger — error-handler.js now uses the unified src/utils/logging.js#logError (same one src/app.js uses), so all errors go to one log file with one rotation policy. Removed the dead asyncHandler export (the real one is in src/utils/async-handler.js and is used everywhere). Deleted the legacy error-logger.js. Both are invisible to users — same HTTP response shapes, same log file path, same error format. Internal-only refactor.
78 lines
2.7 KiB
JavaScript
78 lines
2.7 KiB
JavaScript
const express = require('express');
|
|
const fs = require('fs');
|
|
const fsp = require('fs').promises;
|
|
const { exists } = require('../fs-helpers');
|
|
const { paginate, parsePaginationParams } = require('../pagination');
|
|
const { success } = require('../src/utils/responses');
|
|
|
|
/**
|
|
* Error logs routes factory
|
|
* @param {Object} deps - Explicit dependencies
|
|
* @param {string} deps.ERROR_LOG_FILE - Path to error log file
|
|
* @param {Object} deps.auditLogger - Audit logger instance
|
|
* @param {Function} deps.asyncHandler - Async route handler wrapper
|
|
* @returns {express.Router}
|
|
*/
|
|
module.exports = function({ ERROR_LOG_FILE, auditLogger, asyncHandler }) {
|
|
const router = express.Router();
|
|
|
|
// Get error logs
|
|
router.get('/error-logs', asyncHandler(async (req, res) => {
|
|
if (!await exists(ERROR_LOG_FILE)) {
|
|
return success(res, { logs: [] });
|
|
}
|
|
|
|
const logContent = await fsp.readFile(ERROR_LOG_FILE, 'utf8');
|
|
const logEntries = logContent.split('='.repeat(80)).filter(entry => entry.trim());
|
|
|
|
const logs = logEntries.map(entry => {
|
|
const lines = entry.trim().split('\n');
|
|
const firstLine = lines[0] || '';
|
|
const match = firstLine.match(/\[(.*?)\] (.*?): (.*)/);
|
|
|
|
if (match) {
|
|
return {
|
|
timestamp: match[1],
|
|
context: match[2],
|
|
error: match[3]
|
|
};
|
|
}
|
|
return null;
|
|
}).filter(Boolean);
|
|
|
|
success(res, { logs: logs.slice(-50).reverse() });
|
|
}, 'error-logs-get'));
|
|
|
|
// Clear error logs
|
|
router.delete('/error-logs', asyncHandler(async (req, res) => {
|
|
if (await exists(ERROR_LOG_FILE)) {
|
|
await fsp.writeFile(ERROR_LOG_FILE, '');
|
|
}
|
|
success(res, { message: 'Error logs cleared' });
|
|
}, 'error-logs-clear'));
|
|
|
|
// Audit log
|
|
router.get('/audit-logs', asyncHandler(async (req, res) => {
|
|
const paginationParams = parsePaginationParams(req.query);
|
|
const action = req.query.action || '';
|
|
if (paginationParams) {
|
|
// When paginating, fetch all matching entries and let pagination slice
|
|
const entries = await auditLogger.query({ limit: Number.MAX_SAFE_INTEGER, offset: 0, action });
|
|
const result = paginate(entries, paginationParams);
|
|
success(res, { entries: result.data, pagination: result.pagination });
|
|
} else {
|
|
const limit = parseInt(req.query.limit) || 50;
|
|
const offset = parseInt(req.query.offset) || 0;
|
|
const entries = await auditLogger.query({ limit, offset, action });
|
|
success(res, { entries });
|
|
}
|
|
}, 'audit-log'));
|
|
|
|
router.delete('/audit-logs', asyncHandler(async (req, res) => {
|
|
await auditLogger.clear();
|
|
success(res, { message: 'Audit log cleared' });
|
|
}, 'audit-log-clear'));
|
|
|
|
return router;
|
|
};
|