add logging
This commit is contained in:
parent
6ae8929d91
commit
38848349fd
@ -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> 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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user