You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2023/09/06 11:16:54 UTC
[logging-log4j2] 03/03: Adds an ErrorHandler that does not debounce messages
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 565f57d643bb3775b85a82b5546499636d66277d
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Wed Sep 6 13:16:33 2023 +0200
Adds an ErrorHandler that does not debounce messages
`ListErrorHandler` can be used in tests to verify the number of issued
errors without losing any.
---
.../logging/log4j/core/test/ListErrorHandler.java | 72 ++++++++++++++++++++++
1 file changed, 72 insertions(+)
diff --git a/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/ListErrorHandler.java b/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/ListErrorHandler.java
new file mode 100644
index 0000000000..271b074cbd
--- /dev/null
+++ b/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/ListErrorHandler.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.core.test;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Stream;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.ErrorHandler;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.apache.logging.log4j.status.StatusData;
+import org.apache.logging.log4j.util.StackLocatorUtil;
+
+public class ListErrorHandler implements ErrorHandler {
+
+ private final ArrayList<StatusData> statusData = new ArrayList<>();
+
+ private void addStatusData(final String msg, final Throwable t) {
+ synchronized (statusData) {
+ final StackTraceElement caller = StackLocatorUtil.getStackTraceElement(3);
+ final String threadName = Thread.currentThread().getName();
+ statusData.add(new StatusData(caller, Level.ERROR, new SimpleMessage(msg), t, threadName));
+ }
+ }
+
+ @Override
+ public void error(String msg) {
+ addStatusData(msg, null);
+ }
+
+ @Override
+ public void error(String msg, Throwable t) {
+ addStatusData(msg, t);
+ }
+
+ @Override
+ public void error(String msg, LogEvent event, Throwable t) {
+ addStatusData(msg, t);
+ }
+
+ public void clear() {
+ synchronized (statusData) {
+ statusData.clear();
+ }
+ }
+
+ public Stream<StatusData> getStatusData() {
+ synchronized (statusData) {
+ return ((List<StatusData>) statusData.clone()).stream();
+ }
+ }
+
+ public Stream<StatusData> findStatusData(String regex) {
+ return getStatusData().filter(data -> data.getMessage().getFormattedMessage().matches(regex));
+ }
+}