AMQ-8520: Log4j2 test fixes

Also fixes Log4jConfigView
This commit is contained in:
Christopher L. Shannon (cshannon) 2022-03-03 08:10:58 -05:00 committed by Matt Pavlovich
parent 7ec5254bb5
commit ae30dce4e2
8 changed files with 73 additions and 56 deletions

View File

@ -309,7 +309,7 @@ public class TransportConnection implements Connection, Task, CommandVisitor {
if (SERVICELOG.isDebugEnabled()) { if (SERVICELOG.isDebugEnabled()) {
SERVICELOG.debug("Async error occurred: {}", e.getMessage(), e); SERVICELOG.debug("Async error occurred: {}", e.getMessage(), e);
} else { } else {
SERVICELOG.warn("Async error occurred", e); SERVICELOG.warn("Async error occurred", e.getMessage());
} }
ConnectionError ce = new ConnectionError(); ConnectionError ce = new ConnectionError();
ce.setException(e); ce.setException(e);

View File

@ -22,10 +22,9 @@ import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -41,12 +40,13 @@ public class Log4JConfigView implements Log4JConfigViewMBean {
return null; return null;
} }
Class<?> logManagerClass = getLogManagerClass(cl);
Class<?> loggerClass = getLoggerClass(cl); Class<?> loggerClass = getLoggerClass(cl);
if (loggerClass == null) { if (logManagerClass == null || loggerClass == null) {
return null; return null;
} }
Method getRootLogger = loggerClass.getMethod("getRootLogger", new Class[]{}); Method getRootLogger = logManagerClass.getMethod("getRootLogger", new Class[]{});
Method getLevel = loggerClass.getMethod("getLevel", new Class[]{}); Method getLevel = loggerClass.getMethod("getLevel", new Class[]{});
Object rootLogger = getRootLogger.invoke(null, (Object[])null); Object rootLogger = getRootLogger.invoke(null, (Object[])null);
@ -61,16 +61,15 @@ public class Log4JConfigView implements Log4JConfigViewMBean {
return; return;
} }
Class<?> configuratorClass = getConfiguratorClass(cl);
Class<?> loggerClass = getLoggerClass(cl); Class<?> loggerClass = getLoggerClass(cl);
Class<?> levelClass = getLevelClass(cl); Class<?> levelClass = getLevelClass(cl);
if (levelClass == null || loggerClass == null) { if (configuratorClass == null || levelClass == null || loggerClass == null) {
return; return;
} }
String targetLevel = level.toUpperCase(Locale.US); String targetLevel = level.toUpperCase(Locale.US);
Method getRootLogger = loggerClass.getMethod("getRootLogger", new Class[]{}); Method setRootLevel = configuratorClass.getMethod("setRootLevel", levelClass);
Method setLevel = loggerClass.getMethod("setLevel", levelClass);
Object rootLogger = getRootLogger.invoke(null, (Object[])null);
Method toLevel = levelClass.getMethod("toLevel", String.class); Method toLevel = levelClass.getMethod("toLevel", String.class);
Object newLevel = toLevel.invoke(null, targetLevel); Object newLevel = toLevel.invoke(null, targetLevel);
@ -80,7 +79,7 @@ public class Log4JConfigView implements Log4JConfigViewMBean {
// matched what the user asked for. // matched what the user asked for.
if (newLevel != null && newLevel.toString().equals(targetLevel)) { if (newLevel != null && newLevel.toString().equals(targetLevel)) {
LOG.debug("Set level {} for root logger.", level); LOG.debug("Set level {} for root logger.", level);
setLevel.invoke(rootLogger, newLevel); setRootLevel.invoke(configuratorClass, newLevel);
} }
} }
@ -95,18 +94,23 @@ public class Log4JConfigView implements Log4JConfigViewMBean {
Class<?> logManagerClass = getLogManagerClass(cl); Class<?> logManagerClass = getLogManagerClass(cl);
Class<?> loggerClass = getLoggerClass(cl); Class<?> loggerClass = getLoggerClass(cl);
if (logManagerClass == null || loggerClass == null) { Class<?> loggerConfigClass = getLoggerConfigClass(cl);
if (logManagerClass == null || loggerClass == null || loggerConfigClass == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
Method getCurrentLoggers = logManagerClass.getMethod("getCurrentLoggers", new Class[]{}); Method getContext = logManagerClass.getMethod("getContext", boolean.class);
Method getName = loggerClass.getMethod("getName", new Class[]{}); Object logContext = getContext.invoke(logManagerClass, false);
Method getConfiguration = logContext.getClass().getMethod("getConfiguration");
Object configuration = getConfiguration.invoke(logContext);
Method getLoggers = configuration.getClass().getMethod("getLoggers");
Method getName = loggerConfigClass.getMethod("getName");
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
Enumeration<?> loggers = (Enumeration<?>)getCurrentLoggers.invoke(null, (Object[])null); Map<String, ?> loggers = (Map<String, ?>)getLoggers.invoke(configuration);
while (loggers.hasMoreElements()) { for (Object logger : loggers.values()) {
Object logger = loggers.nextElement();
if (logger != null) { if (logger != null) {
list.add((String) getName.invoke(logger, (Object[])null)); list.add((String) getName.invoke(logger, (Object[])null));
} }
@ -126,12 +130,13 @@ public class Log4JConfigView implements Log4JConfigViewMBean {
return null; return null;
} }
Class<?> logManagerClass = getLogManagerClass(cl);
Class<?> loggerClass = getLoggerClass(cl); Class<?> loggerClass = getLoggerClass(cl);
if (loggerClass == null) { if (logManagerClass == null || loggerClass == null) {
return null; return null;
} }
Method getLogger = loggerClass.getMethod("getLogger", String.class); Method getLogger = logManagerClass.getMethod("getLogger", String.class);
String logLevel = null; String logLevel = null;
if (loggerName != null && !loggerName.isEmpty()) { if (loggerName != null && !loggerName.isEmpty()) {
@ -172,29 +177,26 @@ public class Log4JConfigView implements Log4JConfigViewMBean {
return; return;
} }
Class<?> configuratorClass = getConfiguratorClass(cl);
Class<?> loggerClass = getLoggerClass(cl); Class<?> loggerClass = getLoggerClass(cl);
Class<?> levelClass = getLevelClass(cl); Class<?> levelClass = getLevelClass(cl);
if (loggerClass == null || levelClass == null) { if (configuratorClass == null || loggerClass == null || levelClass == null) {
return; return;
} }
String targetLevel = level.toUpperCase(Locale.US); String targetLevel = level.toUpperCase(Locale.US);
Method getLogger = loggerClass.getMethod("getLogger", String.class); Method setLevel = configuratorClass.getMethod("setLevel", String.class, levelClass);
Method setLevel = loggerClass.getMethod("setLevel", levelClass);
Method toLevel = levelClass.getMethod("toLevel", String.class); Method toLevel = levelClass.getMethod("toLevel", String.class);
Object logger = getLogger.invoke(null, loggerName); Object newLevel = toLevel.invoke(null, targetLevel);
if (logger != null) {
Object newLevel = toLevel.invoke(null, targetLevel);
// Check that the level conversion worked and that we got a level // Check that the level conversion worked and that we got a level
// that matches what was asked for. A bad level name will result // that matches what was asked for. A bad level name will result
// in the lowest level value and we don't want to change unless we // in the lowest level value and we don't want to change unless we
// matched what the user asked for. // matched what the user asked for.
if (newLevel != null && newLevel.toString().equals(targetLevel)) { if (newLevel != null && newLevel.toString().equals(targetLevel)) {
LOG.debug("Set level {} for logger: {}", level, loggerName); LOG.debug("Set level {} for logger: {}", level, loggerName);
setLevel.invoke(logger, newLevel); setLevel.invoke(configuratorClass, loggerName, newLevel);
}
} }
} }
@ -258,29 +260,31 @@ public class Log4JConfigView implements Log4JConfigViewMBean {
} }
private static Class<?> getLogManagerClass(ClassLoader cl) { private static Class<?> getLogManagerClass(ClassLoader cl) {
Class<?> logManagerClass = null; return getClass(cl, "org.apache.logging.log4j.LogManager");
try {
logManagerClass = cl.loadClass("org.apache.log4j.LogManager");
} catch (ClassNotFoundException e) {
}
return logManagerClass;
} }
private static Class<?> getLoggerClass(ClassLoader cl) { private static Class<?> getLoggerClass(ClassLoader cl) {
Class<?> loggerClass = null; return getClass(cl, "org.apache.logging.log4j.Logger");
try { }
loggerClass = cl.loadClass("org.apache.log4j.Logger");
} catch (ClassNotFoundException e) { private static Class<?> getLoggerConfigClass(ClassLoader cl) {
} return getClass(cl, "org.apache.logging.log4j.core.config.LoggerConfig");
return loggerClass;
} }
private static Class<?> getLevelClass(ClassLoader cl) { private static Class<?> getLevelClass(ClassLoader cl) {
Class<?> levelClass = null; return getClass(cl, "org.apache.logging.log4j.Level");
}
private static Class<?> getConfiguratorClass(ClassLoader cl) {
return getClass(cl, "org.apache.logging.log4j.core.config.Configurator");
}
private static Class<?> getClass(ClassLoader cl, String clazz) {
Class<?> configuratorClass = null;
try { try {
levelClass = cl.loadClass("org.apache.log4j.Level"); configuratorClass = cl.loadClass(clazz);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
} }
return levelClass; return configuratorClass;
} }
} }

