Add splitting by newline to console message as well

This commit is contained in:
Vankka 2023-10-27 20:04:40 +03:00
parent 0185ef6e21
commit ac84845d67
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0

View File

@ -304,6 +304,14 @@ public class SingleConsoleHandler {
mostRecentMessageId = receivedMessage.getId(); mostRecentMessageId = receivedMessage.getId();
} }
}); });
}).exceptionally(ex -> {
String error = "Failed to send message to console channel";
if (message.contains(error)) {
// Prevent infinite loop of the same error
return null;
}
logger.error(error, ex);
return null;
}); });
} }
} }
@ -357,26 +365,15 @@ public class SingleConsoleHandler {
List<String> formatted = new ArrayList<>(); List<String> formatted = new ArrayList<>();
// Handle message being longer than a message // Handle message being longer than a message
message = cutToSizeIfNeeded(message, blockLength, maximumPart, formatted); if (message.length() > MESSAGE_MAX_LENGTH) {
message = chopOnNewlines(message, blockLength, maximumPart, formatted);
}
// Handle log entry being longer than a message // Handle log entry being longer than a message
int totalLength = blockLength + throwable.length() + message.length(); int totalLength = blockLength + throwable.length() + message.length();
if (totalLength > MESSAGE_MAX_LENGTH) { if (totalLength > MESSAGE_MAX_LENGTH) {
StringBuilder builder = new StringBuilder(message); String remainingPart = chopOnNewlines(message, blockLength, maximumPart, formatted);
for (String line : throwable.split("\n")) { formatted.add(remainingPart);
line += "\n";
// Handle a single line of a throwable being longer than a message
line = cutToSizeIfNeeded(line, blockLength, maximumPart, formatted);
if (blockLength + line.length() > MESSAGE_MAX_LENGTH) {
// Need to split here
formatted.add(builder.toString());
builder.setLength(0);
}
builder.append(line);
}
formatted.add(builder.toString());
} else { } else {
formatted.add(message + throwable); formatted.add(message + throwable);
} }
@ -384,6 +381,26 @@ public class SingleConsoleHandler {
return formatted; return formatted;
} }
private String chopOnNewlines(String input, int blockLength, int maximumPart, List<String> formatted) {
if (!input.contains("\n")) {
return cutToSizeIfNeeded(input, blockLength, maximumPart, formatted);
}
StringBuilder builder = new StringBuilder();
for (String line : input.split("\n")) {
line += "\n";
line = cutToSizeIfNeeded(line, blockLength, maximumPart, formatted);
if (blockLength + line.length() + builder.length() > MESSAGE_MAX_LENGTH) {
formatted.add(builder.toString());
builder.setLength(0);
}
builder.append(line);
}
return builder.toString();
}
private String cutToSizeIfNeeded( private String cutToSizeIfNeeded(
String content, String content,
int blockLength, int blockLength,