Skip to content

Commit 40d146f

Browse files
committed
refactor(logging): replace console logging with centralized logger utility
- Updated all `console.error` and `console.warn` calls to use `logger.error` and `logger.warn` respectively. - Integrated `fileLogger` utility into `fileManager`, `middleware`, and `routes` to ensure standardized logging.
1 parent 6c172a4 commit 40d146f

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

Servers/middleware/rateLimit.middleware.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import rateLimit, { Options } from 'express-rate-limit';
1616
import { STATUS_CODE } from '../utils/statusCode.utils';
1717
import { Request, Response } from 'express';
18+
import logger from '../utils/logger/fileLogger';
1819

1920
/**
2021
* Rate limit configuration with time window and request limits
@@ -53,7 +54,7 @@ const RATE_LIMIT_CONFIGS: Record<string, RateLimitConfig> = {
5354
const createRateLimitHandler = (message: string) => {
5455
return (req: Request, res: Response) => {
5556
const clientIp = req.ip || req.socket?.remoteAddress || 'unknown';
56-
console.warn(`Rate limit exceeded for IP ${clientIp}: ${message}`);
57+
logger.warn(`Rate limit exceeded for IP ${clientIp} on ${req.path}: ${message}`);
5758
res.status(429).json(STATUS_CODE[429](message));
5859
};
5960
};

Servers/routes/fileManager.route.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { STATUS_CODE } from "../utils/statusCode.utils";
2727
import * as path from "path";
2828
import * as fs from "fs";
2929
import { ALLOWED_MIME_TYPES } from "../utils/validations/fileManagerValidation.utils";
30+
import logger from "../utils/logger/fileLogger";
3031

3132
const router = express.Router();
3233

@@ -100,16 +101,16 @@ const handleMulterError = (err: any, req: Request, res: Response, next: NextFunc
100101
fs.promises.unlink(resolvedPath).catch((cleanupError) => {
101102
// Ignore ENOENT (file already deleted), but log other errors
102103
if (cleanupError.code !== 'ENOENT') {
103-
console.error("Failed to clean up temporary file:", cleanupError);
104+
logger.error("Failed to clean up temporary file:", cleanupError);
104105
}
105106
});
106107
} else {
107108
// Log security violation attempt
108-
console.warn("Security: Blocked cleanup attempt outside temp directory. Path: %s, Allowed: %s", resolvedPath, resolvedTempDir);
109+
logger.warn(`Security: Blocked cleanup attempt outside temp directory. Path: ${resolvedPath}, Allowed: ${resolvedTempDir}`);
109110
}
110111
} catch (e) {
111112
// Unable to resolve file/directory (file may not exist), skip cleanup and continue
112-
console.warn("Failed to resolve path for cleanup: %s", req.file.path, e);
113+
logger.warn(`Failed to resolve path for cleanup: ${req.file.path}`, e);
113114
// Do not return - continue to error handling below
114115
}
115116
}

Servers/utils/fileManager.utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as fs from "fs";
1919
import { promisify } from "util";
2020
import { randomBytes } from "crypto";
2121
import { sanitizeFilename } from "./validations/fileManagerValidation.utils";
22+
import logger from "./logger/fileLogger";
2223

2324
const writeFile = promisify(fs.writeFile);
2425
const mkdir = promisify(fs.mkdir);
@@ -106,7 +107,7 @@ export const uploadFileToManager = async (
106107
} catch (unlinkError: any) {
107108
// Ignore ENOENT (file already deleted), but log other errors
108109
if (unlinkError.code !== 'ENOENT') {
109-
console.error(`Failed to cleanup orphaned file after DB error: ${permanentFilePath}`, unlinkError);
110+
logger.error(`Failed to cleanup orphaned file after DB error: ${permanentFilePath}`, unlinkError);
110111
}
111112
}
112113
// Rethrow original database error
@@ -289,7 +290,7 @@ export const deleteFile = async (fileId: number, tenant: string): Promise<boolea
289290

290291
if (!isContained) {
291292
const error = `Security violation: Attempted to delete file outside tenant directory. Target: ${targetPath}, Base: ${baseDir}`;
292-
console.error(error);
293+
logger.error(error);
293294
throw new Error(error);
294295
}
295296

@@ -303,12 +304,12 @@ export const deleteFile = async (fileId: number, tenant: string): Promise<boolea
303304
} catch (error: any) {
304305
// Only tolerate ENOENT (file already deleted/doesn't exist)
305306
if (error.code === 'ENOENT') {
306-
console.warn(`Physical file already deleted or not found: ${targetPath}`);
307+
logger.warn(`Physical file already deleted or not found: ${targetPath}`);
307308
} else {
308309
// Other errors are concerning - log and track them
309310
diskDeletionFailed = true;
310311
diskDeletionError = error;
311-
console.error(`Failed to delete physical file: ${targetPath}`, error);
312+
logger.error(`Failed to delete physical file: ${targetPath}`, error);
312313
}
313314
}
314315

0 commit comments

Comments
 (0)