Add severity to UI logs

This commit is contained in:
aditya.chandel
2025-10-15 23:59:20 -06:00
parent c8a9075afb
commit d9528af522
15 changed files with 106 additions and 58 deletions
@@ -10,12 +10,26 @@ public class LogNotification {
private final Instant timestamp = Instant.now();
private final String message;
private final Severity severity;
public LogNotification(String message) {
public LogNotification(String message, Severity severity) {
this.message = message;
this.severity = severity;
}
public static LogNotification createLogNotification(String message) {
return new LogNotification(message);
public static LogNotification createLogNotification(String message, Severity severity) {
return new LogNotification(message, severity);
}
public static LogNotification info(String message) {
return new LogNotification(message, Severity.INFO);
}
public static LogNotification warn(String message) {
return new LogNotification(message, Severity.WARN);
}
public static LogNotification error(String message) {
return new LogNotification(message, Severity.ERROR);
}
}
@@ -0,0 +1,8 @@
package com.adityachandel.booklore.model.websocket;
public enum Severity {
INFO,
WARN,
ERROR
}
@@ -20,7 +20,6 @@ import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.time.Instant;
import java.util.List;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
@@ -106,7 +105,7 @@ public class BookdropEventHandlerService {
int queueSize = fileQueue.size();
notificationService.sendMessageToPermissions(
Topic.LOG,
new LogNotification("Processing bookdrop file: " + fileName + " (" + queueSize + " files remaining)"),
LogNotification.info("Processing bookdrop file: " + fileName + " (" + queueSize + " files remaining)"),
Set.of(PermissionType.ADMIN, PermissionType.MANIPULATE_LIBRARY)
);
@@ -134,13 +133,13 @@ public class BookdropEventHandlerService {
if (fileQueue.isEmpty()) {
notificationService.sendMessageToPermissions(
Topic.LOG,
new LogNotification("All bookdrop files have finished processing"),
LogNotification.info("All bookdrop files have finished processing"),
Set.of(PermissionType.ADMIN, PermissionType.MANIPULATE_LIBRARY)
);
} else {
notificationService.sendMessageToPermissions(
Topic.LOG,
new LogNotification("Finished processing bookdrop file: " + fileName + " (" + fileQueue.size() + " files remaining)"),
LogNotification.info("Finished processing bookdrop file: " + fileName + " (" + fileQueue.size() + " files remaining)"),
Set.of(PermissionType.ADMIN, PermissionType.MANIPULATE_LIBRARY)
);
}
@@ -5,6 +5,8 @@ import com.adityachandel.booklore.model.dto.request.SendBookByEmailRequest;
import com.adityachandel.booklore.model.entity.BookEntity;
import com.adityachandel.booklore.model.entity.EmailProviderEntity;
import com.adityachandel.booklore.model.entity.EmailRecipientEntity;
import com.adityachandel.booklore.model.websocket.LogNotification;
import com.adityachandel.booklore.model.websocket.Severity;
import com.adityachandel.booklore.model.websocket.Topic;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.repository.EmailProviderRepository;
@@ -54,17 +56,17 @@ public class EmailService {
private void sendEmailInVirtualThread(EmailProviderEntity emailProvider, String recipientEmail, BookEntity book) {
String bookTitle = book.getMetadata().getTitle();
String logMessage = "Email dispatch initiated for book: " + bookTitle + " to " + recipientEmail;
notificationService.sendMessage(Topic.LOG, createLogNotification(logMessage));
notificationService.sendMessage(Topic.LOG, LogNotification.info(logMessage));
log.info(logMessage);
SecurityContextVirtualThread.runWithSecurityContext(() -> {
try {
sendEmail(emailProvider, recipientEmail, book);
String successMessage = "The book: " + bookTitle + " has been successfully sent to " + recipientEmail;
notificationService.sendMessage(Topic.LOG, createLogNotification(successMessage));
notificationService.sendMessage(Topic.LOG, LogNotification.info(successMessage));
log.info(successMessage);
} catch (Exception e) {
String errorMessage = "An error occurred while sending the book: " + bookTitle + " to " + recipientEmail + ". Error: " + e.getMessage();
notificationService.sendMessage(Topic.LOG, createLogNotification(errorMessage));
notificationService.sendMessage(Topic.LOG, LogNotification.error(errorMessage));
log.error(errorMessage, e);
}
});
@@ -7,6 +7,7 @@ import com.adityachandel.booklore.model.dto.request.SendBookByEmailRequest;
import com.adityachandel.booklore.model.entity.BookEntity;
import com.adityachandel.booklore.model.entity.EmailProviderV2Entity;
import com.adityachandel.booklore.model.entity.EmailRecipientV2Entity;
import com.adityachandel.booklore.model.websocket.LogNotification;
import com.adityachandel.booklore.model.websocket.Topic;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.repository.EmailProviderV2Repository;
@@ -62,17 +63,17 @@ public class SendEmailV2Service {
private void sendEmailInVirtualThread(EmailProviderV2Entity emailProvider, String recipientEmail, BookEntity book) {
String bookTitle = book.getMetadata().getTitle();
String logMessage = "Email dispatch initiated for book: " + bookTitle + " to " + recipientEmail;
notificationService.sendMessage(Topic.LOG, createLogNotification(logMessage));
notificationService.sendMessage(Topic.LOG, LogNotification.info(logMessage));
log.info(logMessage);
SecurityContextVirtualThread.runWithSecurityContext(() -> {
try {
sendEmail(emailProvider, recipientEmail, book);
String successMessage = "The book: " + bookTitle + " has been successfully sent to " + recipientEmail;
notificationService.sendMessage(Topic.LOG, createLogNotification(successMessage));
notificationService.sendMessage(Topic.LOG, LogNotification.info(successMessage));
log.info(successMessage);
} catch (Exception e) {
String errorMessage = "An error occurred while sending the book: " + bookTitle + " to " + recipientEmail + ". Error: " + e.getMessage();
notificationService.sendMessage(Topic.LOG, createLogNotification(errorMessage));
notificationService.sendMessage(Topic.LOG, LogNotification.error(errorMessage));
log.error(errorMessage, e);
}
});
@@ -1,6 +1,7 @@
package com.adityachandel.booklore.service.event;
import com.adityachandel.booklore.model.dto.BookLoreUser;
import com.adityachandel.booklore.model.websocket.LogNotification;
import com.adityachandel.booklore.model.websocket.Topic;
import com.adityachandel.booklore.service.user.UserService;
import lombok.AllArgsConstructor;
@@ -24,7 +25,7 @@ public class AdminEventBroadcaster {
.filter(u -> u.getPermissions().isAdmin())
.toList();
for (BookLoreUser admin : admins) {
messagingTemplate.convertAndSendToUser(admin.getUsername(), Topic.LOG.getPath(), createLogNotification(message));
messagingTemplate.convertAndSendToUser(admin.getUsername(), Topic.LOG.getPath(), LogNotification.info(message));
}
}
}
@@ -1,6 +1,7 @@
package com.adityachandel.booklore.service.event;
import com.adityachandel.booklore.model.dto.Book;
import com.adityachandel.booklore.model.websocket.LogNotification;
import com.adityachandel.booklore.model.websocket.Topic;
import com.adityachandel.booklore.service.user.UserService;
import lombok.AllArgsConstructor;
@@ -26,7 +27,7 @@ public class BookEventBroadcaster {
.forEach(u -> {
String username = u.getUsername();
messagingTemplate.convertAndSendToUser(username, Topic.BOOK_ADD.getPath(), book);
messagingTemplate.convertAndSendToUser(username, Topic.LOG.getPath(), createLogNotification("Book added: " + book.getFileName()));
messagingTemplate.convertAndSendToUser(username, Topic.LOG.getPath(), LogNotification.info("Book added: " + book.getFileName()));
});
}
}
@@ -5,6 +5,7 @@ import com.adityachandel.booklore.model.dto.settings.LibraryFile;
import com.adityachandel.booklore.model.entity.BookAdditionalFileEntity;
import com.adityachandel.booklore.model.entity.BookEntity;
import com.adityachandel.booklore.model.entity.LibraryEntity;
import com.adityachandel.booklore.model.websocket.LogNotification;
import com.adityachandel.booklore.model.websocket.Topic;
import com.adityachandel.booklore.repository.BookAdditionalFileRepository;
import com.adityachandel.booklore.repository.LibraryRepository;
@@ -42,17 +43,17 @@ public class LibraryProcessingService {
@Transactional
public void processLibrary(long libraryId) throws IOException {
LibraryEntity libraryEntity = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId));
notificationService.sendMessage(Topic.LOG, createLogNotification("Started processing library: " + libraryEntity.getName()));
notificationService.sendMessage(Topic.LOG, LogNotification.info("Started processing library: " + libraryEntity.getName()));
LibraryFileProcessor processor = fileProcessorRegistry.getProcessor(libraryEntity);
List<LibraryFile> libraryFiles = libraryFileHelper.getLibraryFiles(libraryEntity, processor);
processor.processLibraryFiles(libraryFiles, libraryEntity);
notificationService.sendMessage(Topic.LOG, createLogNotification("Finished processing library: " + libraryEntity.getName()));
notificationService.sendMessage(Topic.LOG, LogNotification.info("Finished processing library: " + libraryEntity.getName()));
}
@Transactional
public void rescanLibrary(RescanLibraryContext context) throws IOException {
LibraryEntity libraryEntity = libraryRepository.findById(context.getLibraryId()).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(context.getLibraryId()));
notificationService.sendMessage(Topic.LOG, createLogNotification("Started refreshing library: " + libraryEntity.getName()));
notificationService.sendMessage(Topic.LOG, LogNotification.info("Started refreshing library: " + libraryEntity.getName()));
LibraryFileProcessor processor = fileProcessorRegistry.getProcessor(libraryEntity);
List<LibraryFile> libraryFiles = libraryFileHelper.getLibraryFiles(libraryEntity, processor);
List<Long> additionalFileIds = detectDeletedAdditionalFiles(libraryFiles, libraryEntity);
@@ -69,7 +70,7 @@ public class LibraryProcessingService {
entityManager.clear();
processor.processLibraryFiles(detectNewBookPaths(libraryFiles, libraryEntity), libraryEntity);
notificationService.sendMessage(Topic.LOG, createLogNotification("Finished refreshing library: " + libraryEntity.getName()));
notificationService.sendMessage(Topic.LOG, LogNotification.info("Finished refreshing library: " + libraryEntity.getName()));
}
public void processLibraryFiles(List<LibraryFile> libraryFiles, LibraryEntity libraryEntity) {
@@ -18,6 +18,7 @@ import com.adityachandel.booklore.model.entity.BookMetadataEntity;
import com.adityachandel.booklore.model.enums.BookFileType;
import com.adityachandel.booklore.model.enums.Lock;
import com.adityachandel.booklore.model.enums.MetadataProvider;
import com.adityachandel.booklore.model.websocket.LogNotification;
import com.adityachandel.booklore.model.websocket.Topic;
import com.adityachandel.booklore.repository.BookMetadataRepository;
import com.adityachandel.booklore.repository.BookRepository;
@@ -192,7 +193,7 @@ public class BookMetadataService {
.filter(book -> book.getMetadata().getCoverLocked() == null || !book.getMetadata().getCoverLocked())
.toList();
int total = books.size();
notificationService.sendMessage(Topic.LOG, createLogNotification("Started regenerating covers for " + total + " books"));
notificationService.sendMessage(Topic.LOG, LogNotification.info("Started regenerating covers for " + total + " books"));
int[] current = {1};
for (BookEntity book : books) {
@@ -204,11 +205,10 @@ public class BookMetadataService {
}
current[0]++;
}
notificationService.sendMessage(Topic.LOG, createLogNotification("Finished regenerating covers"));
notificationService.sendMessage(Topic.LOG, LogNotification.info("Finished regenerating covers"));
} catch (Exception e) {
log.error("Error during cover regeneration: {}", e.getMessage(), e);
notificationService.sendMessage(Topic.LOG, createLogNotification("Error during cover regeneration: " + e.getMessage()));
notificationService.sendMessage(Topic.LOG, LogNotification.error("Error occurred during cover regeneration"));
}
});
}
@@ -216,7 +216,7 @@ public class BookMetadataService {
private void regenerateCoverForBook(BookEntity book, String progress) {
String title = book.getMetadata().getTitle();
String message = progress + "Regenerating cover for: " + title;
notificationService.sendMessage(Topic.LOG, createLogNotification(message));
notificationService.sendMessage(Topic.LOG, LogNotification.info(message));
BookFileProcessor processor = processorRegistry.getProcessorOrThrow(book.getBookType());
processor.generateCover(book);
@@ -6,8 +6,7 @@ import com.adityachandel.booklore.model.entity.BookEntity;
import com.adityachandel.booklore.model.entity.LibraryEntity;
import com.adityachandel.booklore.model.entity.LibraryPathEntity;
import com.adityachandel.booklore.model.enums.BookFileExtension;
import com.adityachandel.booklore.model.enums.BookFileType;
import com.adityachandel.booklore.model.enums.PermissionType;
import com.adityachandel.booklore.model.websocket.LogNotification;
import com.adityachandel.booklore.model.websocket.Topic;
import com.adityachandel.booklore.repository.LibraryRepository;
import com.adityachandel.booklore.service.NotificationService;
@@ -25,7 +24,6 @@ import java.util.Set;
import static com.adityachandel.booklore.model.enums.PermissionType.ADMIN;
import static com.adityachandel.booklore.model.enums.PermissionType.MANIPULATE_LIBRARY;
import static com.adityachandel.booklore.model.websocket.LogNotification.createLogNotification;
@Slf4j
@Service
@@ -52,7 +50,7 @@ public class BookFileTransactionalHandler {
String fileName = path.getFileName().toString();
String libraryPath = bookFilePersistenceService.findMatchingLibraryPath(libraryEntity, path);
notificationService.sendMessageToPermissions(Topic.LOG, createLogNotification("Started processing file: " + filePath), Set.of(ADMIN, MANIPULATE_LIBRARY));
notificationService.sendMessageToPermissions(Topic.LOG, LogNotification.info("Started processing file: " + filePath), Set.of(ADMIN, MANIPULATE_LIBRARY));
LibraryPathEntity libraryPathEntity = bookFilePersistenceService.getLibraryPathEntityForFile(libraryEntity, libraryPath);
@@ -68,7 +66,7 @@ public class BookFileTransactionalHandler {
libraryProcessingService.processLibraryFiles(List.of(libraryFile), libraryEntity);
notificationService.sendMessageToPermissions(Topic.LOG, createLogNotification("Finished processing file: " + filePath), Set.of(ADMIN, MANIPULATE_LIBRARY));
notificationService.sendMessageToPermissions(Topic.LOG, LogNotification.info("Finished processing file: " + filePath), Set.of(ADMIN, MANIPULATE_LIBRARY));
log.info("[CREATE] Completed processing for file '{}'", filePath);
}
}