Skip to content

Commit

Permalink
[FOLLOWUP] Fix StatusLogger log level filtering when debug mode is en…
Browse files Browse the repository at this point in the history
…abled
  • Loading branch information
panbingkun committed Mar 2, 2024
1 parent c542041 commit 0407c14
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,28 @@ public class StatusConsoleListener implements StatusListener {
// `volatile` is necessary to correctly read the `stream` without holding the lock
private volatile PrintStream stream;

// whether or not enables debug mode
private final boolean debugEnabled;

/**
* Constructs a {@link StatusConsoleListener} instance writing to {@link System#out} using the supplied level.
*
* @param level the level of status messages that should appear on the console
* @throws NullPointerException on null {@code level}
*/
public StatusConsoleListener(final Level level) {
this(level, System.out);
this(level, false);
}

/**
* Constructs a {@link StatusConsoleListener} instance writing to {@link System#out} using the supplied level.
*
* @param level the level of status messages that should appear on the console
* @param debugEnabled whether or not enables debug mode
* @throws NullPointerException on null {@code level}
*/
public StatusConsoleListener(final Level level, final boolean debugEnabled) {
this(level, System.out, debugEnabled);
}

/**
Expand All @@ -65,8 +79,25 @@ public StatusConsoleListener(final Level level) {
* @throws NullPointerException on null {@code level} or {@code stream}
*/
public StatusConsoleListener(final Level level, final PrintStream stream) {
this(level, stream, false);
}

/**
* Constructs a {@link StatusConsoleListener} instance using the supplied level and stream.
* <p>
* Make sure not to use a logger stream of some sort to avoid creating an infinite loop of indirection!
* </p>
*
* @param level the level of status messages that should appear on the console
* @param stream the stream to write to
* @param debugEnabled whether or not enables debug mode
* @throws NullPointerException on null {@code level} or {@code stream}
*/
public StatusConsoleListener(final Level level, final PrintStream stream,
final boolean debugEnabled) {
this.initialLevel = this.level = requireNonNull(level, "level");
this.initialStream = this.stream = requireNonNull(stream, "stream");
this.debugEnabled = debugEnabled;
}

/**
Expand Down Expand Up @@ -134,7 +165,7 @@ public Level getStatusLevel() {
@Override
public void log(final StatusData data) {
requireNonNull(data, "data");
if (level.isLessSpecificThan(data.getLevel())) {
if (debugEnabled || level.isLessSpecificThan(data.getLevel())) {
final String formattedStatus = data.getFormattedStatus();
stream.println(formattedStatus);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,8 @@ private static final class InstanceHolder {
StatusLogger.class.getSimpleName(),
ParameterizedNoReferenceMessageFactory.INSTANCE,
Config.getInstance(),
new StatusConsoleListener(Config.getInstance().fallbackListenerLevel));
new StatusConsoleListener(Config.getInstance().fallbackListenerLevel,
Config.getInstance().debugEnabled));
}

/**
Expand Down

0 comments on commit 0407c14

Please sign in to comment.