Skip to content

Commit 19d0c1f

Browse files
committed
Add file id to default logged fields
1 parent 2126ea4 commit 19d0c1f

3 files changed

Lines changed: 24 additions & 19 deletions

File tree

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

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

33
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;
4+
import static com.github.stickerifier.stickerify.logger.StructuredLogger.FILE_ID_VALUE;
55
import static com.github.stickerifier.stickerify.logger.StructuredLogger.FILE_PATH_LOG_KEY;
66
import static com.github.stickerifier.stickerify.logger.StructuredLogger.ORIGINAL_REQUEST_LOG_KEY;
7-
import static com.github.stickerifier.stickerify.logger.StructuredLogger.REQUEST_DETAILS;
7+
import static com.github.stickerifier.stickerify.logger.StructuredLogger.REQUEST_DETAILS_VALUE;
88
import static com.github.stickerifier.stickerify.telegram.Answer.CORRUPTED;
99
import static com.github.stickerifier.stickerify.telegram.Answer.ERROR;
1010
import static com.github.stickerifier.stickerify.telegram.Answer.FILE_ALREADY_VALID;
@@ -81,7 +81,7 @@ public int process(List<Update> updates) {
8181
updates.forEach(update -> executor.execute(() -> {
8282
if (update.message() != null) {
8383
var request = new TelegramRequest(update.message());
84-
ScopedValue.where(REQUEST_DETAILS, request.toRequestDetails()).run(() -> answer(request));
84+
ScopedValue.where(REQUEST_DETAILS_VALUE, request.toRequestDetails()).run(() -> answer(request));
8585
}
8686
}));
8787

@@ -123,7 +123,7 @@ private void answerFile(TelegramRequest request, TelegramFile file) {
123123
if (file == TelegramFile.NOT_SUPPORTED) {
124124
answerText(ERROR, request);
125125
} else if (file.canBeDownloaded()) {
126-
answerFile(request, file.id());
126+
ScopedValue.where(FILE_ID_VALUE, file.id()).run(() -> answerFile(request, file.id()));
127127
} else {
128128
LOGGER.at(Level.INFO).log("Passed-in file is too large");
129129

@@ -156,7 +156,7 @@ private void answerFile(TelegramRequest request, String fileId) {
156156
} catch (InterruptedException e) {
157157
Thread.currentThread().interrupt();
158158
} catch (Exception e) {
159-
processFailure(request, e, fileId);
159+
processFailure(request, e);
160160
} finally {
161161
deleteTempFiles(pathsToDelete);
162162
}
@@ -176,7 +176,7 @@ private File retrieveFile(String fileId) throws TelegramApiException, FileOperat
176176
}
177177
}
178178

179-
private void processFailure(TelegramRequest request, Exception e, String fileId) {
179+
private void processFailure(TelegramRequest request, Exception e) {
180180
if (e instanceof TelegramApiException telegramException) {
181181
boolean replyToUser = processTelegramFailure(telegramException, false);
182182
if (!replyToUser) {
@@ -185,10 +185,10 @@ private void processFailure(TelegramRequest request, Exception e, String fileId)
185185
}
186186

187187
if (e instanceof CorruptedFileException) {
188-
LOGGER.at(Level.WARN).addKeyValue(FILE_ID_LOG_KEY, fileId).log("Unable to reply to the request: the file is corrupted");
188+
LOGGER.at(Level.WARN).log("Unable to reply to the request: the file is corrupted");
189189
answerText(CORRUPTED, request);
190190
} else {
191-
LOGGER.at(Level.ERROR).setCause(e).addKeyValue(FILE_ID_LOG_KEY, fileId).log("Unable to process file");
191+
LOGGER.at(Level.ERROR).setCause(e).log("Unable to process file");
192192
answerText(ERROR, request);
193193
}
194194
}

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88

99
public record StructuredLogger(Logger logger) {
1010

11-
public static final ScopedValue<RequestDetails> REQUEST_DETAILS = ScopedValue.newInstance();
12-
public static final ScopedValue<String> MIME_TYPE = ScopedValue.newInstance();
11+
public static final ScopedValue<RequestDetails> REQUEST_DETAILS_VALUE = ScopedValue.newInstance();
12+
public static final ScopedValue<String> FILE_ID_VALUE = ScopedValue.newInstance();
13+
public static final ScopedValue<String> MIME_TYPE_VALUE = ScopedValue.newInstance();
1314

14-
private static final String MIME_TYPE_LOG_KEY = "mime_type";
1515
private static final String REQUEST_DETAILS_LOG_KEY = "request_details";
16+
private static final String FILE_ID_LOG_KEY = "file_id";
17+
private static final String MIME_TYPE_LOG_KEY = "mime_type";
1618
public static final String EXCEPTION_MESSAGE_LOG_KEY = "exception_message";
17-
public static final String FILE_ID_LOG_KEY = "file_id";
1819
public static final String ORIGINAL_REQUEST_LOG_KEY = "original_request";
1920
public static final String FILE_PATH_LOG_KEY = "file_path";
2021
public static final String STICKER_LOG_KEY = "sticker";
@@ -24,19 +25,23 @@ public StructuredLogger(Class<?> clazz) {
2425
}
2526

2627
/**
27-
* Creates a {@link LoggingEventBuilder} at the specified level with request details and MIME type information, if set.
28+
* Creates a {@link LoggingEventBuilder} at the specified level enriched with request details,
29+
* file id, and MIME type information when available.
2830
*
2931
* @param level the level of the log
3032
* @return the log builder with context information
3133
*/
3234
public LoggingEventBuilder at(Level level) {
3335
var logBuilder = logger.atLevel(level);
3436

35-
if (REQUEST_DETAILS.isBound()) {
36-
logBuilder = logBuilder.addKeyValue(REQUEST_DETAILS_LOG_KEY, REQUEST_DETAILS.get());
37+
if (REQUEST_DETAILS_VALUE.isBound()) {
38+
logBuilder = logBuilder.addKeyValue(REQUEST_DETAILS_LOG_KEY, REQUEST_DETAILS_VALUE.get());
39+
}
40+
if (FILE_ID_VALUE.isBound()) {
41+
logBuilder = logBuilder.addKeyValue(FILE_ID_LOG_KEY, FILE_ID_VALUE.get());
3742
}
38-
if (MIME_TYPE.isBound()) {
39-
logBuilder = logBuilder.addKeyValue(MIME_TYPE_LOG_KEY, MIME_TYPE.get());
43+
if (MIME_TYPE_VALUE.isBound()) {
44+
logBuilder = logBuilder.addKeyValue(MIME_TYPE_LOG_KEY, MIME_TYPE_VALUE.get());
4045
}
4146

4247
return logBuilder;

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

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

33
import static com.github.stickerifier.stickerify.logger.StructuredLogger.FILE_PATH_LOG_KEY;
4-
import static com.github.stickerifier.stickerify.logger.StructuredLogger.MIME_TYPE;
4+
import static com.github.stickerifier.stickerify.logger.StructuredLogger.MIME_TYPE_VALUE;
55
import static com.github.stickerifier.stickerify.logger.StructuredLogger.STICKER_LOG_KEY;
66
import static com.github.stickerifier.stickerify.media.MediaConstraints.MATROSKA_FORMAT;
77
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_ANIMATION_DURATION_SECONDS;
@@ -69,7 +69,7 @@ public final class MediaHelper {
6969
public static @Nullable File convert(File inputFile) throws Exception {
7070
var mimeType = detectMimeType(inputFile);
7171

72-
return ScopedValue.where(MIME_TYPE, mimeType).call(() -> performConversion(inputFile, mimeType));
72+
return ScopedValue.where(MIME_TYPE_VALUE, mimeType).call(() -> performConversion(inputFile, mimeType));
7373
}
7474

7575
/**

0 commit comments

Comments
 (0)