Skip to content

Commit bbb4f4d

Browse files
committed
feat: add logger
1 parent fbbdfc8 commit bbb4f4d

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

src/shared/validateWithSchema.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import asyncErrorHandlerService from '../service/asyncErrorHandler.service.js';
1111
import customValidationMessage from './customValidationMessage.js';
1212
import httpStatus from '../constant/httpStatus.constants.js';
13+
import loggerService from '../service/logger.service.js';
1314

1415
/**
1516
* @function validateWithSchema
@@ -36,14 +37,16 @@ const validateWithSchema = (schemas, options = {}) => {
3637
);
3738

3839
if (error) {
40+
const message = error.details
41+
.map((detail) => detail.message)
42+
.join(', ');
43+
loggerService.debug(message);
3944
const errorData = {
4045
route: req.originalUrl,
4146
timeStamp: new Date(),
4247
success: false,
4348
data: {},
44-
message: error.details
45-
.map((detail) => detail.message)
46-
.join(', '),
49+
message,
4750
status: httpStatus.BAD_REQUEST,
4851
};
4952

src/utilities/errorResponse.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import httpStatus from '../constant/httpStatus.constants.js';
11+
import loggerService from '../service/logger.service.js';
1112

1213
/**
1314
* errorResponse - A function that generates a standardized error response object.
@@ -20,12 +21,17 @@ import httpStatus from '../constant/httpStatus.constants.js';
2021
* @returns {Object} - An object representing the error response, including the timestamp,
2122
* success flag, data payload, error message, and status code.
2223
*/
23-
const errorResponse = (message, status = httpStatus.BAD_REQUEST) => ({
24-
timeStamp: new Date(),
25-
success: false,
26-
data: {},
27-
message,
28-
status,
29-
});
24+
const errorResponse = (message, status = httpStatus.BAD_REQUEST) => {
25+
toString(status).startsWith('5')
26+
? loggerService.error(message)
27+
: loggerService.debug(message);
28+
return {
29+
timeStamp: new Date(),
30+
success: false,
31+
data: {},
32+
message,
33+
status,
34+
};
35+
};
3036

3137
export default errorResponse;

src/utilities/sendResponse.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import httpStatus from '../constant/httpStatus.constants.js';
11+
import loggerService from '../service/logger.service.js';
1112

1213
/**
1314
* sendResponse - A function that creates a standardized response object for successful
@@ -21,12 +22,17 @@ import httpStatus from '../constant/httpStatus.constants.js';
2122
* @returns {Object} - An object representing the response, including the timestamp,
2223
* success flag, data payload, message, and status code.
2324
*/
24-
const sendResponse = (data, message, status = httpStatus.OK) => ({
25-
timeStamp: new Date(),
26-
success: true,
27-
data,
28-
message,
29-
status,
30-
});
25+
const sendResponse = (data, message, status = httpStatus.OK) => {
26+
toString(status).startsWith('5')
27+
? loggerService.error(message)
28+
: loggerService.debug(message);
29+
return {
30+
timeStamp: new Date(),
31+
success: true,
32+
data,
33+
message,
34+
status,
35+
};
36+
};
3137

3238
export default sendResponse;

0 commit comments

Comments
 (0)