Skip to content

Commit a77a568

Browse files
committed
Configure structured logging
1 parent 6f95d49 commit a77a568

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public int process(List<Update> updates) {
7878
updates.forEach(update -> executor.execute(() -> {
7979
if (update.message() != null) {
8080
var request = new TelegramRequest(update.message());
81-
LOGGER.atInfo().log("Received {}", request.getDescription());
81+
LOGGER.atInfo().addKeyValue("request_description", request.getDescription()).log("Received request");
8282

8383
answer(request);
8484
}
@@ -89,7 +89,7 @@ public int process(List<Update> updates) {
8989

9090
@Override
9191
public void onException(TelegramException e) {
92-
LOGGER.atError().log("There was an unexpected failure: {}", e.getMessage());
92+
LOGGER.atError().addKeyValue("exception_message", e.getMessage()).log("There was an unexpected failure");
9393
}
9494

9595
@Override
@@ -176,7 +176,7 @@ private void processFailure(TelegramRequest request, BaseException e, String fil
176176
}
177177

178178
if (e instanceof CorruptedFileException) {
179-
LOGGER.atInfo().log("Unable to reply to the {}: the file is corrupted", request.getDescription());
179+
LOGGER.atInfo().addKeyValue("request_description", request.getDescription()).log("Unable to reply to the request: the file is corrupted");
180180
answerText(CORRUPTED, request);
181181
} else {
182182
LOGGER.atWarn().setCause(e).log("Unable to process the file {}", fileId);
@@ -186,11 +186,11 @@ private void processFailure(TelegramRequest request, BaseException e, String fil
186186

187187
private void processTelegramFailure(String requestDescription, TelegramApiException e, boolean logUnmatchedFailure) {
188188
switch (e.getDescription()) {
189-
case "Bad Request: message to be replied not found" -> LOGGER.atInfo().log("Unable to reply to the {}: the message sent has been deleted", requestDescription);
190-
case "Forbidden: bot was blocked by the user" -> LOGGER.atInfo().log("Unable to reply to the {}: the user blocked the bot", requestDescription);
189+
case "Bad Request: message to be replied not found" -> LOGGER.atInfo().addKeyValue("request_description", requestDescription).log("Unable to reply to the request: the message sent has been deleted");
190+
case "Forbidden: bot was blocked by the user" -> LOGGER.atInfo().addKeyValue("request_description", requestDescription).log("Unable to reply to the request: the user blocked the bot");
191191
default -> {
192192
if (logUnmatchedFailure) {
193-
LOGGER.atError().setCause(e).log("Unable to reply to the {}", requestDescription);
193+
LOGGER.atError().setCause(e).addKeyValue("request_description", requestDescription).log("Unable to reply to the request");
194194
}
195195
}
196196
}
@@ -199,7 +199,7 @@ private void processTelegramFailure(String requestDescription, TelegramApiExcept
199199
private void answerText(TelegramRequest request) {
200200
var message = request.message();
201201
if (message.text() == null) {
202-
LOGGER.atInfo().log("An unhandled message type has been received: {}", message);
202+
LOGGER.atInfo().addKeyValue("request_message", message).log("An unhandled message type has been received");
203203
}
204204

205205
answerText(request.getAnswerMessage(), request);
@@ -234,10 +234,10 @@ private static void deleteTempFiles(Set<Path> pathsToDelete) {
234234
for (var path : pathsToDelete) {
235235
try {
236236
if (!Files.deleteIfExists(path)) {
237-
LOGGER.atInfo().log("Unable to delete temp file {}", path);
237+
LOGGER.atInfo().addKeyValue("file_path", path).log("Unable to delete temp file");
238238
}
239239
} catch (IOException e) {
240-
LOGGER.atError().setCause(e).log("An error occurred trying to delete temp file {}", path);
240+
LOGGER.atError().setCause(e).addKeyValue("file_path", path).log("An error occurred trying to delete temp file");
241241
}
242242
}
243243
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public final class MediaHelper {
9090
return convertToWebp(inputFile);
9191
}
9292
} catch (MediaException e) {
93-
LOGGER.atWarn().setCause(e).log("The file with {} MIME type could not be converted", mimeType);
93+
LOGGER.atWarn().setCause(e).addKeyValue("mime_type", mimeType).log("The file with {} MIME type could not be converted", mimeType);
9494
throw e;
9595
}
9696

@@ -107,11 +107,11 @@ public final class MediaHelper {
107107
private static String detectMimeType(File file) throws MediaException {
108108
try {
109109
var mimeType = TIKA.detect(file);
110-
LOGGER.atDebug().log("The file has {} MIME type", mimeType);
110+
LOGGER.atDebug().addKeyValue("mime_type", mimeType).log("MIME type successfully detected");
111111

112112
return mimeType;
113113
} catch (IOException e) {
114-
LOGGER.atError().log("Unable to retrieve MIME type for file {}", file.getName());
114+
LOGGER.atError().addKeyValue("file_name", file.getName()).log("Unable to retrieve MIME type");
115115
throw new MediaException(e);
116116
}
117117
}
@@ -241,7 +241,7 @@ private static boolean isAnimatedStickerCompliant(File file, String mimeType) th
241241
try (var gzipInputStream = new GZIPInputStream(new FileInputStream(file))) {
242242
uncompressedContent = new String(gzipInputStream.readAllBytes(), UTF_8);
243243
} catch (IOException _) {
244-
LOGGER.atError().log("Unable to retrieve gzip content from file {}", file.getName());
244+
LOGGER.atError().addKeyValue("file_name", file.getName()).log("Unable to retrieve gzip content");
245245
}
246246

247247
try {
@@ -255,7 +255,7 @@ private static boolean isAnimatedStickerCompliant(File file, String mimeType) th
255255
}
256256
}
257257

258-
LOGGER.atWarn().log("The {} doesn't meet Telegram's requirements", sticker);
258+
LOGGER.atWarn().addKeyValue("sticker", sticker).log("The animated sticker doesn't meet Telegram's requirements");
259259
} catch (JsonSyntaxException _) {
260260
LOGGER.atInfo().log("The archive isn't an animated sticker");
261261
}
@@ -436,7 +436,7 @@ private static File createTempFile(String fileExtension) throws FileOperationExc
436436
private static void deleteFile(File file) throws FileOperationException {
437437
try {
438438
if (!Files.deleteIfExists(file.toPath())) {
439-
LOGGER.atInfo().log("Unable to delete file {}", file.toPath());
439+
LOGGER.atInfo().addKeyValue("file_path", file.toPath()).log("Unable to delete file");
440440
}
441441
} catch (IOException e) {
442442
throw new FileOperationException("An error occurred deleting the file", e);
@@ -485,7 +485,7 @@ private static File convertToWebm(File file) throws MediaException, InterruptedE
485485
try {
486486
deleteFile(new File(logFileName));
487487
} catch (FileOperationException e) {
488-
LOGGER.atWarn().setCause(e).log("Could not delete {}", logFileName);
488+
LOGGER.atWarn().setCause(e).addKeyValue("file_name", logFileName).log("Could not delete log file");
489489
}
490490
}
491491

src/main/resources/logback.xml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<configuration>
2+
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
3+
24
<conversionRule conversionWord="msg" class="com.github.stickerifier.stickerify.logger.MessageHighlighter"/>
35
<conversionRule conversionWord="ex" class="com.github.stickerifier.stickerify.logger.ExceptionHighlighter"/>
46

@@ -8,9 +10,31 @@
810
</encoder>
911
</appender>
1012

11-
<root level="${LOG_LEVEL:-info}">
12-
<appender-ref ref="CONSOLE"/>
13+
<appender name="JSON_CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
14+
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
15+
<providers>
16+
<timestamp>
17+
<timeZone>UTC</timeZone>
18+
</timestamp>
19+
<logLevel/>
20+
<threadName/>
21+
<pattern>
22+
<pattern>
23+
{
24+
"class_name": "%logger{0}"
25+
}
26+
</pattern>
27+
</pattern>
28+
<message/>
29+
<keyValuePairs/>
30+
<stackTrace/>
31+
</providers>
32+
</encoder>
33+
</appender>
34+
35+
<root level="${LOG_LEVEL:-INFO}">
36+
<appender-ref ref="JSON_CONSOLE"/>
1337
</root>
1438

15-
<logger name="org.apache.tika" level="info" />
39+
<logger name="org.apache.tika" level="INFO"/>
1640
</configuration>

src/test/java/com/github/stickerifier/stickerify/junit/TempFilesCleanupExtension.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ private void deleteFile(Path path) {
4040
try {
4141
Files.delete(path);
4242

43-
LOGGER.atTrace().log("The file {} has been deleted", path.getFileName());
43+
LOGGER.atTrace().addKeyValue("file_name", path.getFileName()).log("The file has been deleted");
4444
} catch (IOException e) {
45-
LOGGER.atWarn().setCause(e).log("The file {} could not be deleted from the system", path.getFileName());
45+
LOGGER.atWarn().setCause(e).addKeyValue("file_name", path.getFileName()).log("The file could not be deleted from the system");
4646
}
4747
}
4848
}

0 commit comments

Comments
 (0)