From 43efcc43425ac33ffb6c520df4063bb6ffe25c1d Mon Sep 17 00:00:00 2001 From: Elshimaa Betah <79271594+ShimaaBetah@users.noreply.github.com> Date: Wed, 15 May 2024 22:31:21 +0300 Subject: [PATCH] Added Logger for Job service (#91) --- services/jobs/pom.xml | 25 +++++++++ .../java/com/workup/jobs/JobsApplication.java | 9 ++++ .../jobs/commands/AcceptProposalCommand.java | 12 ++++- .../jobs/commands/CreateJobCommand.java | 8 ++- .../jobs/commands/CreateProposalCommand.java | 9 ++-- .../jobs/commands/GetJobByIdCommand.java | 8 +++ .../jobs/commands/GetMyJobsCommand.java | 6 +++ .../jobs/commands/GetMyProposalsCommand.java | 12 +++-- .../commands/GetProposalsByJobIdCommand.java | 8 ++- .../jobs/commands/SearchJobsCommand.java | 11 +++- services/jobs/src/main/resources/log4j2.xml | 52 +++++++++++++++++++ 11 files changed, 148 insertions(+), 12 deletions(-) create mode 100644 services/jobs/src/main/resources/log4j2.xml diff --git a/services/jobs/pom.xml b/services/jobs/pom.xml index 1d89c3f4..53281e1e 100644 --- a/services/jobs/pom.xml +++ b/services/jobs/pom.xml @@ -18,9 +18,30 @@ 21 + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-logging + + + + + com.lmax + disruptor + 3.4.2 + org.springframework.boot spring-boot-starter-amqp + + + org.springframework.boot + spring-boot-starter-logging + + com.workup @@ -80,6 +101,10 @@ 1.18.30 provided + + org.springframework.boot + spring-boot-starter-log4j2 + diff --git a/services/jobs/src/main/java/com/workup/jobs/JobsApplication.java b/services/jobs/src/main/java/com/workup/jobs/JobsApplication.java index 885a33e3..01eb15b9 100644 --- a/services/jobs/src/main/java/com/workup/jobs/JobsApplication.java +++ b/services/jobs/src/main/java/com/workup/jobs/JobsApplication.java @@ -5,6 +5,7 @@ import org.springframework.amqp.core.*; import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; import org.springframework.amqp.support.converter.MessageConverter; +import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @@ -19,6 +20,14 @@ public static void main(String[] args) { SpringApplication.run(JobsApplication.class, args); } + @Bean + public ApplicationRunner runner(AmqpTemplate template) { + return args -> { + System.out.println("ApplicationRunner is executing"); + // Configurator.setLevel("com.workup.jobs", org.apache.logging.log4j.Level.ERROR); + }; + } + @Bean public Queue myQueue() { return new Queue(ServiceQueueNames.JOBS); diff --git a/services/jobs/src/main/java/com/workup/jobs/commands/AcceptProposalCommand.java b/services/jobs/src/main/java/com/workup/jobs/commands/AcceptProposalCommand.java index 61dea683..8e291a36 100644 --- a/services/jobs/src/main/java/com/workup/jobs/commands/AcceptProposalCommand.java +++ b/services/jobs/src/main/java/com/workup/jobs/commands/AcceptProposalCommand.java @@ -17,18 +17,23 @@ import java.util.Optional; import java.util.UUID; import java.util.stream.Collectors; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class AcceptProposalCommand extends JobCommand { + private static final Logger logger = LogManager.getLogger(AcceptProposalCommand.class); @Override public AcceptProposalResponse Run(AcceptProposalRequest request) { + logger.info("[x] Accepting proposal with id: " + request.getProposalId()); try { UUID proposalId = UUID.fromString(request.getProposalId()); ProposalPrimaryKey proposalPrimaryKey = ProposalPrimaryKey.builder().withId(proposalId).withJobId(request.getJobId()).build(); Optional proposals = proposalRepository.findById(proposalPrimaryKey); if (proposals.isEmpty()) { + logger.info("[x] Proposal not found with id: " + proposalId); return AcceptProposalResponse.builder() .withStatusCode(HttpStatusCode.NOT_FOUND) .withErrorMessage("Proposal not found") @@ -36,6 +41,7 @@ public AcceptProposalResponse Run(AcceptProposalRequest request) { } Proposal acceptedProposal = proposals.get(); if (acceptedProposal.getStatus() != ProposalStatus.PENDING) { + logger.info("[x] Proposal is not pending with id: " + proposalId); return AcceptProposalResponse.builder() .withStatusCode(HttpStatusCode.BAD_REQUEST) .withErrorMessage("Proposal is not pending") @@ -45,6 +51,7 @@ public AcceptProposalResponse Run(AcceptProposalRequest request) { UUID jobId = UUID.fromString(request.getJobId()); Optional job = jobRepository.findById(jobId); if (job.isEmpty()) { + logger.info("[x] Job not found with id: " + jobId); return AcceptProposalResponse.builder() .withStatusCode(HttpStatusCode.NOT_FOUND) .withErrorMessage("Job not found") @@ -52,12 +59,14 @@ public AcceptProposalResponse Run(AcceptProposalRequest request) { } Job acceptedJob = job.get(); if (!acceptedJob.isActive()) { + logger.info("[x] Job is no longer active with id: " + jobId); return AcceptProposalResponse.builder() .withStatusCode(HttpStatusCode.BAD_REQUEST) .withErrorMessage("This job is no longer active!") .build(); } if (!acceptedJob.getClientId().equals(request.getUserId())) { + logger.info("[x] User is not the owner of this proposal's job with id: " + jobId); return AcceptProposalResponse.builder() .withStatusCode(HttpStatusCode.UNAUTHORIZED) .withErrorMessage("User is not the owner of this proposal's job!") @@ -67,6 +76,7 @@ public AcceptProposalResponse Run(AcceptProposalRequest request) { proposalRepository.save(acceptedProposal); jobRepository.save(acceptedJob); String contractId = initiateContract(acceptedProposal, acceptedJob, request); + logger.info("[x] Contract Initiated with id" + contractId); return AcceptProposalResponse.builder() .withStatusCode(HttpStatusCode.OK) .withJob(AcceptedJobInfo.builder().withId(request.getJobId()).withIsActive(false).build()) @@ -79,7 +89,7 @@ public AcceptProposalResponse Run(AcceptProposalRequest request) { .withContractId(contractId) .build(); } catch (Exception e) { - e.printStackTrace(); + logger.error("[x] An error occurred while accepting job proposal", e.getMessage()); return AcceptProposalResponse.builder() .withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR) .withErrorMessage("An error occurred while accepting job proposal") diff --git a/services/jobs/src/main/java/com/workup/jobs/commands/CreateJobCommand.java b/services/jobs/src/main/java/com/workup/jobs/commands/CreateJobCommand.java index 1e4731bc..6e654bc7 100644 --- a/services/jobs/src/main/java/com/workup/jobs/commands/CreateJobCommand.java +++ b/services/jobs/src/main/java/com/workup/jobs/commands/CreateJobCommand.java @@ -6,11 +6,15 @@ import com.workup.shared.enums.HttpStatusCode; import java.util.Date; import java.util.UUID; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class CreateJobCommand extends JobCommand { + private static final Logger logger = LogManager.getLogger(CreateJobCommand.class); @Override public CreateJobResponse Run(CreateJobRequest request) { + logger.info("[x] Saving job '" + request.getTitle() + "'"); try { Job job = Job.builder() @@ -27,13 +31,13 @@ public CreateJobResponse Run(CreateJobRequest request) { .withUpdatedAt(new Date()) .build(); Job savedJob = jobRepository.save(job); - System.out.println(" [x] Saved Job '" + savedJob.getTitle()); + logger.info("[x] Job saved with id: " + savedJob.getId()); return CreateJobResponse.builder() .withStatusCode(HttpStatusCode.CREATED) .withJobId(savedJob.getId().toString()) .build(); } catch (Exception e) { - e.printStackTrace(); + logger.error("[x] An error occurred while saving job", e.getMessage()); return CreateJobResponse.builder() .withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR) .withErrorMessage("An error occurred while saving job") diff --git a/services/jobs/src/main/java/com/workup/jobs/commands/CreateProposalCommand.java b/services/jobs/src/main/java/com/workup/jobs/commands/CreateProposalCommand.java index 986e6832..a650a4ad 100644 --- a/services/jobs/src/main/java/com/workup/jobs/commands/CreateProposalCommand.java +++ b/services/jobs/src/main/java/com/workup/jobs/commands/CreateProposalCommand.java @@ -13,12 +13,16 @@ import java.util.Optional; import java.util.UUID; import java.util.stream.Collectors; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class CreateProposalCommand extends JobCommand { + private static final Logger logger = LogManager.getLogger(CreateProposalCommand.class); @Override public CreateProposalResponse Run(CreateProposalRequest request) { + logger.info("[x] Saving proposal for job '" + request.getJobId() + "'"); try { Optional job = jobRepository.findById(UUID.fromString(request.getJobId())); if (!job.isPresent()) { @@ -66,14 +70,13 @@ public CreateProposalResponse Run(CreateProposalRequest request) { .withStatus(ProposalStatus.PENDING) .build(); Proposal savedProposal = proposalRepository.save(proposal); - System.out.println(" [x] Saved Proposal '" + savedProposal.getAttachments()); - System.out.println(" [x] Saved Proposal '" + savedProposal.getMilestones()); + logger.info("[x] Proposal saved with id: " + savedProposal.getPrimaryKey().getId()); return CreateProposalResponse.builder() .withStatusCode(HttpStatusCode.CREATED) .withId(savedProposal.getPrimaryKey().getId().toString()) .build(); } catch (Exception e) { - e.printStackTrace(); + logger.error("[x] An error occurred while saving proposal", e.getMessage()); return CreateProposalResponse.builder() .withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR) .withErrorMessage("An error occurred while saving proposal") diff --git a/services/jobs/src/main/java/com/workup/jobs/commands/GetJobByIdCommand.java b/services/jobs/src/main/java/com/workup/jobs/commands/GetJobByIdCommand.java index 0334e61a..cc2d9fb2 100644 --- a/services/jobs/src/main/java/com/workup/jobs/commands/GetJobByIdCommand.java +++ b/services/jobs/src/main/java/com/workup/jobs/commands/GetJobByIdCommand.java @@ -6,16 +6,21 @@ import com.workup.shared.enums.HttpStatusCode; import java.util.Optional; import java.util.UUID; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class GetJobByIdCommand extends JobCommand { + private static final Logger logger = LogManager.getLogger(GetJobByIdCommand.class); @Override public GetJobByIdResponse Run(GetJobByIdRequest request) { + logger.info("[x] Fetching job with id: " + request.getJobId()); try { UUID jobId = UUID.fromString(request.getJobId()); Optional job = jobRepository.findById(jobId); if (job.isPresent()) { + logger.info("[x] Job found with id: " + jobId); return GetJobByIdResponse.builder() .withId(job.get().getId().toString()) .withTitle(job.get().getTitle()) @@ -31,12 +36,15 @@ public GetJobByIdResponse Run(GetJobByIdRequest request) { .withStatusCode(HttpStatusCode.OK) .build(); } else { + logger.info("[x] Job not found with id: " + jobId); return GetJobByIdResponse.builder() .withStatusCode(HttpStatusCode.NOT_FOUND) .withErrorMessage("Job not found") .build(); } } catch (Exception e) { + logger.error("[x] An error occurred while fetching job", e.getMessage()); + return GetJobByIdResponse.builder() .withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR) .withErrorMessage("An error occurred while fetching job") diff --git a/services/jobs/src/main/java/com/workup/jobs/commands/GetMyJobsCommand.java b/services/jobs/src/main/java/com/workup/jobs/commands/GetMyJobsCommand.java index 60ecb19b..842834b0 100644 --- a/services/jobs/src/main/java/com/workup/jobs/commands/GetMyJobsCommand.java +++ b/services/jobs/src/main/java/com/workup/jobs/commands/GetMyJobsCommand.java @@ -6,14 +6,19 @@ import com.workup.shared.commands.jobs.responses.GetMyJobsResponse; import com.workup.shared.enums.HttpStatusCode; import java.util.List; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class GetMyJobsCommand extends JobCommand { + private static final Logger logger = LogManager.getLogger(GetMyJobsCommand.class); @Override public GetMyJobsResponse Run(GetMyJobsRequest request) { + try { String clientId = request.getUserId(); List jobs = jobRepository.getJobsByClientId(clientId); + logger.info("[x] Jobs fetched for client: " + clientId); return GetMyJobsResponse.builder() .withJobs( jobs.stream() @@ -30,6 +35,7 @@ public GetMyJobsResponse Run(GetMyJobsRequest request) { .withStatusCode(HttpStatusCode.OK) .build(); } catch (Exception e) { + logger.error("[x] An error occurred while fetching jobs", e.getMessage()); return GetMyJobsResponse.builder() .withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR) .withErrorMessage("An error occurred while fetching jobs") diff --git a/services/jobs/src/main/java/com/workup/jobs/commands/GetMyProposalsCommand.java b/services/jobs/src/main/java/com/workup/jobs/commands/GetMyProposalsCommand.java index ef923994..4f4bcc1b 100644 --- a/services/jobs/src/main/java/com/workup/jobs/commands/GetMyProposalsCommand.java +++ b/services/jobs/src/main/java/com/workup/jobs/commands/GetMyProposalsCommand.java @@ -11,17 +11,20 @@ import com.workup.shared.enums.HttpStatusCode; import java.util.ArrayList; import java.util.List; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class GetMyProposalsCommand extends JobCommand { + private static final Logger logger = LogManager.getLogger(GetMyProposalsCommand.class); @Override public GetMyProposalsResponse Run(GetMyProposalsRequest request) { + logger.info("[x] Fetching proposals for freelancer " + request.getUserId()); try { - System.out.println(" [x] Fetching proposals for freelancer " + request.getUserId()); List response = proposalRepository.findByFreelancerId(request.getUserId()); - System.out.println(" [x] Found " + response.size() + " proposals"); - + logger.info( + "[x] Found " + response.size() + " proposals for freelancer " + request.getUserId()); List proposals = new ArrayList<>(); for (Proposal proposal : response) { ArrayList attachments = new ArrayList<>(); @@ -59,11 +62,14 @@ public GetMyProposalsResponse Run(GetMyProposalsRequest request) { .withMilestones(milestones) .build()); } + logger.info( + "[x] Returning " + proposals.size() + " proposals for freelancer " + request.getUserId()); return GetMyProposalsResponse.builder() .withProposals(proposals) .withStatusCode(HttpStatusCode.OK) .build(); } catch (Exception e) { + logger.error("[x] An error occurred while fetching proposals", e.getMessage()); return GetMyProposalsResponse.builder() .withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR) .withErrorMessage("An error occurred while fetching proposals") diff --git a/services/jobs/src/main/java/com/workup/jobs/commands/GetProposalsByJobIdCommand.java b/services/jobs/src/main/java/com/workup/jobs/commands/GetProposalsByJobIdCommand.java index 069ed6a0..d6bc5095 100644 --- a/services/jobs/src/main/java/com/workup/jobs/commands/GetProposalsByJobIdCommand.java +++ b/services/jobs/src/main/java/com/workup/jobs/commands/GetProposalsByJobIdCommand.java @@ -11,15 +11,19 @@ import com.workup.shared.enums.HttpStatusCode; import java.util.ArrayList; import java.util.List; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class GetProposalsByJobIdCommand extends JobCommand { + private static final Logger logger = LogManager.getLogger(GetProposalsByJobIdCommand.class); @Override public GetProposalsByJobIdResponse Run(GetProposalsByJobIdRequest request) { + logger.info("[x] Fetching proposals for job " + request.getJobId()); try { List response = proposalRepository.findByJobId(request.getJobId()); - + logger.info("[x] Found " + response.size() + " proposals for job " + request.getJobId()); List proposals = new ArrayList<>(); for (Proposal proposal : response) { ArrayList attachments = new ArrayList<>(); @@ -57,11 +61,13 @@ public GetProposalsByJobIdResponse Run(GetProposalsByJobIdRequest request) { .withMilestones(milestones) .build()); } + logger.info("[x] Returning " + proposals.size() + " proposals for job " + request.getJobId()); return GetProposalsByJobIdResponse.builder() .withProposals(proposals) .withStatusCode(HttpStatusCode.OK) .build(); } catch (Exception e) { + logger.error("[x] An error occurred while fetching proposals", e.getMessage()); return GetProposalsByJobIdResponse.builder() .withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR) .withErrorMessage("An error occurred while fetching proposals") diff --git a/services/jobs/src/main/java/com/workup/jobs/commands/SearchJobsCommand.java b/services/jobs/src/main/java/com/workup/jobs/commands/SearchJobsCommand.java index c436ad4c..ecb6b361 100644 --- a/services/jobs/src/main/java/com/workup/jobs/commands/SearchJobsCommand.java +++ b/services/jobs/src/main/java/com/workup/jobs/commands/SearchJobsCommand.java @@ -6,27 +6,35 @@ import com.workup.shared.commands.jobs.responses.SearchJobsResponse; import com.workup.shared.enums.HttpStatusCode; import java.nio.ByteBuffer; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.data.cassandra.core.query.CassandraPageRequest; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Slice; public class SearchJobsCommand extends JobCommand { + private static final Logger logger = LogManager.getLogger(SearchJobsCommand.class); @Override public SearchJobsResponse Run(SearchJobsRequest request) { + logger.info("[x] Searching for jobs with query: " + request.getQuery()); try { CassandraPageRequest pageRequest; if (request.getPagingState() != null) { + logger.info("[x] Using paging state: " + request.getPagingState()); PageRequest pageReq = PageRequest.of(0, request.getPageLimit()); ByteBuffer byteBuffer = com.datastax.oss.protocol.internal.util.Bytes.fromHexString(request.getPagingState()); pageRequest = CassandraPageRequest.of(pageReq, byteBuffer); } else { + logger.info("[x] Fetching First page"); pageRequest = CassandraPageRequest.of(0, request.getPageLimit()); } Slice result = jobRepository.searchForJob("%" + request.getQuery() + "%", pageRequest); + // log number of jibs fetched + logger.info("[x] Fetched " + result.getContent().size() + " jobs"); return SearchJobsResponse.builder() .withJobs( result.stream() @@ -45,8 +53,7 @@ public SearchJobsResponse Run(SearchJobsRequest request) { .withStatusCode(HttpStatusCode.OK) .build(); } catch (Exception e) { - System.err.println("ERROR: " + e.getMessage()); - e.printStackTrace(); + logger.error("[x] An error occurred while searching for jobs", e.getMessage()); return SearchJobsResponse.builder() .withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR) .withErrorMessage(e.getMessage()) diff --git a/services/jobs/src/main/resources/log4j2.xml b/services/jobs/src/main/resources/log4j2.xml new file mode 100644 index 00000000..099a2245 --- /dev/null +++ b/services/jobs/src/main/resources/log4j2.xml @@ -0,0 +1,52 @@ + + + logs/jobs + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file