- Added api-security-hardening (helmet, rate limits) - Added nodejs-backend-patterns (error handling) - Added observability-monitoring (pino logging) - Added websocket-engineer (socket.io real-time updates) - Added docker (Multi-stage build, compose) - Added vitest (testing configuration and store tests) - Added data-visualizer (MetricsBar and HostChart) - Added infrastructure-monitoring/proxmox-admin/network-engineer types - Fixed UI accessibility and styling - Cleaned up node_modules tracking
43 lines
976 B
TypeScript
43 lines
976 B
TypeScript
import { Request, Response, NextFunction } from 'express';
|
|
import pino from 'pino';
|
|
|
|
/**
|
|
* Pino structured logger.
|
|
* - Development: colorized, human-readable output
|
|
* - Production: JSON for log aggregation
|
|
*/
|
|
export const logger = pino({
|
|
level: process.env.LOG_LEVEL || 'info',
|
|
...(process.env.NODE_ENV !== 'production' && {
|
|
transport: {
|
|
target: 'pino-pretty',
|
|
options: { colorize: true },
|
|
},
|
|
}),
|
|
});
|
|
|
|
/**
|
|
* Request logging middleware.
|
|
* Logs method, url, status, and duration for every request.
|
|
*/
|
|
export const requestLogger = (
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction
|
|
) => {
|
|
const start = Date.now();
|
|
|
|
res.on('finish', () => {
|
|
const duration = Date.now() - start;
|
|
logger.info({
|
|
method: req.method,
|
|
url: req.url,
|
|
status: res.statusCode,
|
|
duration: `${duration}ms`,
|
|
ip: req.ip,
|
|
});
|
|
});
|
|
|
|
next();
|
|
};
|