Skip to content

Commit fbb8117

Browse files
committed
feat: support different formats for cached log lines
1 parent 1a9c63e commit fbb8117

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

cloudnet-rest-module/src/main/java/eu/cloudnetservice/ext/modules/rest/v3/V3HttpHandlerNode.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import jakarta.inject.Singleton;
5959
import jakarta.validation.Valid;
6060
import java.nio.charset.StandardCharsets;
61+
import java.util.Locale;
6162
import java.util.Map;
6263
import java.util.concurrent.atomic.AtomicInteger;
6364
import java.util.concurrent.atomic.AtomicReference;
@@ -217,8 +218,24 @@ public V3HttpHandlerNode(
217218

218219
@RequestHandler(path = "/api/v3/node/logLines")
219220
@Authentication(providers = "jwt", scopes = {"cloudnet_rest:node_read", "cloudnet_rest:node_log_lines"})
220-
public @NonNull IntoResponse<?> handleLogLinesRequest() {
221-
return JsonResponse.builder().body(Map.of("lines", this.consoleLogAppender.formattedCachedLogLines()));
221+
public @NonNull IntoResponse<?> handleLogLinesRequest(
222+
@NonNull @Optional @FirstRequestQueryParam(value = "format", def = "raw") String formatType
223+
) {
224+
return switch (formatType.toLowerCase(Locale.ROOT)) {
225+
case "raw" -> {
226+
var lines = this.consoleLogAppender.cachedLogEntries().stream()
227+
.map(ILoggingEvent::getFormattedMessage)
228+
.toList();
229+
yield JsonResponse.builder().body(lines);
230+
}
231+
case "ansi" -> JsonResponse.builder().body(this.consoleLogAppender.cachedLogEntries());
232+
default -> ProblemDetail.builder()
233+
.type("console-invalid-formatting-type")
234+
.title("Console Invalid Formatting Type")
235+
.status(HttpResponseCode.BAD_REQUEST)
236+
.detail("The cached log lines do not support the format " + formatType)
237+
.build();
238+
};
222239
}
223240

224241
private void reloadConfig() {

cloudnet-rest-module/src/main/resources/documentation/swagger.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,21 @@ paths:
714714
get:
715715
tags:
716716
- Node
717+
parameters:
718+
- name: format
719+
in: query
720+
required: false
721+
description: |
722+
Whether to send the cached log lines as plain text or as ansi formatted text.
723+
- `raw`: The log lines are sent as plain text
724+
- `ansi`: The log lines are sent as ansi formatted text
725+
726+
If no value is provided the default value is `raw`.
717727
summary: Get the cached log lines of the node the request is sent to
718728
description: |
729+
Based on the requested formatting the log lines are either returned as text only, where no color and formatting
730+
is applied or as ansi formatted text, where the color and formatting is applied.
731+
719732
One of the following scopes is needed to execute the request:
720733
- `cloudnet_rest:node_read`
721734
- `cloudnet_rest:node_log_lines`

0 commit comments

Comments
 (0)