From 9174ff55f4b5639107d1a095a4eda19251180ad5 Mon Sep 17 00:00:00 2001 From: poojakarma Date: Wed, 27 Nov 2024 11:53:42 +0530 Subject: [PATCH] updated logger messges --- src/common/filters/exception.filter.ts | 6 ++---- .../adapters/emailService.adapter.ts | 3 +-- .../notification/adapters/pushService.adapter.ts | 2 +- .../notification/adapters/smsService.adapter.ts | 2 +- src/modules/notification/notification.service.ts | 16 ++++++++++------ .../notification_events.service.ts | 16 ++++++++-------- 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/common/filters/exception.filter.ts b/src/common/filters/exception.filter.ts index d437d1d..bd39636 100644 --- a/src/common/filters/exception.filter.ts +++ b/src/common/filters/exception.filter.ts @@ -29,8 +29,7 @@ export class AllExceptionsFilter implements ExceptionFilter { let errorMessage = exception?.message || ERROR_MESSAGES.INTERNAL_SERVER_ERROR; - LoggerUtil.error(`Error occurred on API: ${request.url} Method : ${request.method}`, - errorMessage, 'Notification Service', `requested by userId: ${userId}`, + LoggerUtil.error(errorMessage, `Error occurred on API: ${request.url} Method : ${request.method}`, 'Notification Service', `requested by userId: ${userId}`, ) if (exception instanceof HttpException) { @@ -51,9 +50,8 @@ export class AllExceptionsFilter implements ExceptionFilter { statusCode.toString(), ); LoggerUtil.error(`Database Query Failed on API: ${request.url} requested by userId: ${userId}`, - request.method, (exception as QueryFailedError).message, - 'QueryFailedError') + 'Notification Service', `${userId}`) return response.status(statusCode).json(errorResponse); } diff --git a/src/modules/notification/adapters/emailService.adapter.ts b/src/modules/notification/adapters/emailService.adapter.ts index 810c4e0..3f1d0a1 100644 --- a/src/modules/notification/adapters/emailService.adapter.ts +++ b/src/modules/notification/adapters/emailService.adapter.ts @@ -111,10 +111,9 @@ export class EmailAdapter implements NotificationServiceInterface { if (result.status === 'success') { notificationLogs.status = true; await this.notificationServices.saveNotificationLogs(notificationLogs); - LoggerUtil.error(SUCCESS_MESSAGES.EMAIL_NOTIFICATION_SEND_SUCCESSFULLY); + LoggerUtil.log(SUCCESS_MESSAGES.EMAIL_NOTIFICATION_SEND_SUCCESSFULLY); return result; } - else { throw new Error(`Email not send ${JSON.stringify(result.errors)}`) } diff --git a/src/modules/notification/adapters/pushService.adapter.ts b/src/modules/notification/adapters/pushService.adapter.ts index c742910..284afc2 100644 --- a/src/modules/notification/adapters/pushService.adapter.ts +++ b/src/modules/notification/adapters/pushService.adapter.ts @@ -93,7 +93,7 @@ export class PushAdapter implements NotificationServiceInterface { }); if (result.status === 200 && result.data.name) { - LoggerUtil.error(SUCCESS_MESSAGES.PUSH_NOTIFICATION_SEND_SUCCESSFULLY); + LoggerUtil.log(SUCCESS_MESSAGES.PUSH_NOTIFICATION_SEND_SUCCESSFULLY); notificationLogs.status = true; await this.notificationServices.saveNotificationLogs(notificationLogs); return result; diff --git a/src/modules/notification/adapters/smsService.adapter.ts b/src/modules/notification/adapters/smsService.adapter.ts index 80650f0..a966b61 100644 --- a/src/modules/notification/adapters/smsService.adapter.ts +++ b/src/modules/notification/adapters/smsService.adapter.ts @@ -89,7 +89,7 @@ export class SmsAdapter implements NotificationServiceInterface { to: `+91${notificationData.recipient}`, body: notificationData.body, }); - LoggerUtil.error(SUCCESS_MESSAGES.SMS_NOTIFICATION_SEND_SUCCESSFULLY); + LoggerUtil.log(SUCCESS_MESSAGES.SMS_NOTIFICATION_SEND_SUCCESSFULLY); notificationLogs.status = true; await this.notificationServices.saveNotificationLogs(notificationLogs); return message; diff --git a/src/modules/notification/notification.service.ts b/src/modules/notification/notification.service.ts index a75db21..209518d 100644 --- a/src/modules/notification/notification.service.ts +++ b/src/modules/notification/notification.service.ts @@ -59,7 +59,7 @@ export class NotificationService { // Check if notification template exists const notification_event = await this.notificationActions.findOne({ where: { context, key } }); if (!notification_event) { - LoggerUtil.log(apiId, SUCCESS_MESSAGES.UPDATE_TEMPLATE_API, ERROR_MESSAGES.TEMPLATE_NOT_EXIST, userId); + LoggerUtil.error(`template not found with this ${context} and ${key}`, ERROR_MESSAGES.TEMPLATE_NOTFOUND, apiId, userId); throw new BadRequestException(ERROR_MESSAGES.TEMPLATE_NOTFOUND); } @@ -137,7 +137,12 @@ export class NotificationService { .status(HttpStatus.OK) .json(APIResponse.success(apiId, finalResponses, 'OK')); } catch (e) { - LoggerUtil.error(apiId, SUCCESS_MESSAGES.UPDATE_TEMPLATE_API, ERROR_MESSAGES.TEMPLATE_NOT_EXIST, userId); + LoggerUtil.error( + `Error: ${e}`, + e, + apiId, + userId + ); throw e; } } @@ -146,7 +151,7 @@ export class NotificationService { if (recipients && recipients.length > 0 && Object.keys(recipients).length > 0) { const notification_details = await this.notificationActionTemplates.find({ where: { actionId: notification_event.actionId, type } }); if (notification_details.length === 0) { - LoggerUtil.error(`/Send ${channel} Notification`, SUCCESS_MESSAGES.UPDATE_TEMPLATE_API, ERROR_MESSAGES.TEMPLATE_NOT_EXIST, userId); + LoggerUtil.error(ERROR_MESSAGES.TEMPLATE_CONFIG_NOTFOUND, `/Send ${channel} Notification`, userId); throw new BadRequestException(`${ERROR_MESSAGES.TEMPLATE_CONFIG_NOTFOUND} ${type}`); } let bodyText; @@ -198,7 +203,7 @@ export class NotificationService { } return { status: 200, message: SUCCESS_MESSAGES.NOTIFICATION_QUEUE_SAVE_SUCCESSFULLY }; } catch (error) { - LoggerUtil.error('/send', SUCCESS_MESSAGES.UPDATE_TEMPLATE_API, ERROR_MESSAGES.TEMPLATE_NOT_EXIST, userId); + LoggerUtil.error(ERROR_MESSAGES.NOTIFICATION_QUEUE_SAVE_FAILED, error, 'Saving in Qaueue', userId); throw new Error(ERROR_MESSAGES.NOTIFICATION_QUEUE_SAVE_FAILED); } } else { @@ -228,7 +233,6 @@ export class NotificationService { // Function to validate that all placeholders have corresponding replacements validatePlaceholders(placeholders: string[], replacements: { [key: string]: string }): void { const missingReplacements = placeholders.filter((placeholder) => !replacements.hasOwnProperty(placeholder)); - if (missingReplacements.length > 0) { throw new BadRequestException(`Missing replacements for placeholders: ${missingReplacements.join(', ')}`); } @@ -359,7 +363,7 @@ export class NotificationService { await this.notificationLogRepository.save(notificationLogs); } catch (e) { - LoggerUtil.error(SUCCESS_MESSAGES.SAVE_NOTIFICATION_LOG, `error ${e}`, ERROR_MESSAGES.NOTIFICATION_LOG_SAVE_FAILED); + LoggerUtil.error(`error ${e}`, ERROR_MESSAGES.NOTIFICATION_LOG_SAVE_FAILED); throw new Error(ERROR_MESSAGES.NOTIFICATION_LOG_SAVE_FAILED); } } diff --git a/src/modules/notification_events/notification_events.service.ts b/src/modules/notification_events/notification_events.service.ts index 1fe6994..b5c2e2b 100644 --- a/src/modules/notification_events/notification_events.service.ts +++ b/src/modules/notification_events/notification_events.service.ts @@ -28,7 +28,7 @@ export class NotificationEventsService { where: { context: data.context, key: data.key }, }); if (existingTemplate) { - LoggerUtil.log(apiId, SUCCESS_MESSAGES.CREATE_TEMPLATE_API, ERROR_MESSAGES.TEMPLATE_ALREADY_EXIST, userId); + LoggerUtil.error(ERROR_MESSAGES.TEMPLATE_ALREADY_EXIST, ERROR_MESSAGES.NOT_FOUND, apiId, userId); throw new BadRequestException(ERROR_MESSAGES.TEMPLATE_ALREADY_EXIST); } @@ -70,7 +70,7 @@ export class NotificationEventsService { if (data.sms && Object.keys(data.sms).length > 0) { await createConfig('sms', data.sms); } - LoggerUtil.log(SUCCESS_MESSAGES.TEMPLATE_CREATED_SUCESSFULLY(userId), `templateId: ${notificationTemplateResult.actionId}`, '/create/template'); + LoggerUtil.log(SUCCESS_MESSAGES.TEMPLATE_CREATED_SUCESSFULLY(userId), apiId, userId); return response .status(HttpStatus.CREATED) .json(APIResponse.success(apiId, notificationTemplateResult, 'Created')); @@ -89,7 +89,7 @@ export class NotificationEventsService { where: { actionId: id }, }); if (!existingTemplate) { - LoggerUtil.log(apiId, SUCCESS_MESSAGES.UPDATE_TEMPLATE_API, ERROR_MESSAGES.TEMPLATE_NOT_EXIST, userId); + LoggerUtil.error(ERROR_MESSAGES.TEMPLATE_NOT_EXIST, ERROR_MESSAGES.NOT_FOUND, apiId, userId); throw new BadRequestException(ERROR_MESSAGES.TEMPLATE_NOT_EXIST); } //check key already exist for this context @@ -99,7 +99,7 @@ export class NotificationEventsService { where: { context: existingTemplate.context, key: updateEventDto.key }, }); if (checkKeyAlreadyExist) { - LoggerUtil.error(apiId, ERROR_MESSAGES.ALREADY_EXIST_KEY_FOR_CONTEXT, `requested By ${userId}`); + LoggerUtil.error(ERROR_MESSAGES.ALREADY_EXIST_KEY_FOR_CONTEXT, ERROR_MESSAGES.INVALID_REQUEST, apiId, `${userId}`); throw new BadRequestException(ERROR_MESSAGES.ALREADY_EXIST_KEY_FOR_CONTEXT_ENTER_ANOTHER); } } @@ -160,7 +160,7 @@ export class NotificationEventsService { } }); } - LoggerUtil.log(`Template updated successfully by userId: ${userId}`, `Id: ${id}`, '/update/template'); + LoggerUtil.log(`Template updated successfully by userId: ${userId}`, apiId, userId); return response .status(HttpStatus.OK) .json(APIResponse.success(apiId, { id: id }, 'OK')); @@ -192,7 +192,7 @@ export class NotificationEventsService { }, {}); return { ...rest, templates: formattedTemplateConfig }; }); - LoggerUtil.log(SUCCESS_MESSAGES.GET_TEMPLATE(userId), '/get/template'); + LoggerUtil.log(SUCCESS_MESSAGES.GET_TEMPLATE(userId), apiId, userId,); return response .status(HttpStatus.OK) .json(APIResponse.success(apiId, finalResult, 'OK')); @@ -202,14 +202,14 @@ export class NotificationEventsService { const apiId = APIID.TEMPLATE_DELETE; const templateId = await this.notificationTemplatesRepository.findOne({ where: { actionId } }); if (!templateId) { - LoggerUtil.log(apiId, SUCCESS_MESSAGES.UPDATE_TEMPLATE_API, ERROR_MESSAGES.TEMPLATE_NOT_EXIST, userId); + LoggerUtil.error(ERROR_MESSAGES.TEMPLATE_NOT_EXIST, ERROR_MESSAGES.NOT_FOUND, apiId, userId); throw new NotFoundException(ERROR_MESSAGES.TEMPLATE_ID_NOTFOUND(actionId)) } const deleteTemplate = await this.notificationTemplatesRepository.delete({ actionId }); if (deleteTemplate.affected !== 1) { throw new BadRequestException(ERROR_MESSAGES.TEMPLATE_NOT_DELETED); } - LoggerUtil.log(SUCCESS_MESSAGES.DELETE_TEMPLATE(userId), '/delete/template'); + LoggerUtil.log(SUCCESS_MESSAGES.DELETE_TEMPLATE(userId), apiId, userId); return response .status(HttpStatus.OK) .json(APIResponse.success(apiId, { id: actionId }, 'OK'));