From 4a92560bcd0971b4dd7bf2a91a44729c0d77cdf6 Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Mon, 29 Apr 2024 15:43:45 +0200 Subject: [PATCH] camel-jbang - Make camel log read spring boot logs --- .../core/commands/action/CamelLogAction.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java index 2310d30959feb..7a2a518e82e40 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java @@ -54,6 +54,8 @@ public class CamelLogAction extends ActionBaseCommand { private static final int NAME_MAX_WIDTH = 25; private static final int NAME_MIN_WIDTH = 10; + private static final String TIMESTAMP_MAIN = "yyyy-MM-dd HH:mm:ss.SSS"; + public static class PrefixCompletionCandidates implements Iterable { public PrefixCompletionCandidates() { @@ -249,6 +251,7 @@ private int readLogFiles(Map rows) throws Exception { try { line = row.reader.readLine(); if (line != null) { + line = alignTimestamp(line); boolean valid = true; if (grep != null) { valid = isValidGrep(line); @@ -290,7 +293,7 @@ private void dumpLogFiles(Map rows, int tail) { // only sort if there are multiple Camels running if (names.size() > 1) { // sort lines - final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + final SimpleDateFormat sdf = new SimpleDateFormat(TIMESTAMP_MAIN); lines.sort((l1, l2) -> { l1 = unescapeAnsi(l1); l2 = unescapeAnsi(l2); @@ -420,6 +423,7 @@ private void tailLogFiles(Map rows, int tail, Date limit) throws Exce do { line = row.reader.readLine(); if (line != null) { + line = alignTimestamp(line); boolean valid = isValidSince(limit, line); if (valid && grep != null) { valid = isValidGrep(line); @@ -435,6 +439,21 @@ private void tailLogFiles(Map rows, int tail, Date limit) throws Exce } } + private String alignTimestamp(String line) { + // if using spring boot then adjust the timestamp to uniform camel-main style + String ts = StringHelper.before(line, " "); + if (ts != null && ts.contains("T")) { + ts = ts.replace('T', ' '); + int dot = ts.indexOf('.'); + if (dot != -1) { + ts = ts.substring(0, dot + 4); + } + String after = StringHelper.after(line, " "); + return ts + " " + after; + } + return line; + } + private boolean isValidSince(Date limit, String line) { if (limit == null) { return true; @@ -443,7 +462,7 @@ private boolean isValidSince(Date limit, String line) { line = unescapeAnsi(line); String ts = StringHelper.before(line, " "); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + SimpleDateFormat sdf = new SimpleDateFormat(TIMESTAMP_MAIN); try { Date row = sdf.parse(ts); return row.compareTo(limit) >= 0; @@ -498,7 +517,6 @@ private static class Row { String name; Queue fifo; LineNumberReader reader; - } }