Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,55 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.test;
package org.apache.logging.log4j.core.appender;

import java.util.ArrayList;
import java.util.Collections;
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 {
/**
* {@link ErrorHandler} implementation buffering invocations in the form of {@link StatusData}.
*/
final class BufferingErrorHandler implements ErrorHandler {

private final List<StatusData> statusDataBuffer = Collections.synchronizedList(new ArrayList<>());

private final ArrayList<StatusData> statusData = new ArrayList<>();
BufferingErrorHandler() {}

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));
}
private void addStatusData(final String message, final Throwable throwable) {
final StackTraceElement caller = StackLocatorUtil.getStackTraceElement(3);
final String threadName = Thread.currentThread().getName();
final StatusData statusData =
new StatusData(caller, Level.ERROR, new SimpleMessage(message), throwable, threadName);
statusDataBuffer.add(statusData);
}

@Override
public void error(String msg) {
addStatusData(msg, null);
public void error(String message) {
addStatusData(message, null);
}

@Override
public void error(String msg, Throwable t) {
addStatusData(msg, t);
public void error(final String message, final Throwable throwable) {
addStatusData(message, throwable);
}

@Override
public void error(String msg, LogEvent event, Throwable t) {
addStatusData(msg, t);
public void error(final String message, final LogEvent event, final Throwable throwable) {
addStatusData(message, throwable);
}

public void clear() {
synchronized (statusData) {
statusData.clear();
}
}

public Stream<StatusData> getStatusData() {
synchronized (statusData) {
return ((List<StatusData>) statusData.clone()).stream();
}
statusDataBuffer.clear();
}

public Stream<StatusData> findStatusData(String regex) {
return getStatusData()
.filter(data -> data.getMessage().getFormattedMessage().matches(regex));
public List<StatusData> getBuffer() {
return statusDataBuffer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.appender;

import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.logging.log4j.core.net.TcpSocketManager;

/**
* {@link TcpSocketManager.HostResolver} implementation always resolving to the given list of {@link #addresses}.
*/
final class FixedHostResolver extends TcpSocketManager.HostResolver {

private final List<InetSocketAddress> addresses;

private FixedHostResolver(final List<InetSocketAddress> addresses) {
this.addresses = addresses;
}

static FixedHostResolver ofServers(final LineReadingTcpServer... servers) {
final List<InetSocketAddress> addresses = Arrays.stream(servers)
.map(server -> (InetSocketAddress) server.getServerSocket().getLocalSocketAddress())
.collect(Collectors.toList());
return new FixedHostResolver(addresses);
}

@Override
public List<InetSocketAddress> resolveHost(final String ignoredHost, final int ignoredPort) {
return addresses;
}
}
Loading