Skip to content

Commit

Permalink
camel-jbang - Make camel log read spring boot logs
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Apr 29, 2024
1 parent 7df290d commit 4a92560
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {

public PrefixCompletionCandidates() {
Expand Down Expand Up @@ -249,6 +251,7 @@ private int readLogFiles(Map<Long, Row> rows) throws Exception {
try {
line = row.reader.readLine();
if (line != null) {
line = alignTimestamp(line);
boolean valid = true;
if (grep != null) {
valid = isValidGrep(line);
Expand Down Expand Up @@ -290,7 +293,7 @@ private void dumpLogFiles(Map<Long, Row> 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);
Expand Down Expand Up @@ -420,6 +423,7 @@ private void tailLogFiles(Map<Long, Row> 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);
Expand All @@ -435,6 +439,21 @@ private void tailLogFiles(Map<Long, Row> 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;
Expand All @@ -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;
Expand Down Expand Up @@ -498,7 +517,6 @@ private static class Row {
String name;
Queue<String> fifo;
LineNumberReader reader;

}

}

0 comments on commit 4a92560

Please sign in to comment.