[ 
https://issues.apache.org/jira/browse/LOG4J2-2720?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Ralph Goers updated LOG4J2-2720:
--------------------------------
    Description: 
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}

  was:
I have written unit tests on Logs by creating a custom appender

 

{color:#000080}import {color}java.io.Serializable;
{color:#000080}import {color}java.util.ArrayList;
{color:#000080}import {color}java.util.List;
{color:#000080}import {color}org.apache.logging.log4j.core.Filter;
{color:#000080}import {color}org.apache.logging.log4j.core.Layout;
{color:#000080}import {color}org.apache.logging.log4j.core.LogEvent;
{color:#000080}import 
{color}org.apache.logging.log4j.core.appender.AbstractAppender;
{color:#000080}import 
{color}org.apache.logging.log4j.core.config.plugins.{color:#808000}Plugin{color};
{color:#000080}import 
{color}org.apache.logging.log4j.core.config.plugins.{color:#808000}PluginAttribute{color};
{color:#000080}import 
{color}org.apache.logging.log4j.core.config.plugins.{color:#808000}PluginElement{color};
{color:#000080}import 
{color}org.apache.logging.log4j.core.config.plugins.{color:#808000}PluginFactory{color};
{color:#000080}import {color}org.apache.logging.log4j.core.impl.MutableLogEvent;
{color:#000080}import {color}org.apache.logging.log4j.core.layout.PatternLayout;

{color:#808000}@Plugin{color}(name = {color:#008000}"TestAppender"{color}, 
category = {color:#008000}"Core"{color}, elementType = 
{color:#008000}"appender"{color}, printObject = {color:#000080}true{color})
{color:#000080}public class {color}TestAppender {color:#000080}extends 
{color}AbstractAppender {

 {color:#000080}private {color}List<String> {color:#660e7a}messages {color}= 
{color:#000080}new {color}ArrayList<>();

 TestAppender(String name, Filter filter, Layout<? {color:#000080}extends 
{color}Serializable> layout) {
 {color:#000080}super{color}(name, filter, layout);
 }

 {color:#808000}@Override
{color} {color:#000080}public void {color}append(LogEvent event) {
 getMessages().add(((MutableLogEvent) (event)).getFormattedMessage());
 }

 {color:#808000}@PluginFactory
{color} {color:#000080}public static {color}TestAppender createAppender(
 {color:#808000}@PluginAttribute{color}({color:#008000}"name"{color}) String 
name,
 {color:#808000}@PluginElement{color}({color:#008000}"Layout"{color}) Layout<? 
{color:#000080}extends {color}Serializable> layout,
 {color:#808000}@PluginElement{color}({color:#008000}"Filter"{color}) 
{color:#000080}final {color}Filter filter,
 {color:#808000}@PluginAttribute{color}({color:#008000}"otherAttribute"{color}) 
String otherAttribute) {
 {color:#000080}if {color}(name == {color:#000080}null{color}) {
 {color:#660e7a}LOGGER{color}.error({color:#008000}"No name provided for 
TestAppender"{color});
 {color:#000080}return null{color};
 }
 {color:#000080}if {color}(layout == {color:#000080}null{color}) {
 layout = PatternLayout.createDefaultLayout();
 }
 {color:#000080}return new {color}TestAppender(name, filter, layout);
 }

 {color:#000080}public {color}List<String> getMessages() {
 {color:#000080}return {color}{color:#660e7a}messages{color};
 }

 {color:#000080}public void {color}clearAppender() {
 {color:#000080}this{color}.{color:#660e7a}messages {color}= {color:#000080}new 
{color}ArrayList<>();
 }
}

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.


> 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}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to