Adwait Kumar Singh created LOG4J2-2720:
------------------------------------------

             Summary: 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
             Fix For: 2.8


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.



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

Reply via email to