Skip to content

refactor(backend): replace console logs with Winston logger#309

Open
drb101005 wants to merge 2 commits into
GitMetricsLab:mainfrom
drb101005:main
Open

refactor(backend): replace console logs with Winston logger#309
drb101005 wants to merge 2 commits into
GitMetricsLab:mainfrom
drb101005:main

Conversation

@drb101005
Copy link
Copy Markdown

@drb101005 drb101005 commented May 17, 2026

Implemented structured logging in the backend by replacing direct console.log usage in server.js with a centralized Winston logger.

This improves log readability and makes it easier to control logging behavior across different environments.

Changes Made
Added a reusable logger.js utility using Winston
Replaced existing console.log and error logs with:
logger.info()
logger.error()

Added configurable log levels using NODE_ENV / LOG_LEVEL
Included timestamps and better error formatting with stack traces

Added winston dependency to package.json

Files Updated
logger.js
server.js
package.json

Closes Feature: Replace console.log Usage in {server.js} with Structured Logging #308

Summary by CodeRabbit

  • Chores
    • Enhanced backend logging infrastructure for database connections and error handling.

Review Change Stack

@netlify
Copy link
Copy Markdown

netlify Bot commented May 17, 2026

Deploy Preview for github-spy ready!

Name Link
🔨 Latest commit 08b2092
🔍 Latest deploy log https://app.netlify.com/projects/github-spy/deploys/6a0a255b3d96760007f54284
😎 Deploy Preview https://deploy-preview-309--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 17, 2026

Warning

Rate limit exceeded

@drb101005 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 49 minutes and 9 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5f181b09-31c2-4c2c-977e-fc6b0da1d643

📥 Commits

Reviewing files that changed from the base of the PR and between 6f41635 and 08b2092.

📒 Files selected for processing (2)
  • backend/logger.js
  • backend/server.js
📝 Walkthrough

Walkthrough

Structured logging is introduced via a new Winston logger module configured with custom formatting for timestamps and log levels. The logger dependency is added to package.json, and server startup code replaces console.log calls with structured logger calls for MongoDB connection events and error reporting.

Changes

Structured Logging Setup

Layer / File(s) Summary
Logger module and dependency configuration
backend/logger.js, backend/package.json
Winston logger is initialized with custom printf formatting (${timestamp} ${level}: ${stack || message}), dynamic log level from LOG_LEVEL or NODE_ENV, and Console transport with colorization. Winston ^3.19.0 is added as a dependency.
Server startup logging via logger
backend/server.js
MongoDB connection handlers in server startup now import and use the logger for successful connections and errors, replacing console.log with logger.info and logger.error that report error message and stack.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related issues

Poem

🐰 A logger hops into the code with flair,
Winston's timestamps floating through the air,
No more console.log in sight,
Structured logs shining bright! 📝✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the purpose, changes made, and affected files, but does not follow the provided template structure with required sections like Related Issue, How Has This Been Tested, and Type of Change checkboxes. Reformat the description to match the template structure: add Related Issue section with issue number, include Testing section, mark the Type of Change checkbox, and maintain the current content within these sections.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: replacing console logs with a Winston logger in the backend, which is reflected in all three modified files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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 @drb101005 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: 3

🤖 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/logger.js`:
- Around line 4-6: The log formatter (logFormat using printf) currently uses
`${stack || message}` which drops the primary message when a stack exists;
change the format to always include the message and append the stack on a new
line when present (e.g., format string that emits message first and
conditionally adds `\n${stack}`), updating the printf callback that references
level, message, timestamp and stack so both message and stack are preserved in
the output.
- Around line 8-14: The logger configuration created by createLogger should
include Winston's errors format so Error objects' stacks are captured
automatically; update the format combine(...) used for logger and for the
transports.Console format to include format.errors({ stack: true }) (i.e., add
errors({ stack: true }) alongside timestamp() and logFormat) so
logger.error(err) and logger.error('msg', err) will include stack traces without
manual deconstruction in server.js; ensure the identifiers createLogger, logger,
logFormat, and transports.Console are updated accordingly.

In `@backend/server.js`:
- Line 40: The log call using logger.error('MongoDB connection error', {
message: err.message, stack: err.stack }) causes the metadata 'message' to
overwrite the main message; change the call in the MongoDB error handler (the
logger.error invocation) to either pass the Error object directly
(logger.error('MongoDB connection error', err)) if you've added Winston's
errors() format, or, if you must keep the current logger config, put the error
under a non-colliding key (e.g., logger.error('MongoDB connection error', {
error: err })) and update the logger format to read error.stack/message.
🪄 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: 473e3985-fef2-4c45-a237-e4cd7c1479cd

📥 Commits

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

📒 Files selected for processing (3)
  • backend/logger.js
  • backend/package.json
  • backend/server.js

Comment thread backend/logger.js
Comment thread backend/logger.js
Comment thread backend/server.js Outdated
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.

1 participant