backend logging system

πŸ“… 25 Jul 2025

View on Github β†—

date: 2025-07-25 topics: [nodejs, logging, mern, backend, devflow] day: 9

πŸ“˜ Day 9 – Logging Done Right: Track Everything with Clean Logs

βœ… What I Worked On

  • Replaced random console.log scattered throughout DevFlow
  • Implemented a structured logging system:
    • πŸ“… Timestamps
    • βœ… Log levels (info, warn, error)
    • 🧠 MongoDB log persistence
  • Added global middleware to log user events (e.g., login, actions)
  • Kept dev console readable while saving full logs to the database

πŸ“š What I Learned

πŸ”§ Why Console Logs Weren’t Enough

  • No timestamps = hard to debug timing issues
  • No context = can't trace user/session events
  • Not persistent = logs gone after a crash

βœ… Structured Logging Solution

  • Used a custom logger wrapper (or winston-style approach)
  • Example:
    logger.info('User logged in', { userId, time: new Date().toISOString() });
    

* Saved logs into a `logs` collection in MongoDB
* Console still prints logs, but DB keeps full history

### 🎯 Log Types Tracked

* βœ… Auth events (login, logout)
* βœ… User actions (e.g., post answer, delete comment)
* βœ… Errors with stack traces


## ❌ Blockers

* Initially struggled with Mongo connection for logging without circular imports
* Had to debounce log writes in some areas to avoid spam


## 🧠 Reflection

Logging isn’t boring β€” it’s **your eyes into production**.
Now I can trace user behavior, debug faster, and track performance over time.

 β€œGood logs don’t just tell you what happened β€” they tell you *why* it happened.”


## πŸ”— References / Code

* πŸ”§ [DevFlow Logging Commit](https://github.com/Sangam5756/devflow/commit/72a37c73716914f914a4af852a3e127f345e2905)
* πŸ’» [DevFlow Repository](https://github.com/Sangam5756/devflow)