Skip to content

Commit

Permalink
Fix The first element of the queue cannot be deleted when an exceptio…
Browse files Browse the repository at this point in the history
…n occurs in the method processWithAccessKeyLogger(#9978)

Fixes issue #9953
  • Loading branch information
106umao committed May 7, 2022
1 parent 3a8e5e6 commit c5c1bad
Showing 1 changed file with 8 additions and 10 deletions.
Expand Up @@ -34,7 +34,6 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -156,22 +155,21 @@ private void writeLogToFile() {
}

private void processWithAccessKeyLogger(Queue<AccessLogData> logQueue, File file) throws IOException {
try (FileWriter writer = new FileWriter(file, true)) {
for (Iterator<AccessLogData> iterator = logQueue.iterator();
iterator.hasNext();
iterator.remove()) {
writer.write(iterator.next().getLogMessage());
FileWriter writer = new FileWriter(file, true);
try {
while (!logQueue.isEmpty()) {
writer.write(logQueue.poll().getLogMessage());
writer.write(System.getProperty(LINE_SEPARATOR));
}
}finally {
writer.flush();
writer.close();
}
}

private void processWithServiceLogger(Queue<AccessLogData> logQueue) {
for (Iterator<AccessLogData> iterator = logQueue.iterator();
iterator.hasNext();
iterator.remove()) {
AccessLogData logData = iterator.next();
while (!logQueue.isEmpty()) {
AccessLogData logData = logQueue.poll();
LoggerFactory.getLogger(LOG_KEY + "." + logData.getServiceName()).info(logData.getLogMessage());
}
}
Expand Down

0 comments on commit c5c1bad

Please sign in to comment.