Skip to content

Commit 2126ea4

Browse files
committed
Extract log keys in constants to ensure name consistency across the project
1 parent 1b39628 commit 2126ea4

3 files changed

Lines changed: 32 additions & 15 deletions

File tree

src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.github.stickerifier.stickerify.bot;
22

3+
import static com.github.stickerifier.stickerify.logger.StructuredLogger.EXCEPTION_MESSAGE_LOG_KEY;
4+
import static com.github.stickerifier.stickerify.logger.StructuredLogger.FILE_ID_LOG_KEY;
5+
import static com.github.stickerifier.stickerify.logger.StructuredLogger.FILE_PATH_LOG_KEY;
6+
import static com.github.stickerifier.stickerify.logger.StructuredLogger.ORIGINAL_REQUEST_LOG_KEY;
37
import static com.github.stickerifier.stickerify.logger.StructuredLogger.REQUEST_DETAILS;
48
import static com.github.stickerifier.stickerify.telegram.Answer.CORRUPTED;
59
import static com.github.stickerifier.stickerify.telegram.Answer.ERROR;
@@ -86,7 +90,10 @@ public int process(List<Update> updates) {
8690

8791
@Override
8892
public void onException(TelegramException e) {
89-
LOGGER.at(Level.ERROR).setCause(e).addKeyValue("exception_message", e.getMessage()).log("An unexpected failure occurred");
93+
LOGGER.at(Level.ERROR)
94+
.setCause(e)
95+
.addKeyValue(EXCEPTION_MESSAGE_LOG_KEY, e.getMessage())
96+
.log("An unexpected failure occurred");
9097
}
9198

9299
@Override
@@ -178,10 +185,10 @@ private void processFailure(TelegramRequest request, Exception e, String fileId)
178185
}
179186

