|
| 1 | +**Contributor:** Ogagaoghene Esavwede |
| 2 | +**Domain:** Backend |
| 3 | +**Difficulty:** Beginner–Intermediate |
| 4 | +**Tech Stack:** Node.js, Express.js, Pino |
| 5 | + |
| 6 | +# 🚀 Express Logging Server with Pino |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## 📝 Description |
| 11 | + |
| 12 | +A simple Express.js server that replaces all `console.log` statements with a **structured logging system** using **Pino** — a fast, production-grade logging library for Node.js. |
| 13 | +This project demonstrates how to create a centralized logger, use it throughout your app, and log useful events like route access and server startup. |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 🎯 Features |
| 18 | + |
| 19 | +- ✅ Centralized logger configuration |
| 20 | +- ✅ Structured JSON logs |
| 21 | +- ✅ Replaces `console.log` with `logger.info`, `logger.error`, etc. |
| 22 | +- ✅ Lightweight and high performance (using Pino) |
| 23 | +- ✅ Works in production and development environments |
| 24 | +- ✅ Easily extensible for file or external log management |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## 🛠️ Tech Stack |
| 29 | + |
| 30 | +- **Node.js** – Runtime environment |
| 31 | +- **Express.js** – Web framework |
| 32 | +- **Pino** – Fast JSON logger |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +## 🚀 How to Run |
| 37 | + |
| 38 | +### Prerequisites |
| 39 | + |
| 40 | +- Node.js installed (v14 or higher) |
| 41 | +- npm or yarn package manager |
| 42 | + |
| 43 | +### Installation Steps |
| 44 | + |
| 45 | +1. **Install dependencies** |
| 46 | + |
| 47 | + ```bash |
| 48 | + npm install |
| 49 | + ``` |
| 50 | + |
| 51 | +2. **Start the server** |
| 52 | + |
| 53 | + ```bash |
| 54 | + npm start |
| 55 | + ``` |
| 56 | + |
| 57 | +3. **Server runs at** |
| 58 | + |
| 59 | + ``` |
| 60 | + http://localhost:3000 |
| 61 | + ``` |
| 62 | + |
| 63 | +4. **See logs in terminal** |
| 64 | + |
| 65 | + ``` |
| 66 | + {"level":30,"time":1729325400000,"msg":"Server running and listening on PORT: 3000","pid":12345,"hostname":"localhost"} |
| 67 | + ``` |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## 📁 Project Structure |
| 72 | + |
| 73 | +``` |
| 74 | +logging/ |
| 75 | +├── src/ |
| 76 | +│ ├── app.js # Main Express server file |
| 77 | +│ └── logging/ |
| 78 | +│ └── logger.js # Centralized logger configuration |
| 79 | +├── package.json # Dependencies and scripts |
| 80 | +├── .gitignore # Git ignore file |
| 81 | +└── README.md # Documentation |
| 82 | +``` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## 📡 Example Usage |
| 87 | + |
| 88 | +### app.js |
| 89 | + |
| 90 | +```javascript |
| 91 | +import express from "express"; |
| 92 | +import logger from "./logging/logger.js"; |
| 93 | + |
| 94 | +const app = express(); |
| 95 | +const PORT = process.env.PORT || 3000; |
| 96 | + |
| 97 | +app.get("/", (req, res) => { |
| 98 | + logger.info("User hit home route"); |
| 99 | + res.status(200).json({ success: true, message: "Welcome to the server" }); |
| 100 | +}); |
| 101 | + |
| 102 | +app.listen(PORT, () => { |
| 103 | + logger.info("Server running and listening on PORT: " + PORT); |
| 104 | +}); |
| 105 | +``` |
| 106 | + |
| 107 | +### logger.js |
| 108 | + |
| 109 | +```javascript |
| 110 | +import pino from "pino"; |
| 111 | + |
| 112 | +const logger = pino({ |
| 113 | + level: "info", |
| 114 | +}); |
| 115 | + |
| 116 | +export default logger; |
| 117 | +``` |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +## 💻 Code Comparison |
| 122 | + |
| 123 | +### ❌ Old Way (Using `console.log`) |
| 124 | + |
| 125 | +```javascript |
| 126 | +app.listen(PORT, () => { |
| 127 | + console.log("Server running on port " + PORT); |
| 128 | +}); |
| 129 | +``` |
| 130 | + |
| 131 | +### ✅ New Way (Using `logger.info`) |
| 132 | + |
| 133 | +```javascript |
| 134 | +app.listen(PORT, () => { |
| 135 | + logger.info("Server running and listening on PORT: " + PORT); |
| 136 | +}); |
| 137 | +``` |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +## 📚 Learning Outcomes |
| 142 | + |
| 143 | +### Skills Practiced |
| 144 | + |
| 145 | +- ✅ Structured logging in Node.js |
| 146 | +- ✅ Using Pino for performance logging |
| 147 | +- ✅ Creating reusable log modules |
| 148 | +- ✅ Logging best practices |
| 149 | + |
| 150 | +### Concepts Learned |
| 151 | + |
| 152 | +- Importance of centralized logging |
| 153 | +- Structured vs. unstructured logs |
| 154 | +- Logging levels (`info`, `warn`, `error`, `debug`) |
| 155 | +- Environment-based logging strategies |
| 156 | + |
| 157 | +--- |
| 158 | + |
| 159 | +## 🎨 Enhancement Ideas |
| 160 | + |
| 161 | +Want to improve this project? Try adding: |
| 162 | + |
| 163 | +1. **Pretty Logs** – Use `pino-pretty` for human-readable logs |
| 164 | + |
| 165 | + ```bash |
| 166 | + npm install pino-pretty |
| 167 | + npm start | npx pino-pretty |
| 168 | + ``` |
| 169 | + |
| 170 | +2. **File Logging** – Pipe logs into a file: |
| 171 | + |
| 172 | + ```bash |
| 173 | + node src/app.js > server.log |
| 174 | + ``` |
| 175 | + |
| 176 | +3. **Environment-specific Levels** – Use `debug` level in dev, `info` in prod |
| 177 | +4. **HTTP Request Logging** – Combine Pino with `pino-http` |
| 178 | +5. **Error Tracking** – Send logs to monitoring tools like Logtail, Datadog, or Sentry |
| 179 | + |
| 180 | +--- |
| 181 | + |
| 182 | +## 🧪 Testing |
| 183 | + |
| 184 | +### Using curl |
| 185 | + |
| 186 | +```bash |
| 187 | +curl http://localhost:3000/ |
| 188 | +``` |
| 189 | + |
| 190 | +**Terminal Output:** |
| 191 | + |
| 192 | +```bash |
| 193 | +{"level":30,"time":1729325400000,"msg":"User hit home route","pid":12345,"hostname":"localhost"} |
| 194 | +``` |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +## 🚀 Future Enhancements |
| 199 | + |
| 200 | +- [ ] Add pretty-print logs in development |
| 201 | +- [ ] Add log file rotation |
| 202 | +- [ ] Add request logging middleware |
| 203 | +- [ ] Add error logging middleware |
| 204 | +- [ ] Add environment-based log filtering |
| 205 | + |
| 206 | +--- |
| 207 | + |
| 208 | +## 📄 License |
| 209 | + |
| 210 | +MIT License — Free to use and modify! |
| 211 | + |
| 212 | +--- |
| 213 | + |
| 214 | +## 🤝 Contributing |
| 215 | + |
| 216 | +This project is open-source and created to help developers learn **how to replace `console.log` with a proper logger** in Node.js applications. |
| 217 | +Feel free to: |
| 218 | + |
| 219 | +- Fork and enhance |
| 220 | +- Report issues |
| 221 | +- Suggest improvements |
| 222 | +- Use it as a logging starter template |
| 223 | + |
| 224 | +--- |
0 commit comments