Skip to content

fix: add morgan HTTP request logging middleware#232

Open
pericharlabindhumadhavi-data wants to merge 2 commits into
xthxr:mainfrom
pericharlabindhumadhavi-data:fix/add-morgan-logging-204
Open

fix: add morgan HTTP request logging middleware#232
pericharlabindhumadhavi-data wants to merge 2 commits into
xthxr:mainfrom
pericharlabindhumadhavi-data:fix/add-morgan-logging-204

Conversation

@pericharlabindhumadhavi-data

@pericharlabindhumadhavi-data pericharlabindhumadhavi-data commented Jun 7, 2026

Copy link
Copy Markdown

Summary

morgan was already installed in package.json but was never imported or registered as middleware in server.js. This meant no HTTP access logs were being generated in production, making it impossible to debug request-level issues without adding extra instrumentation.

The fix adds two lines to server.js:

  • const morgan = require('morgan'); at the top with other imports
  • app.use(morgan('combined')); early in the middleware chain before route handlers

Every HTTP request will now produce a structured log line like:
GET /api/user/links 200 45ms

Related Issue

Fixes #204

Type of Change

  • Bug fix

Checklist

  • I have read the CONTRIBUTING.md guide
  • My code follows the project's coding standards
  • I have tested my changes locally
  • I have linked related issues

Notes

No new dependencies introduced — morgan was already present in package.json. Fix is exactly two lines.

Summary by CodeRabbit

  • Chores
    • Adds detailed HTTP request logging on the server. Request entries now include method, URL, status, content length and response time, producing more comprehensive server logs for incoming traffic. No other public APIs or routing behavior were changed.

@vercel

vercel Bot commented Jun 7, 2026

Copy link
Copy Markdown

@pericharlabindhumadhavi-data is attempting to deploy a commit to the xthxr's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0e104f3a-8dd8-4778-ba1d-cdea9699f89e

📥 Commits

Reviewing files that changed from the base of the PR and between b0c3ad4 and b49c7a4.

📒 Files selected for processing (1)
  • server.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • server.js

📝 Walkthrough

Walkthrough

server.js imports the morgan package and registers an Express middleware using a custom log format that records method, URL, status, content-length, and response time for each incoming HTTP request. No other routing, authentication, or exported API surface was changed.

Changes

HTTP Request Logging

Layer / File(s) Summary
Enable morgan middleware
server.js
morgan is imported at module scope and registered on the Express app with a custom log format to emit method, URL, status code, content-length, and response-time for every request.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

🐰 I hop along the server trail,
Logging each request without fail.
Method, path, and time I sing,
Tiny hops that tracing bring.
Cheerful logs — a debug spring.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: add morgan HTTP request logging middleware' clearly and specifically summarizes the main change in the pull request—adding HTTP logging functionality via the morgan middleware.
Linked Issues check ✅ Passed The PR fully addresses all coding requirements from issue #204: morgan is imported in server.js, registered early in the middleware chain with 'combined' format, and no new dependencies were added.
Out of Scope Changes check ✅ Passed All changes are directly related to issue #204—only the necessary morgan import and middleware registration were added; no routing, authentication, or other unrelated modifications were made.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
server.js (1)

86-86: ⚡ Quick win

Consider environment-based configuration and log streaming for production.

Morgan defaults to stdout, which may not integrate well with production log aggregation systems. Consider:

  • Different formats for development vs. production
  • Streaming logs to a file or external service in production
  • Conditional enabling based on NODE_ENV
💡 Example environment-based configuration
-app.use(morgan('combined'));
+// Use 'dev' format in development, 'combined' in production
+if (process.env.NODE_ENV === 'production') {
+  // In production, consider streaming to a file or log service
+  app.use(morgan('combined', {
+    skip: (req, res) => res.statusCode < 400 // Only log errors in production
+  }));
+} else {
+  app.use(morgan('dev')); // Colorized, concise format for development
+}

For production file logging:

const fs = require('fs');
const path = require('path');

// Create a write stream (in append mode)
const accessLogStream = fs.createWriteStream(
  path.join(__dirname, 'access.log'),
  { flags: 'a' }
);

app.use(morgan('combined', { stream: accessLogStream }));
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@server.js` at line 86, The morgan middleware is currently always configured
with app.use(morgan('combined')), which writes to stdout and isn’t suitable for
production log aggregation; update the middleware to configure logging based on
NODE_ENV (use a verbose/console-friendly format like 'dev' in development and
'combined' in production), conditionally enable morgan only when needed, and in
production stream logs to a persistent sink by creating a write stream via
fs.createWriteStream (e.g., accessLogStream) or by wiring morgan’s stream option
to your external logging service; modify the existing
app.use(morgan('combined')) call to choose format and stream based on
process.env.NODE_ENV and ensure errors creating the stream are handled.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@server.js`:
- Line 86: The app is using morgan('combined') which logs PII (client IP and
user-agent); replace this with a privacy-safe logging format by updating the
app.use(morgan('combined')) call to use either a built-in minimal format ('tiny'
or 'common') or a custom morgan format string that omits :remote-addr and
:user-agent (e.g., include only :method :url :status :res[content-length]
:response-time), or register custom tokens to anonymize IPs before logging;
update the call to morgan in the same location (the app.use(...) invocation) and
ensure any retention/consent policy is documented if you must keep IPs.

---

Nitpick comments:
In `@server.js`:
- Line 86: The morgan middleware is currently always configured with
app.use(morgan('combined')), which writes to stdout and isn’t suitable for
production log aggregation; update the middleware to configure logging based on
NODE_ENV (use a verbose/console-friendly format like 'dev' in development and
'combined' in production), conditionally enable morgan only when needed, and in
production stream logs to a persistent sink by creating a write stream via
fs.createWriteStream (e.g., accessLogStream) or by wiring morgan’s stream option
to your external logging service; modify the existing
app.use(morgan('combined')) call to choose format and stream based on
process.env.NODE_ENV and ensure errors creating the stream are handled.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9e5339ef-c3e5-4538-9589-4e64a51d58fd

📥 Commits

Reviewing files that changed from the base of the PR and between 6f3dde9 and b0c3ad4.

📒 Files selected for processing (1)
  • server.js

Comment thread server.js Outdated
@pericharlabindhumadhavi-data

Copy link
Copy Markdown
Author

Hey @xthxr, this one is a small 2-line fix whenever you get a moment
to review. Let me know if anything needs changing!

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.

[BUG] ReferenceError: splitTestService is not defined — split-test routes crash on every request

1 participant