From c1a09f86c2270eecefbed38c458698ea114e230c Mon Sep 17 00:00:00 2001 From: Goutam Kumar Ghosal Date: Mon, 18 May 2026 10:01:40 +0530 Subject: [PATCH 1/2] fix: replace console.log with structured JSON logger in server.js (#308) Replaces all three console.log calls with a zero-dependency structured logger that emits JSON log lines. Logs are suppressed in test/production environments via NODE_ENV check. Closes #308 --- backend/server.js | 49 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/backend/server.js b/backend/server.js index 3f19f00..9ecb2e1 100644 --- a/backend/server.js +++ b/backend/server.js @@ -17,9 +17,9 @@ app.use(cors('*')); // Middleware app.use(bodyParser.json()); app.use(session({ - secret: process.env.SESSION_SECRET, - resave: false, - saveUninitialized: false, + secret: process.env.SESSION_SECRET, + resave: false, + saveUninitialized: false, })); app.use(passport.initialize()); app.use(passport.session()); @@ -28,12 +28,45 @@ app.use(passport.session()); const authRoutes = require('./routes/auth'); app.use('/api/auth', authRoutes); +/** + * Structured logger -- gates output behind NODE_ENV so that + * verbose logs are suppressed in production (NODE_ENV=production) + * and tests (NODE_ENV=test) while remaining available in development. + * + * Each log entry is emitted as a JSON line for easy ingestion by + * log-aggregation tools (Datadog, CloudWatch, ELK, etc.). + */ +const logger = { + _write(level, message, meta = {}) { + // Suppress all logging in test environments + if (process.env.NODE_ENV === 'test') return; + + const entry = { + timestamp: new Date().toISOString(), + level, + message, + ...meta, + }; + + if (level === 'error') { + // Errors go to stderr so they can be captured separately + process.stderr.write(JSON.stringify(entry) + '\n'); + } else { + // info / warn go to stdout + process.stdout.write(JSON.stringify(entry) + '\n'); + } + }, + info(message, meta) { this._write('info', message, meta); }, + warn(message, meta) { this._write('warn', message, meta); }, + error(message, meta) { this._write('error', message, meta); }, +}; + // Connect to MongoDB mongoose.connect(process.env.MONGO_URI, {}).then(() => { - console.log('Connected to MongoDB'); - app.listen(process.env.PORT, () => { - console.log(`Server running on port ${process.env.PORT}`); - }); + logger.info('Connected to MongoDB'); + app.listen(process.env.PORT, () => { + logger.info('Server started', { port: process.env.PORT }); + }); }).catch((err) => { - console.log('MongoDB connection error:', err); + logger.error('MongoDB connection error', { error: err.message, stack: err.stack }); }); From d141506c46ba4aae02649876836d2e2fd86d5148 Mon Sep 17 00:00:00 2001 From: Goutam Kumar Ghosal Date: Mon, 18 May 2026 11:14:56 +0530 Subject: [PATCH 2/2] Apply suggestion from @coderabbitai[bot] Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- backend/server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/server.js b/backend/server.js index 9ecb2e1..3436a9e 100644 --- a/backend/server.js +++ b/backend/server.js @@ -30,8 +30,8 @@ app.use('/api/auth', authRoutes); /** * Structured logger -- gates output behind NODE_ENV so that - * verbose logs are suppressed in production (NODE_ENV=production) - * and tests (NODE_ENV=test) while remaining available in development. + * logs are suppressed in tests (NODE_ENV=test). + * In development and production, JSON logs are emitted. * * Each log entry is emitted as a JSON line for easy ingestion by * log-aggregation tools (Datadog, CloudWatch, ELK, etc.).