Skip to content

Commit 80e74f6

Browse files
Wcoder547Copilot
andcommitted
feat(logging): integrate winston for improved logging and error handling
Co-authored-by: Copilot <copilot@github.com>
1 parent 97126bb commit 80e74f6

12 files changed

Lines changed: 592 additions & 18 deletions

File tree

Back-end/app.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { createServer } from "http";
88
import { v4 as uuid } from "uuid";
99
import cors from "cors";
1010
import { v2 as cloudinary } from "cloudinary";
11+
import logger from "./utils/logger.js";
12+
import morgan from "morgan";
1113
import {
1214
CHAT_JOINED,
1315
CHAT_LEAVED,
@@ -39,8 +41,7 @@ const onlineUsers = new Set();
3941

4042
connectDB(mongoURI);
4143

42-
console.log("laadle ci/cd pipeline bna li he ");
43-
console.log("laadle ci/cd pipeline bna li he 2");
44+
logger.info("Database connected successfully");
4445

4546
cloudinary.config({
4647
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
@@ -64,11 +65,20 @@ app.use(cors(corsOptions));
6465
app.use("/api/v1/user", userRoute);
6566
app.use("/api/v1/chat", chatRoute);
6667
app.use("/api/v1/admin", adminRoute);
68+
app.use("/api/v1/health", (req, res) => {
69+
res.status(200).json({ status: "success", message: "API is healthy" });
70+
});
6771

6872
app.get("/", (req, res) => {
6973
res.send("Hello World");
7074
});
7175

76+
const morganStream = {
77+
write: (message) => logger.http(message.trim()),
78+
};
79+
80+
app.use(morgan("combined", { stream: morganStream }));
81+
7282
//middleware for socket authentication
7383
io.use((socket, next) => {
7484
cookieParser()(
@@ -80,10 +90,10 @@ io.use((socket, next) => {
8090
});
8191

8292
io.on("connection", (socket) => {
83-
console.log("New client connected:", socket.id);
93+
logger.info("New client connected:", socket.id);
8494
const user = socket.user;
8595
userSocketIDs.set(user._id.toString(), socket.id);
86-
console.log(`User connected: ${user.name}`);
96+
logger.info(`User connected: ${user.name}`);
8797

8898
socket.on(NEW_MESSAGE, async ({ chatId, members, message }) => {
8999
const messageForRealTime = {
@@ -151,7 +161,7 @@ io.on("connection", (socket) => {
151161
app.use(errorMiddleware);
152162

153163
server.listen(port, () => {
154-
console.log(`Server is running on port ${port} in ${envMode} Mode`);
164+
logger.info(`Server is running on port ${port} in ${envMode} Mode`);
155165
});
156166

157167
export { envMode, adminSecretKey, userSocketIDs };

Back-end/middlewares/auth.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { adminSecretKey } from "../app.js";
44
import { TryCatch } from "./error.js";
55
import { CHATTU_TOKEN } from "../constants/config.js";
66
import { User } from "../models/user.js";
7+
import logger from "../utils/logger.js";
78

89
const isAuthenticated = TryCatch((req, res, next) => {
910
const token = req.cookies[CHATTU_TOKEN];
@@ -53,7 +54,7 @@ const socketAuthenticator = async (err, socket, next) => {
5354

5455
return next();
5556
} catch (error) {
56-
console.log(error);
57+
logger.error("Socket authentication error:", error);
5758
return next(new ErrorHandler("Please login to access this route", 401));
5859
}
5960
};

Back-end/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
"express-validator": "^7.2.1",
2323
"jsonwebtoken": "^9.0.2",
2424
"mongoose": "^8.18.0",
25+
"morgan": "^1.10.1",
2526
"multer": "^2.0.2",
2627
"nodemon": "^3.1.14",
2728
"socket.io": "^4.8.1",
28-
"uuid": "^11.1.0"
29+
"uuid": "^11.1.0",
30+
"winston": "^3.19.0"
2931
},
3032
"devDependencies": {
3133
"@faker-js/faker": "^10.0.0"

0 commit comments

Comments
 (0)