Skip to content

Adopt Morgan for one-line access logs and add centralized detailed error logging#84

Draft
Copilot wants to merge 3 commits intodevfrom
copilot/add-request-logging
Draft

Adopt Morgan for one-line access logs and add centralized detailed error logging#84
Copilot wants to merge 3 commits intodevfrom
copilot/add-request-logging

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 10, 2026

Current logging was custom and noisy for request tracing. This change switches request logging to a standard package with a minimal one-line format, while adding centralized rich diagnostics for error paths.

  • Request logging (minimal, package-based)

    • Replaced the custom request logger with morgan.
    • Kept output intentionally compact: one line per request with method, URL, status, latency, and client IP.
  • Error logging (detailed, centralized)

    • Added errorLogger Express middleware to capture propagated errors in one place.
    • Logs structured error context: timestamp, method, URL, resolved status code (err.statusCode / err.status fallback), message, and stack.
  • Server wiring

    • Registered errorLogger at the end of the middleware chain in server.ts so route/middleware errors are captured consistently.
morgan.token('client-ip', (req: Request) => getClientIp(req));
export const requestLogger = morgan(':method :url :status :response-time ms - :client-ip');

export const errorLogger: ErrorRequestHandler = (err, req, _res, next) => {
  const statusCode = typeof err?.statusCode === 'number' ? err.statusCode : typeof err?.status === 'number' ? err.status : 500;
  console.error('[request-error]', {
    timestamp: new Date().toISOString(),
    method: req.method,
    url: req.originalUrl,
    statusCode,
    message: err?.message || 'Unknown error',
    stack: err?.stack
  });
  next(err);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants