Skip to content

Commit

Permalink
feat: add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
montasim committed Aug 30, 2024
1 parent fbbdfc8 commit bbb4f4d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/shared/validateWithSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import asyncErrorHandlerService from '../service/asyncErrorHandler.service.js';
import customValidationMessage from './customValidationMessage.js';
import httpStatus from '../constant/httpStatus.constants.js';
import loggerService from '../service/logger.service.js';

/**
* @function validateWithSchema
Expand All @@ -36,14 +37,16 @@ const validateWithSchema = (schemas, options = {}) => {
);

if (error) {
const message = error.details
.map((detail) => detail.message)
.join(', ');
loggerService.debug(message);
const errorData = {
route: req.originalUrl,
timeStamp: new Date(),
success: false,
data: {},
message: error.details
.map((detail) => detail.message)
.join(', '),
message,
status: httpStatus.BAD_REQUEST,
};

Expand Down
20 changes: 13 additions & 7 deletions src/utilities/errorResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import httpStatus from '../constant/httpStatus.constants.js';
import loggerService from '../service/logger.service.js';

/**
* errorResponse - A function that generates a standardized error response object.
Expand All @@ -20,12 +21,17 @@ import httpStatus from '../constant/httpStatus.constants.js';
* @returns {Object} - An object representing the error response, including the timestamp,
* success flag, data payload, error message, and status code.
*/
const errorResponse = (message, status = httpStatus.BAD_REQUEST) => ({
timeStamp: new Date(),
success: false,
data: {},
message,
status,
});
const errorResponse = (message, status = httpStatus.BAD_REQUEST) => {
toString(status).startsWith('5')
? loggerService.error(message)
: loggerService.debug(message);
return {
timeStamp: new Date(),
success: false,
data: {},
message,
status,
};
};

export default errorResponse;
20 changes: 13 additions & 7 deletions src/utilities/sendResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import httpStatus from '../constant/httpStatus.constants.js';
import loggerService from '../service/logger.service.js';

/**
* sendResponse - A function that creates a standardized response object for successful
Expand All @@ -21,12 +22,17 @@ import httpStatus from '../constant/httpStatus.constants.js';
* @returns {Object} - An object representing the response, including the timestamp,
* success flag, data payload, message, and status code.
*/
const sendResponse = (data, message, status = httpStatus.OK) => ({
timeStamp: new Date(),
success: true,
data,
message,
status,
});
const sendResponse = (data, message, status = httpStatus.OK) => {
toString(status).startsWith('5')
? loggerService.error(message)

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

This logs sensitive data returned by
an access to passwordValidationResult
as clear text.
This logs sensitive data returned by
an access to passwordValidationResult
as clear text.
This logs sensitive data returned by
an access to passwordValidationResult
as clear text.
: loggerService.debug(message);

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

This logs sensitive data returned by
an access to passwordValidationResult
as clear text.
This logs sensitive data returned by
an access to passwordValidationResult
as clear text.
This logs sensitive data returned by
an access to passwordValidationResult
as clear text.
return {
timeStamp: new Date(),
success: true,
data,
message,
status,
};
};

export default sendResponse;

0 comments on commit bbb4f4d

Please sign in to comment.