You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Adwait Kumar Singh (Jira)" <ji...@apache.org> on 2019/11/19 08:49:00 UTC
[jira] [Comment Edited] (LOG4J2-2720) TestAppender based on
MutableLogEvent stop working in Log4j2.11
[ https://issues.apache.org/jira/browse/LOG4J2-2720?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16977259#comment-16977259 ]
Adwait Kumar Singh edited comment on LOG4J2-2720 at 11/19/19 8:48 AM:
----------------------------------------------------------------------
[~rgoers] I was partially able to root cause, the issue is the TestAppender that I am creating in `@BeforeClass` in the unit test and the TestAppender used by the logger is different. But this is happening only after upgrade to 2.11.
I am setting up the TestAppender like this.
{code:java}
LoggerContext context = LoggerContext.getContext(false);
final Configuration config = context.getConfiguration();
var testAppender = (TestAppender) config.getAppenders().get("TestAppender");
testAppender.clearAppender();
return testAppender;
{code}
was (Author: adwsingh):
[~rgoers] I was partially able to root cause, the issue is the TestAppender that I am creating in `@BeforeClass` in the unit test and the TestAppender used by the logger is different. But this is happening only after upgrade to 2.11.
I am setting up the TestAppender like this.
{code:java}
LoggerContext context = LoggerContext.getContext(false);
final Configuration config = context.getConfiguration();
var testAppender = (TestAppender) config.getAppenders().get("TestAppender");
testAppender.clearAppender();
return testAppender;
{code:java}
> TestAppender based on MutableLogEvent stop working in Log4j2.11
> ---------------------------------------------------------------
>
> Key: LOG4J2-2720
> URL: https://issues.apache.org/jira/browse/LOG4J2-2720
> Project: Log4j 2
> Issue Type: Question
> Affects Versions: 2.11.2
> Environment: JDK 11, Log4J-2.11.2
> Reporter: Adwait Kumar Singh
> Priority: Critical
> Fix For: 2.8
>
>
> I have written unit tests on Logs by creating a custom appender
> After moving to Log4j2.11, I am seeing that the getMessages is returning empty after I am logging something. I attached a debugger and can see that the append() method is being invoked and the message list is getting populated, however at the time of verififcation it is magically vanishing off. Reverting back to 2.8 fixes this.
> {code:java}
> import java.io.Serializable;
> import java.util.ArrayList;
> import java.util.List;
> import org.apache.logging.log4j.core.Filter;
> import org.apache.logging.log4j.core.Layout;
> import org.apache.logging.log4j.core.LogEvent;
> import org.apache.logging.log4j.core.appender.AbstractAppender;
> import org.apache.logging.log4j.core.config.plugins.Plugin;
> import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
> import org.apache.logging.log4j.core.config.plugins.PluginElement;
> import org.apache.logging.log4j.core.config.plugins.PluginFactory;
> import org.apache.logging.log4j.core.impl.MutableLogEvent;
> import org.apache.logging.log4j.core.layout.PatternLayout;
> @Plugin(name = "TestAppender", category = "Core", elementType = "appender", printObject = true)
> public class TestAppender extends AbstractAppender {
> private List<String> messages = new ArrayList<>();
> TestAppender(String name, Filter filter, Layout<? extends Serializable> layout) {
> super(name, filter, layout);
> }
> @Override public void append(LogEvent event) {
> getMessages().add(((MutableLogEvent) (event)).getFormattedMessage());
> }
> @PluginFactory public static TestAppender createAppender(
> @PluginAttribute("name") String name,
> @PluginElement("Layout") Layout<? extends Serializable> layout,
> @PluginElement("Filter") final Filter filter,
> @PluginAttribute("otherAttribute") String otherAttribute) {
> if (name == null) {
> LOGGER.error("No name provided for TestAppender");
> return null;
> }
> if (layout == null) {
> layout = PatternLayout.createDefaultLayout();
> }
> return new TestAppender(name, filter, layout);
> }
> public List<String> getMessages() {
> return messages;
> }
> public void clearAppender() {
> this.messages = new ArrayList<>();
> }
> }{code}
> Test log configuration file
> {code:xml}
> <?xml version="1.0" encoding="UTF-8"?>
> <Configuration status="WARN" packages="com.sample.package.test.utils">
> <Properties>
> <Property name="log-path">${sys:root}/var/output/logs</Property>
> </Properties>
> <Appenders>
> <Console name="Console" target="SYSTEM_OUT">
> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
> </Console>
> <TestAppender name="TestAppender">
> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
> </TestAppender>
> </Appenders>
> <Loggers>
> <Logger name="com.amazon.sample.package" level="ALL"/>
> <Root>
> <AppenderRef ref="TestAppender" level="ALL"/>
> </Root>
> </Loggers>
> </Configuration>
> {code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)