Skip to content

Commit a2aae5c

Browse files
committed
Enrich logged context information
1 parent 730e80d commit a2aae5c

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

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

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

3-
import static com.github.stickerifier.stickerify.logger.StructuredLogger.USER_ID;
3+
import static com.github.stickerifier.stickerify.logger.StructuredLogger.REQUEST_DETAILS;
44
import static com.github.stickerifier.stickerify.telegram.Answer.CORRUPTED;
55
import static com.github.stickerifier.stickerify.telegram.Answer.ERROR;
66
import static com.github.stickerifier.stickerify.telegram.Answer.FILE_ALREADY_VALID;
@@ -77,7 +77,7 @@ public int process(List<Update> updates) {
7777
updates.forEach(update -> executor.execute(() -> {
7878
if (update.message() != null) {
7979
var request = new TelegramRequest(update.message());
80-
ScopedValue.where(USER_ID, request.getUserId()).run(() -> answer(request));
80+
ScopedValue.where(REQUEST_DETAILS, request.toRequestDetails()).run(() -> answer(request));
8181
}
8282
}));
8383

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

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

3+
import com.github.stickerifier.stickerify.telegram.model.TelegramRequest.RequestDetails;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56
import org.slf4j.event.Level;
67
import org.slf4j.spi.LoggingEventBuilder;
78

89
public record StructuredLogger(Logger logger) {
910

10-
public static final ScopedValue<Long> USER_ID = ScopedValue.newInstance();
11+
public static final ScopedValue<RequestDetails> REQUEST_DETAILS = ScopedValue.newInstance();
1112
public static final ScopedValue<String> MIME_TYPE = ScopedValue.newInstance();
1213

1314
public StructuredLogger(Class<?> clazz) {
1415
this(LoggerFactory.getLogger(clazz));
1516
}
1617

18+
/**
19+
* Creates a {@link LoggingEventBuilder} at the specified level with request details and MIME type information, if set.
20+
*
21+
* @param level the level of the log
22+
* @return the log builder with context information
23+
*/
1724
public LoggingEventBuilder at(Level level) {
1825
var logBuilder = logger.atLevel(level);
1926

20-
if (USER_ID.isBound()) {
21-
logBuilder = logBuilder.addKeyValue("user_id", USER_ID.get());
27+
if (REQUEST_DETAILS.isBound()) {
28+
logBuilder = logBuilder.addKeyValue("request_details", REQUEST_DETAILS.get());
2229
}
2330
if (MIME_TYPE.isBound()) {
2431
logBuilder = logBuilder.addKeyValue("mime_type", MIME_TYPE.get());

src/main/java/com/github/stickerifier/stickerify/telegram/model/TelegramRequest.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static com.github.stickerifier.stickerify.telegram.Answer.PRIVACY_POLICY;
66
import static java.util.Comparator.comparing;
77

8+
import com.fasterxml.jackson.annotation.JsonProperty;
89
import com.github.stickerifier.stickerify.telegram.Answer;
910
import com.pengrad.telegrambot.model.Document;
1011
import com.pengrad.telegrambot.model.Message;
@@ -25,7 +26,7 @@
2526
* @param message the message to wrap
2627
*/
2728
public record TelegramRequest(Message message) {
28-
public static final String NEW_USER = " (new user)";
29+
2930
private static final String START_COMMAND = "/start";
3031
private static final String HELP_COMMAND = "/help";
3132
private static final String PRIVACY_COMMAND = "/privacy";
@@ -77,24 +78,12 @@ public Integer getMessageId() {
7778
return message.messageId();
7879
}
7980

80-
/**
81-
* Creates a String describing the current request,
82-
* writing <b>only</b> the user identifier and if the sender is a new user.
83-
*
84-
* @return the description of the request
85-
*/
86-
public String getDescription() {
87-
var description = "request from user " + getUserId();
88-
89-
if (START_COMMAND.equals(message.text())) {
90-
description += NEW_USER;
91-
}
92-
93-
return description;
81+
private Long getUserId() {
82+
return message.from().id();
9483
}
9584

96-
public Long getUserId() {
97-
return message.from().id();
85+
private boolean isNewUser() {
86+
return START_COMMAND.equals(message.text());
9887
}
9988

10089
public Answer getAnswerMessage() {
@@ -105,6 +94,10 @@ public Answer getAnswerMessage() {
10594
};
10695
}
10796

97+
public RequestDetails toRequestDetails() {
98+
return new RequestDetails(getUserId(), isNewUser());
99+
}
100+
108101
@Override
109102
public String toString() {
110103
var file = Optional.ofNullable(getFile()).map(TelegramFile::id).orElse(null);
@@ -123,4 +116,6 @@ private static String writeIfNotEmpty(String field, @Nullable String value) {
123116
? ", " + field + "=" + value
124117
: "";
125118
}
119+
120+
public record RequestDetails(@JsonProperty("user_id") Long userId, @JsonProperty("new_user") boolean isNewUser) {}
126121
}

0 commit comments

Comments
 (0)