Skip to content

Commit

Permalink
Try to recover from logging errors when closing application
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jan 20, 2025
1 parent 34eb48e commit ef1aaaa
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,36 @@ public final void close() throws SecurityException {
synchronized (this) {
if (!logRecords.isEmpty()) {
Formatter formatter = getFormatter();
Formatter defaultFormatter = new PatternFormatter("%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %s%e%n");
boolean usingDefaultFormatter = false;
if (formatter == null) {
formatter = new PatternFormatter("%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %s%e%n");
usingDefaultFormatter = true;
formatter = defaultFormatter;
}
StandardOutputStreams.printError("The DelayedHandler was closed before any children handlers were " +
"configured. Messages will be written to stderr.");
// Always attempt to drain the queue
ExtLogRecord record;
Exception formattingException = null;
while ((record = logRecords.pollFirst()) != null) {
StandardOutputStreams.printError(formatter.format(record));
String value = null;
try {
value = formatter.format(record);
} catch (Exception e) {
if (!usingDefaultFormatter) {
// try once more using the default formatter
value = defaultFormatter.format(record);
} else {
formattingException = e;
}
}
if (value != null) {
StandardOutputStreams.printError(value);
}
}
if (formattingException != null) {
// let's just log the last formatting exception as there is not much more we can do
formattingException.printStackTrace();
}
}
}
Expand Down

0 comments on commit ef1aaaa

Please sign in to comment.