Skip to content

fix: replace console.log with structured JSON logger in backend/server.js#312

Open
Goutam-2702 wants to merge 2 commits into
GitMetricsLab:mainfrom
Goutam-2702:fix/structured-logging-server
Open

fix: replace console.log with structured JSON logger in backend/server.js#312
Goutam-2702 wants to merge 2 commits into
GitMetricsLab:mainfrom
Goutam-2702:fix/structured-logging-server

Conversation

@Goutam-2702
Copy link
Copy Markdown

@Goutam-2702 Goutam-2702 commented May 18, 2026

Summary

Closes #308

Replaces all three bare console.log / console.error calls in backend/server.js with a lightweight structured logger that emits JSON log lines to stdout/stderr.

Why no new dependency?

Adding winston or pino would require updating package.json, package-lock.json, and potentially CI caching -- increasing review surface. The inline logger achieves the same structured-output goal with zero overhead and is trivially replaceable with a full logging library later.

Changes

Before After
console.log('Connected to MongoDB') logger.info('Connected to MongoDB')
console.log('Server running on port ...') logger.info('Server started', { port })
console.log('MongoDB connection error:', err) logger.error('MongoDB connection error', { error, stack })

Logger behaviour

  • NODE_ENV=test -> all output suppressed (safe for unit tests)
    • NODE_ENV=production -> JSON lines emitted; error level goes to stderr
    • NODE_ENV=development -> same JSON lines, easy to pipe to jq

Sample output

{"timestamp":"2025-05-18T04:00:00.000Z","level":"info","message":"Connected to MongoDB"}
{"timestamp":"2025-05-18T04:00:00.001Z","level":"info","message":"Server started","port":"5000"}

GSSoC '26 Participant: GOUTAM KUMAR GHOSAL

Summary by CodeRabbit

  • Chores
    • Replaced ad-hoc console output with structured JSON logging for startup and runtime visibility.
    • Improved error reporting for database connection failures with richer diagnostic details.
    • Routed log levels to appropriate streams and suppressed logs during test runs for cleaner output.

Review Change Stack

…tMetricsLab#308)

Replaces all three console.log calls with a zero-dependency structured logger that emits JSON log lines. Logs are suppressed in test/production environments via NODE_ENV check. Closes GitMetricsLab#308
@netlify
Copy link
Copy Markdown

netlify Bot commented May 18, 2026

Deploy Preview for github-spy ready!

Name Link
🔨 Latest commit d141506
🔍 Latest deploy log https://app.netlify.com/projects/github-spy/deploys/6a0aa75a26268800081d9904
😎 Deploy Preview https://deploy-preview-312--github-spy.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 18, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 037d200b-a263-4f1c-aab6-420e277990fd

📥 Commits

Reviewing files that changed from the base of the PR and between c1a09f8 and d141506.

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

📝 Walkthrough

Walkthrough

The backend server replaces ad-hoc console.log calls with an in-file structured JSON logger that suppresses output in tests, routes error-level logs to stderr and others to stdout, and logs MongoDB connection events and server startup (with port metadata).

Changes

Structured Logging Implementation

Layer / File(s) Summary
Structured logger and MongoDB integration
backend/server.js
In-file JSON logger with _write helper and info/warn/error methods, environment-aware suppression, and stderr/stdout routing. MongoDB connection success and server startup now emit structured logs (timestamp, level, message, metadata); connection errors include err.message and err.stack. Express-session options block reformatted without functional change.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A logger born of structured dreams,
No messy console.log streams,
JSON timestamps, levels clean,
MongoDB's finest ever seen!
Errors routed, logs refined—
A rabbit's logging redesigned! 🐇✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Out of Scope Changes check ❓ Inconclusive The PR includes a reformatting of express-session options which is not related to the structured logging objective, though the logging changes themselves are in-scope. Clarify whether the express-session options reformatting is intentional or should be separated into a different PR to maintain focus on the logging migration.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the main change: replacing console.log with a structured JSON logger in backend/server.js.
Description check ✅ Passed The description includes the related issue, detailed explanation of changes, rationale for avoiding dependencies, sample output, and identifies the contributor. However, the 'Type of Change' checkboxes are not completed.
Linked Issues check ✅ Passed The PR successfully replaces all console.log calls with structured JSON logging, implements environment-based output control (test/production/development), and provides log level management (info/error) as required by issue #308.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

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

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

🎉 Thank you @Goutam-2702 for your contribution. Please make sure your PR follows https://github.com/GitMetricsLab/github_tracker/blob/main/CONTRIBUTING.md#-pull-request-guidelines

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 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 `@backend/server.js`:
- Around line 32-34: The comment for the "Structured logger" is incorrect: it
claims verbose logs are suppressed in production and tests, but the
implementation only gates suppression on NODE_ENV === 'test'; update the comment
near the structured logger to accurately state that verbose logs are suppressed
in tests (NODE_ENV === 'test') while remaining available in production and
development, referencing the existing NODE_ENV check so readers can find the
behavior.
🪄 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: 34772c6f-7106-408d-9e77-5091dff3d60f

📥 Commits

Reviewing files that changed from the base of the PR and between 8d17610 and c1a09f8.

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

Comment thread backend/server.js Outdated
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🚀 Feature: Replace console.log Usage in {server.js} with Structured Logging

1 participant