Skip to content

Commit 812cc19

Browse files
authored
[ITA-2533] Truncate logs from ActiveMqSender (#8)
1 parent a59e011 commit 812cc19

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

servicebuilder-activemq/src/main/java/no/obos/util/servicebuilder/mq/ActiveMqListener.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private void handleMessage(MessageHandler handler, Message message, Session sess
9191

9292
MDC.put(X_OBOS_REQUEST_ID, requestId);
9393

94-
log.info("Received message '{}'", truncateMessageForLogging(text));
94+
log.info("Received message '{}'", ActiveMqUtils.truncateMessageForLogging(text));
9595

9696
handler.handle(new ObjectMapper().readTree(text));
9797
} catch (Exception e) {
@@ -111,24 +111,6 @@ private void handleMessage(MessageHandler handler, Message message, Session sess
111111
}
112112
}
113113

114-
static String truncateMessageForLogging(String text) {
115-
if (StringUtils.isEmpty(text) || text.length() <= MAX_LENGTH_PER_MESSAGE) {
116-
return text;
117-
}
118-
try {
119-
log.info("Truncating message for logging...");
120-
String truncatedText = text.substring(0, MAX_LENGTH_PER_MESSAGE);
121-
String dataKey = "\"data\":";
122-
if (truncatedText.contains(dataKey)) {
123-
return truncatedText.substring(0, truncatedText.indexOf(dataKey)) + dataKey + "\"...\"}";
124-
}
125-
return truncatedText;
126-
} catch (Exception e) {
127-
log.warn("Failed to truncate message for logging");
128-
return text;
129-
}
130-
}
131-
132114
@Override
133115
public void requeueFailedMessages() {
134116
try {

servicebuilder-activemq/src/main/java/no/obos/util/servicebuilder/mq/ActiveMqSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private void queueMessage(String message) {
3131
Session session = ActiveMqUtils.startSession(connection);
3232

3333
try {
34-
log.info("Sending message '{}' to {}", message, queue);
34+
log.info("Sending message '{}' to {}", ActiveMqUtils.truncateMessageForLogging(message), queue);
3535
ActiveMqUtils.queueMessage(session, message, queue);
3636
} finally {
3737
ActiveMqUtils.closeConnection(session, connection);

servicebuilder-activemq/src/main/java/no/obos/util/servicebuilder/mq/ActiveMqUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package no.obos.util.servicebuilder.mq;
22

3+
import lombok.extern.slf4j.Slf4j;
34
import org.apache.activemq.ActiveMQConnection;
5+
import org.apache.commons.lang3.StringUtils;
46
import org.slf4j.MDC;
57

68
import javax.jms.JMSException;
@@ -12,6 +14,7 @@
1214

1315
import static no.obos.util.servicebuilder.model.Constants.X_OBOS_REQUEST_ID;
1416

17+
@Slf4j
1518
class ActiveMqUtils {
1619

1720
private static final String BROKER_URL_JUNIT = "vm://localhost";
@@ -67,4 +70,21 @@ static void closeConnection(Session session, ActiveMQConnection connection) {
6770
}
6871
}
6972

73+
static String truncateMessageForLogging(String text) {
74+
if (StringUtils.isEmpty(text) || text.length() <= ActiveMqListener.MAX_LENGTH_PER_MESSAGE) {
75+
return text;
76+
}
77+
try {
78+
log.info("Truncating message for logging...");
79+
String truncatedText = text.substring(0, ActiveMqListener.MAX_LENGTH_PER_MESSAGE);
80+
String dataKey = "\"data\":";
81+
if (truncatedText.contains(dataKey)) {
82+
return truncatedText.substring(0, truncatedText.indexOf(dataKey)) + dataKey + "\"...\"}";
83+
}
84+
return truncatedText;
85+
} catch (Exception e) {
86+
log.warn("Failed to truncate message for logging");
87+
return text;
88+
}
89+
}
7090
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
import static org.assertj.core.api.Assertions.assertThat;
66

7-
public class ActiveMqListenerLoggingTest {
7+
public class ActiveMqUtilsTruncateLoggingTest {
88

99
@Test
1010
public void handleNull() {
11-
assertThat(ActiveMqListener.truncateMessageForLogging(null)).isEqualTo(null);
11+
assertThat(ActiveMqUtils.truncateMessageForLogging(null)).isEqualTo(null);
1212
}
1313

1414
@Test
1515
public void logNormalLengthMessage() {
1616
String msg = "{\"mottakerId\":13136040,\"message\":\"Hei Adam! Eva ber om å bli lagt til som beboer"
1717
+ " i Lade Alle Hjem Boligsameie. Gå til Min bolig på https://vibbo.no/xxxx for å godta forespørselen.\",\"from\":\"Vibbo\",\"to\":\"99999999\"}";
1818

19-
assertThat(ActiveMqListener.truncateMessageForLogging(msg)).isEqualTo(msg);
19+
assertThat(ActiveMqUtils.truncateMessageForLogging(msg)).isEqualTo(msg);
2020
}
2121
@Test
2222
public void logLongMessageWithDataObject() {
@@ -62,7 +62,7 @@ public void logLongMessageWithDataObject() {
6262
+ "\"owner\":\"OBOS konsernet\",\"classification\":\"Beskyttet\",\"storageRoutine\":\"LS32\",\"template\":null,\"comment\":null,"
6363
+ "\"files\":[{\"name\":\"Kontoutskrift Santander høyrentekonto 280575114.pdf\",\"data\":\"...\"}";
6464

65-
assertThat(ActiveMqListener.truncateMessageForLogging(msg)).isEqualTo(expectedLoggedMessage);
65+
assertThat(ActiveMqUtils.truncateMessageForLogging(msg)).isEqualTo(expectedLoggedMessage);
6666
}
6767

6868
@Test
@@ -101,6 +101,6 @@ public void logLongMessageWithoutDataObject() {
101101
+ "PiK557ML1cUqzG7m6VvvhJtfe3s9woZnP7z/2Xv9q9FR1P+za7ttUDNsW8rtrPfD5baF49+Xvp9jnhd8vX7zkwTRvZnga135Pn/7tz1p9umn7G3U633yi2n27/034d7Z3JrNyk508X0s"
102102
+ "6t+7Dbe55bzf9h/t2u04vqxA1njzf6+3q18ilMOtM7zCp6hcJGls6vJmn76s7N5hb4HJw1nZoqnAyk+/HRrnG0DaVpYs0qmlTF/rM7+cxPneXz8LjW8sNZ2eym8b/FMw59vJuVe4zW"
103103
+ "0vOrgxz+ONt+rmmVN4+TvOurNU1LmPa3h3jyrNs1Hv84foTz3vaZ4UqnOzqf5SnEfjuiQ33H01nRjgAAAI5luh";
104-
assertThat(ActiveMqListener.truncateMessageForLogging(msg).length()).isEqualTo(ActiveMqListener.MAX_LENGTH_PER_MESSAGE);
104+
assertThat(ActiveMqUtils.truncateMessageForLogging(msg).length()).isEqualTo(ActiveMqListener.MAX_LENGTH_PER_MESSAGE);
105105
}
106106
}

0 commit comments

Comments
 (0)