Skip to content

Commit

Permalink
Merge pull request #21473 from stuartwdouglas/log-messages
Browse files Browse the repository at this point in the history
Only warn about loggable messages
  • Loading branch information
gastaldi authored Nov 18, 2021
2 parents a0a501e + 14aa776 commit 335e5cf
Showing 1 changed file with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.logging.ErrorManager;
import java.util.logging.Formatter;
import java.util.logging.Handler;
Expand Down Expand Up @@ -49,6 +55,7 @@ public class QuarkusDelayedHandler extends ExtHandler {

private final Deque<ExtLogRecord> logRecords = new ArrayDeque<>();
private final List<Runnable> logCloseTasks = new ArrayList<>();
private final Set<CategoryAndLevel> droppedRecords = Collections.synchronizedSet(new HashSet<>());

private final int queueLimit;
private volatile boolean buildTimeLoggingActivated = false;
Expand Down Expand Up @@ -83,15 +90,14 @@ protected void doPublish(final ExtLogRecord record) {
// this is not ideal, but we can run out of memory otherwise
// this only happens if we end up with more than 4k log messages before activation
if (record.getLevel().intValue() <= discardLevel) {
droppedRecords.add(new CategoryAndLevel(record.getLoggerName(), record.getLevel()));
return;
}
// Determine whether the queue was overrun
if (logRecords.size() >= queueLimit) {
reportError(
"The delayed handler's queue was overrun and log record(s) were lost. Did you forget to configure logging?",
null, ErrorManager.WRITE_FAILURE);
compactQueue();
if (logRecords.size() >= queueLimit) {
droppedRecords.add(new CategoryAndLevel(record.getLoggerName(), record.getLevel()));
//still too full, nothing we can do
return;
}
Expand Down Expand Up @@ -123,6 +129,7 @@ private void compactQueue() {
while (it.hasNext()) {
ExtLogRecord rec = it.next();
if (rec.getLevel().intValue() == lowestInQueue) {
droppedRecords.add(new CategoryAndLevel(rec.getLoggerName(), rec.getLevel()));
it.remove();
} else {
newLowest = Integer.min(rec.getLevel().intValue(), newLowest);
Expand Down Expand Up @@ -285,6 +292,50 @@ private synchronized void activate() {
publishToNestedHandlers(record);
}
}
Map<String, List<java.util.logging.Level>> lostCategories = new TreeMap<>();
for (CategoryAndLevel entry : droppedRecords) {
if (Logger.getLogger(entry.category).isLoggable(entry.level)) {
lostCategories.computeIfAbsent(entry.category, (key) -> new ArrayList<>())
.add(entry.level);
}
}
if (lostCategories.size() > 0) {
StringBuilder msg = new StringBuilder(
"The delayed handler's queue was overrun and log record(s) were lost (Did you forget to configure logging?): \n");
for (Map.Entry<String, List<java.util.logging.Level>> entry : lostCategories.entrySet()) {
msg.append(String.format("\t - %s: %s\n", entry.getKey(), entry.getValue()));
}
reportError(msg.toString(), null, ErrorManager.WRITE_FAILURE);
lostCategories.clear();
}
droppedRecords.clear();
activated = true;
}

static final class CategoryAndLevel {
final String category;
final java.util.logging.Level level;

CategoryAndLevel(String category, java.util.logging.Level level) {
this.category = category;
this.level = level;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CategoryAndLevel that = (CategoryAndLevel) o;
return Objects.equals(category, that.category) && Objects.equals(level, that.level);
}

@Override
public int hashCode() {
return Objects.hash(category, level);
}
}
}

0 comments on commit 335e5cf

Please sign in to comment.