View File

@ -17,19 +17,16 @@
package org.apache.activemq.broker.jmx; package org.apache.activemq.broker.jmx;
import java.util.List; import java.util.List;
import javax.jms.ConnectionFactory; import javax.jms.ConnectionFactory;
import javax.management.MBeanServer; import javax.management.MBeanServer;
import javax.management.MBeanServerInvocationHandler; import javax.management.MBeanServerInvocationHandler;
import javax.management.MalformedObjectNameException; import javax.management.MalformedObjectNameException;
import javax.management.ObjectName; import javax.management.ObjectName;
import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.EmbeddedBrokerTestSupport; import org.apache.activemq.EmbeddedBrokerTestSupport;
import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerService;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.junit.Test; import org.junit.Test;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -136,6 +133,9 @@ public class Log4JConfigTest extends EmbeddedBrokerTestSupport {
level = log4jConfigView.getLogLevel(BROKER_LOGGER); level = log4jConfigView.getLogLevel(BROKER_LOGGER);
assertNotNull(level); assertNotNull(level);
assertEquals("INFO", level); assertEquals("INFO", level);
List<String> loggers = log4jConfigView.getLoggers();
assertEquals(2, loggers.size());
} }
@Test @Test

View File

@ -48,6 +48,10 @@ import org.apache.activemq.util.DefaultTestAppender;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.filter.AbstractFilter;
import org.apache.logging.log4j.core.layout.MessageLayout;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -71,7 +75,7 @@ public class SecurityJMXTest extends TestCase {
final AtomicBoolean gotExpected = new AtomicBoolean(false); final AtomicBoolean gotExpected = new AtomicBoolean(false);
final AtomicReference<Object> stackTrace = new AtomicReference<Object>(); final AtomicReference<Object> stackTrace = new AtomicReference<Object>();
final Appender appender = new DefaultTestAppender() { final Appender appender = new AbstractAppender("testAppender", new AbstractFilter() {}, new MessageLayout(), false, new Property[0]) {
@Override @Override
public void append(LogEvent event) { public void append(LogEvent event) {
String message = event.getMessage().getFormattedMessage(); String message = event.getMessage().getFormattedMessage();
@ -82,6 +86,7 @@ public class SecurityJMXTest extends TestCase {
} }
}; };
appender.start();
org.apache.logging.log4j.core.Logger toVerify = (org.apache.logging.log4j.core.Logger)LogManager.getLogger(TransportConnection.class.getName() + ".Service"); org.apache.logging.log4j.core.Logger toVerify = (org.apache.logging.log4j.core.Logger)LogManager.getLogger(TransportConnection.class.getName() + ".Service");
toVerify.addAppender(appender); toVerify.addAppender(appender);

View File

@ -193,7 +193,7 @@ public class SharedFileLockerTest {
final AtomicInteger logCounts = new AtomicInteger(0); final AtomicInteger logCounts = new AtomicInteger(0);
// start new // start new
final var logger = org.apache.logging.log4j.core.Logger.class.cast(LogManager.getLogger(SharedFileLocker.class)); final var logger = org.apache.logging.log4j.core.Logger.class.cast(LogManager.getLogger(SharedFileLocker.class));
final var appender = new AbstractAppender("testAppender", new AbstractFilter() {}, new MessageLayout(), false, new Property[0]) { final var appender = new AbstractAppender("testAppender2", new AbstractFilter() {}, new MessageLayout(), false, new Property[0]) {
@Override @Override
public void append(LogEvent event) { public void append(LogEvent event) {
logCounts.incrementAndGet(); logCounts.incrementAndGet();

View File

@ -28,7 +28,11 @@ import org.apache.logging.log4j.core.LogEvent;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Configurator; import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.filter.AbstractFilter;
import org.apache.logging.log4j.core.layout.MessageLayout;
/** /**
* *
@ -52,7 +56,7 @@ public class KahaDBPersistenceAdapterTest extends PersistenceAdapterTestSupport
final AtomicBoolean gotSomeReplay = new AtomicBoolean(Boolean.FALSE); final AtomicBoolean gotSomeReplay = new AtomicBoolean(Boolean.FALSE);
final AtomicBoolean trappedLogMessages = new AtomicBoolean(Boolean.FALSE); final AtomicBoolean trappedLogMessages = new AtomicBoolean(Boolean.FALSE);
Appender appender = new DefaultTestAppender() { final var appender = new AbstractAppender("testAppender", new AbstractFilter() {}, new MessageLayout(), false, new Property[0]) {
@Override @Override
public void append(LogEvent event) { public void append(LogEvent event) {
trappedLogMessages.set(true); trappedLogMessages.set(true);
@ -66,7 +70,7 @@ public class KahaDBPersistenceAdapterTest extends PersistenceAdapterTestSupport
appender.start(); appender.start();
try { try {
Configurator.setLevel(MessageDatabase.class.getName(), Level.INFO); Configurator.setLevel(MessageDatabase.class.getName(), Level.DEBUG);
((org.apache.logging.log4j.core.Logger)LogManager.getLogger(MessageDatabase.class)).addAppender(appender); ((org.apache.logging.log4j.core.Logger)LogManager.getLogger(MessageDatabase.class)).addAppender(appender);
brokerService = new BrokerService(); brokerService = new BrokerService();

View File

@ -20,12 +20,14 @@ import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.TransportConnection; import org.apache.activemq.broker.TransportConnection;
import org.apache.activemq.broker.TransportConnector; import org.apache.activemq.broker.TransportConnector;
import org.apache.activemq.store.kahadb.MessageDatabase;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger; import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.appender.AbstractAppender; import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.Property; import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.filter.AbstractFilter; import org.apache.logging.log4j.core.filter.AbstractFilter;
import org.apache.logging.log4j.core.layout.MessageLayout; import org.apache.logging.log4j.core.layout.MessageLayout;
@ -82,6 +84,7 @@ public class TcpTransportCloseSocketNoWarnTest {
@Before @Before
public void before() throws Exception { public void before() throws Exception {
gotExceptionInLog.set(false);
brokerService = new BrokerService(); brokerService = new BrokerService();
brokerService.setPersistent(false); brokerService.setPersistent(false);
brokerService.setUseJmx(false); brokerService.setUseJmx(false);
@ -90,7 +93,7 @@ public class TcpTransportCloseSocketNoWarnTest {
rootLogger.get().addAppender(appender, Level.DEBUG, new AbstractFilter() {}); rootLogger.get().addAppender(appender, Level.DEBUG, new AbstractFilter() {});
rootLogger.addAppender(appender); rootLogger.addAppender(appender);
org.apache.logging.log4j.core.Logger.class.cast(LogManager.getLogger(TransportConnection.class.getName() + ".Transport")).setLevel(Level.WARN); Configurator.setLevel(TransportConnection.class.getName() + ".Transport", Level.WARN);
} }
@After @After

View File

@ -80,7 +80,8 @@ public class TcpTransportInactiveDuringHandshakeTest {
appender = new AbstractAppender("testAppender", new AbstractFilter() {}, new MessageLayout(), false, new Property[0]) { appender = new AbstractAppender("testAppender", new AbstractFilter() {}, new MessageLayout(), false, new Property[0]) {
@Override @Override
public void append(LogEvent event) { public void append(LogEvent event) {
if (Level.WARN.equals(event.getLevel()) && event.getMessage().getFormattedMessage().contains("InactivityIOException")) { if (Level.WARN.equals(event.getLevel()) &&
event.getMessage().getFormattedMessage().contains("CONNECT frame not received with in connectionTimeout")) {
inactivityMonitorFired.countDown(); inactivityMonitorFired.countDown();
} }
} }