Skip to content

Conversation

100NikhilBro
Copy link
Contributor

📝 Logging Setup - Winston + Morgan

Issue / Feature

Improvement: Winston + Morgan Logging Setup (#1285)

This setup provides a production-ready logging solution for Express apps, using Winston for application-level logging and Morgan for HTTP request logging.

🗂 Folder Structure

src/
├── logger/
│   ├── winston.js             # Winston configuration
│   ├── morgan.js              # Morgan HTTP logger
├── middlewares/
│   └── logging.middleware.js  # Attach req.logger and Morgan
├── middlewares/error.handler.js  # Global error handler using Winston - i updated this 
├── server.js        # Main server entry - used here  - loggingMiddleware(server)
  

Notes:

  • logs/ folder is auto-created on first run
  • error.log → contains only error-level logs
  • combined.log → contains all logs

⚡ Features

  • ✅ Automatic logs folder creation if missing
  • ✅ Request-level logging via Morgan + Winston
  • ✅ Error logging in both console and log files
  • ✅ Production-ready (different log levels for dev/prod)
  • ✅ Non-breaking modular middleware

🛠 Installation / Usage

  1. Install dependencies (if not installed yet):
    npm install winston morgan
          
  2. <li>
      <strong>Use request logger in routes</strong>:
      <pre>
    

    app.get('/', (req, res) => {
    req.logger.info('Home route accessed!')
    res.send('Logging system working perfectly 🚀')
    })

    ⚙️ Logging Behavior

    Log Type File Notes
    Console N/A Visible in terminal (colorized in dev)
    Error logs/error.log Only error-level logs
    Combined logs/combined.log All logs (info, warn, error, etc.)
    HTTP Requests Routed via Morgan Forwarded to Winston

Copy link

vercel bot commented Oct 6, 2025

@100NikhilBro is attempting to deploy a commit to the avdheshvarshney's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

github-actions bot commented Oct 6, 2025

Thank you for submitting your pull request! We'll review it as soon as possible. For further communication, join our discord server https://discord.gg/tSqtvHUJzE.

@100NikhilBro
Copy link
Contributor Author

Hi @Avdhesh-Varshney,

I just created a PR and wanted to share it with you. Please have a look when you get a chance, and let me know if any updates or improvements are needed.

Thanks!

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.

1 participant