Skip to content

Commit fcad1fa

Browse files
committed
Merge pull request #43931 from nosan
* pr/43931: Polish SystemStatusListener Closes gh-43931
2 parents 8e15317 + 4878fc2 commit fcad1fa

File tree

1 file changed

+11
-34
lines changed

1 file changed

+11
-34
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/SystemStatusListener.java

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@
2424
import ch.qos.logback.core.status.OnConsoleStatusListener;
2525
import ch.qos.logback.core.status.Status;
2626
import ch.qos.logback.core.status.StatusListener;
27-
import ch.qos.logback.core.status.StatusManager;
28-
import ch.qos.logback.core.util.StatusPrinter2;
27+
import ch.qos.logback.core.util.StatusListenerConfigHelper;
2928

3029
/**
3130
* {@link StatusListener} used to print appropriate status messages to {@link System#out}
3231
* or {@link System#err}. Note that this class extends {@link OnConsoleStatusListener} so
3332
* that {@link BasicStatusManager#add(StatusListener)} does not add the same listener
34-
* twice. It also implement a version of retrospectivePrint that can filter status
33+
* twice. It also implements a version of retrospectivePrint that can filter status
3534
* messages by level.
3635
*
3736
* @author Dmytro Nosan
@@ -41,8 +40,6 @@ final class SystemStatusListener extends OnConsoleStatusListener {
4140

4241
static final long RETROSPECTIVE_THRESHOLD = 300;
4342

44-
private static final StatusPrinter2 PRINTER = new StatusPrinter2();
45-
4643
private final boolean debug;
4744

4845
private SystemStatusListener(boolean debug) {
@@ -63,53 +60,33 @@ private void retrospectivePrint() {
6360
}
6461
long now = System.currentTimeMillis();
6562
List<Status> statusList = this.context.getStatusManager().getCopyOfStatusList();
66-
statusList.stream().filter((status) -> isPrintable(status, now)).forEach(this::print);
67-
}
68-
69-
private void print(Status status) {
70-
StringBuilder sb = new StringBuilder();
71-
PRINTER.buildStr(sb, "", status);
72-
getPrintStream().print(sb);
63+
statusList.stream()
64+
.filter((status) -> getElapsedTime(status, now) < RETROSPECTIVE_THRESHOLD)
65+
.forEach(this::addStatusEvent);
7366
}
7467

7568
@Override
7669
public void addStatusEvent(Status status) {
77-
if (isPrintable(status, 0)) {
70+
if (this.debug || status.getLevel() >= Status.WARN) {
7871
super.addStatusEvent(status);
7972
}
8073
}
8174

82-
private boolean isPrintable(Status status, long now) {
83-
boolean timstampInRange = (now == 0 || (now - status.getTimestamp()) < RETROSPECTIVE_THRESHOLD);
84-
return timstampInRange && (this.debug || status.getLevel() >= Status.WARN);
85-
}
86-
8775
@Override
8876
protected PrintStream getPrintStream() {
8977
return (!this.debug) ? System.err : System.out;
9078
}
9179

80+
private static long getElapsedTime(Status status, long now) {
81+
return now - status.getTimestamp();
82+
}
83+
9284
static void addTo(LoggerContext loggerContext) {
9385
addTo(loggerContext, false);
9486
}
9587

9688
static void addTo(LoggerContext loggerContext, boolean debug) {
97-
SystemStatusListener listener = new SystemStatusListener(debug);
98-
listener.setContext(loggerContext);
99-
StatusManager statusManager = loggerContext.getStatusManager();
100-
if (statusManager.add(listener)) {
101-
listener.start();
102-
}
103-
}
104-
105-
@Override
106-
public boolean equals(Object obj) {
107-
return (obj != null) && (obj.getClass() == getClass());
108-
}
109-
110-
@Override
111-
public int hashCode() {
112-
return getClass().hashCode();
89+
StatusListenerConfigHelper.addOnConsoleListenerInstance(loggerContext, new SystemStatusListener(debug));
11390
}
11491

11592
}

0 commit comments

Comments
 (0)