Skip to content

Commit 0c55b9b

Browse files
Merge pull request #73 from Esavwede/ogaga/backend/events
Add: [Backend] ogaga/backend/nodeevents
2 parents 1d31bfb + d6422e7 commit 0c55b9b

2 files changed

Lines changed: 236 additions & 0 deletions

File tree

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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+
---
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const EventEmitter = require("events");
2+
3+
// Create an instance
4+
const emitter = new EventEmitter();
5+
6+
// Listen for an event
7+
emitter.on("greet", (name) => {
8+
console.log(`Hello, ${name}!`);
9+
});
10+
11+
// Emit (trigger) the event
12+
emitter.emit("greet", "Ogaga");

0 commit comments

Comments
 (0)