From 38848349fd6ea8434d79702333154ac4bc46281d Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 24 Apr 2024 17:17:28 +0100 Subject: [PATCH] add logging --- .../fascinated/backend/service/PasteService.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/cc/fascinated/backend/service/PasteService.java b/src/main/java/cc/fascinated/backend/service/PasteService.java index 1d88f6b..03a192b 100644 --- a/src/main/java/cc/fascinated/backend/service/PasteService.java +++ b/src/main/java/cc/fascinated/backend/service/PasteService.java @@ -4,6 +4,7 @@ import cc.fascinated.backend.exception.impl.BadRequestException; import cc.fascinated.backend.exception.impl.ResourceNotFoundException; import cc.fascinated.backend.model.Paste; import cc.fascinated.backend.repository.PasteRepository; +import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.RandomStringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -11,7 +12,7 @@ import org.springframework.stereotype.Service; import java.util.Optional; -@Service +@Service @Log4j2(topic = "Paste Service") public class PasteService { /** @@ -44,18 +45,22 @@ public class PasteService { */ public String createPaste(String content) { int length = content.length(); + log.info("Creating a new paste. (characters: {})", length); // Check if the content is too large. if (length > uploadSizeLimit && uploadSizeLimit != -1) { + log.info("Paste didn't meet the size requirements. (characters: {})", length); throw new BadRequestException("The paste content is too large, the limit is " + uploadSizeLimit + " characters"); } // Save the paste to the database. - return pasteRepository.save(new Paste( + Paste paste = pasteRepository.save(new Paste( RandomStringUtils.randomAlphabetic(idLength), System.currentTimeMillis(), content - )).getId(); + )); + log.info("Created a new paste with the id '{}'", paste.getId()); + return paste.getId(); } /** @@ -65,12 +70,15 @@ public class PasteService { * @return The content of the paste. */ public Paste getPaste(String id) { + log.info("Getting paste with the id '{}'", id); Optional paste = pasteRepository.findById(id); // The paste does not exist. if (paste.isEmpty()) { + log.info("Paste with the id '{}' not found", id); throw new ResourceNotFoundException("Paste '%s' not found".formatted(id)); } + log.info("Got paste with the id '{}'", id); return paste.get(); } }