Skip to content

Commit f3c80e3

Browse files
committed
ListAppender: Synchronize on list while iterating
* Prevent ConcurrentModificationException while making snapshots * Returned snapshots no longer require an unmodifiable view
1 parent 7209b27 commit f3c80e3

File tree

1 file changed

+15
-9
lines changed
  • log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/appender

1 file changed

+15
-9
lines changed

log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/appender/ListAppender.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,18 @@ public ListAppender clear() {
195195
return this;
196196
}
197197

198-
/** Returns an immutable snapshot of captured log events */
198+
/** Returns a snapshot of captured log events */
199199
public List<LogEvent> getEvents() {
200-
return Collections.<LogEvent>unmodifiableList(new ArrayList<>(events));
200+
synchronized (events) {
201+
return new ArrayList<>(events);
202+
}
201203
}
202204

203-
/** Returns an immutable snapshot of captured messages */
205+
/** Returns a snapshot of captured messages */
204206
public List<String> getMessages() {
205-
return Collections.<String>unmodifiableList(new ArrayList<>(messages));
207+
synchronized (messages) {
208+
return new ArrayList<>(messages);
209+
}
206210
}
207211

208212
/**
@@ -215,9 +219,11 @@ public List<String> getMessages(final int minSize, final long timeout, final Tim
215219
return getMessages();
216220
}
217221

218-
/** Returns an immutable snapshot of captured data */
222+
/** Returns a snapshot of captured data */
219223
public List<byte[]> getData() {
220-
return Collections.<byte[]>unmodifiableList(new ArrayList<>(data));
224+
synchronized (data) {
225+
return new ArrayList<>(data);
226+
}
221227
}
222228

223229
public static ListAppender createAppender(
@@ -297,9 +303,9 @@ public static ListAppender getListAppender(final String name) {
297303

298304
@Override
299305
public String toString() {
300-
return "ListAppender [events=" + events + ", messages=" + messages + ", data=" + data + ", newLine=" + newLine
301-
+ ", raw=" + raw + ", countDownLatch=" + countDownLatch + ", getHandler()=" + getHandler()
302-
+ ", getLayout()=" + getLayout() + ", getName()=" + getName() + ", ignoreExceptions()="
306+
return "ListAppender [events=" + getEvents() + ", messages=" + getMessages() + ", data=" + getData()
307+
+ ", newLine=" + newLine + ", raw=" + raw + ", countDownLatch=" + countDownLatch + ", getHandler()="
308+
+ getHandler() + ", getLayout()=" + getLayout() + ", getName()=" + getName() + ", ignoreExceptions()="
303309
+ ignoreExceptions() + ", getFilter()=" + getFilter() + ", getState()=" + getState() + "]";
304310
}
305311
}

0 commit comments

Comments
 (0)