180187
if (e instanceof CorruptedFileException) {
181-
LOGGER.at(Level.WARN).addKeyValue("file_id", fileId).log("Unable to reply to the request: the file is corrupted");
188+
LOGGER.at(Level.WARN).addKeyValue(FILE_ID_LOG_KEY, fileId).log("Unable to reply to the request: the file is corrupted");
182189
answerText(CORRUPTED, request);
183190
} else {
184-
LOGGER.at(Level.ERROR).setCause(e).addKeyValue("file_id", fileId).log("Unable to process file");
191+
LOGGER.at(Level.ERROR).setCause(e).addKeyValue(FILE_ID_LOG_KEY, fileId).log("Unable to process file");
185192
answerText(ERROR, request);
186193
}
187194
}
@@ -206,7 +213,7 @@ private boolean processTelegramFailure(TelegramApiException e, boolean logUnmatc
206213
private void answerText(TelegramRequest request) {
207214
var message = request.message();
208215
if (message.text() == null) {
209-
LOGGER.at(Level.INFO).addKeyValue("original_request", message).log("An unhandled message type has been received");
216+
LOGGER.at(Level.INFO).addKeyValue(ORIGINAL_REQUEST_LOG_KEY, message).log("An unhandled message type has been received");
210217
}
211218

212219
answerText(request.getAnswerMessage(), request);
@@ -241,10 +248,10 @@ private static void deleteTempFiles(Set<Path> pathsToDelete) {
241248
for (var path : pathsToDelete) {
242249
try {
243250
if (!Files.deleteIfExists(path)) {
244-
LOGGER.at(Level.INFO).addKeyValue("file_path", path).log("Unable to delete temp file");
251+
LOGGER.at(Level.INFO).addKeyValue(FILE_PATH_LOG_KEY, path).log("Unable to delete temp file");
245252
}
246253
} catch (IOException e) {
247-
LOGGER.at(Level.ERROR).setCause(e).addKeyValue("file_path", path).log("An error occurred trying to delete temp file");
254+
LOGGER.at(Level.ERROR).setCause(e).addKeyValue(FILE_PATH_LOG_KEY, path).log("An error occurred trying to delete temp file");
248255
}
249256
}
250257
}

src/main/java/com/github/stickerifier/stickerify/logger/StructuredLogger.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ public record StructuredLogger(Logger logger) {
1111
public static final ScopedValue<RequestDetails> REQUEST_DETAILS = ScopedValue.newInstance();
1212
public static final ScopedValue<String> MIME_TYPE = ScopedValue.newInstance();
1313

14+
private static final String MIME_TYPE_LOG_KEY = "mime_type";
15+
private static final String REQUEST_DETAILS_LOG_KEY = "request_details";
16+
public static final String EXCEPTION_MESSAGE_LOG_KEY = "exception_message";
17+
public static final String FILE_ID_LOG_KEY = "file_id";
18+
public static final String ORIGINAL_REQUEST_LOG_KEY = "original_request";
19+
public static final String FILE_PATH_LOG_KEY = "file_path";
20+
public static final String STICKER_LOG_KEY = "sticker";
21+
1422
public StructuredLogger(Class<?> clazz) {
1523
this(LoggerFactory.getLogger(clazz));
1624
}
@@ -25,10 +33,10 @@ public LoggingEventBuilder at(Level level) {
2533
var logBuilder = logger.atLevel(level);
2634

2735
if (REQUEST_DETAILS.isBound()) {
28-
logBuilder = logBuilder.addKeyValue("request_details", REQUEST_DETAILS.get());
36+
logBuilder = logBuilder.addKeyValue(REQUEST_DETAILS_LOG_KEY, REQUEST_DETAILS.get());
2937
}
3038
if (MIME_TYPE.isBound()) {
31-
logBuilder = logBuilder.addKeyValue("mime_type", MIME_TYPE.get());
39+
logBuilder = logBuilder.addKeyValue(MIME_TYPE_LOG_KEY, MIME_TYPE.get());
3240
}
3341

3442
return logBuilder;

src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.github.stickerifier.stickerify.media;
22

3+
import static com.github.stickerifier.stickerify.logger.StructuredLogger.FILE_PATH_LOG_KEY;
34
import static com.github.stickerifier.stickerify.logger.StructuredLogger.MIME_TYPE;
5+
import static com.github.stickerifier.stickerify.logger.StructuredLogger.STICKER_LOG_KEY;
46
import static com.github.stickerifier.stickerify.media.MediaConstraints.MATROSKA_FORMAT;
57
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_ANIMATION_DURATION_SECONDS;
68
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_ANIMATION_FILE_SIZE;
@@ -81,7 +83,7 @@ private static String detectMimeType(File file) throws MediaException {
8183
try {
8284
return TIKA.detect(file);
8385
} catch (IOException e) {
84-
LOGGER.at(Level.ERROR).setCause(e).addKeyValue("file_name", file.getName()).log("Unable to retrieve MIME type");
86+
LOGGER.at(Level.ERROR).setCause(e).addKeyValue(FILE_PATH_LOG_KEY, file.getPath()).log("Unable to retrieve MIME type");
8587
throw new MediaException(e);
8688
}
8789
}
@@ -253,7 +255,7 @@ private static boolean isAnimatedStickerCompliant(File file, String mimeType) th
253255
try (var gzipInputStream = new GZIPInputStream(new FileInputStream(file))) {
254256
uncompressedContent = new String(gzipInputStream.readAllBytes(), UTF_8);
255257
} catch (IOException e) {
256-
LOGGER.at(Level.ERROR).setCause(e).addKeyValue("file_name", file.getName()).log("Unable to retrieve gzip content");
258+
LOGGER.at(Level.ERROR).setCause(e).addKeyValue(FILE_PATH_LOG_KEY, file.getPath()).log("Unable to retrieve gzip content");
257259
}
258260

259261
try {
@@ -267,7 +269,7 @@ private static boolean isAnimatedStickerCompliant(File file, String mimeType) th
267269
}
268270
}
269271

270-
LOGGER.at(Level.WARN).addKeyValue("sticker", sticker).log("The animated sticker doesn't meet Telegram's requirements");
272+
LOGGER.at(Level.WARN).addKeyValue(STICKER_LOG_KEY, sticker).log("The animated sticker doesn't meet Telegram's requirements");
271273
} catch (JsonSyntaxException _) {
272274
LOGGER.at(Level.INFO).log("The archive isn't an animated sticker");
273275
}
@@ -448,7 +450,7 @@ private static File createTempFile(String fileExtension) throws FileOperationExc
448450
private static void deleteFile(File file) throws FileOperationException {
449451
try {
450452
if (!Files.deleteIfExists(file.toPath())) {
451-
LOGGER.at(Level.INFO).addKeyValue("file_path", file.toPath()).log("Unable to delete file");
453+
LOGGER.at(Level.INFO).addKeyValue(FILE_PATH_LOG_KEY, file.toPath()).log("Unable to delete file");
452454
}
453455
} catch (IOException e) {
454456
throw new FileOperationException("An error occurred deleting the file", e);
@@ -493,11 +495,11 @@ private static File convertToWebm(File file) throws MediaException, InterruptedE
493495
}
494496
throw new MediaException("FFmpeg two-pass conversion failed", e);
495497
} finally {
496-
var logFileName = logPrefix + "-0.log";
498+
var logFilePath = logPrefix + "-0.log";
497499
try {
498-
deleteFile(new File(logFileName));
500+
deleteFile(new File(logFilePath));
499501
} catch (FileOperationException e) {
500-
LOGGER.at(Level.WARN).setCause(e).addKeyValue("file_name", logFileName).log("Could not delete log file");
502+
LOGGER.at(Level.WARN).setCause(e).addKeyValue(FILE_PATH_LOG_KEY, logFilePath).log("Could not delete log file");
501503
}
502504
}
503505

0 commit comments

Comments
 (0)