I can't create instance of my class because a don't know how to pass needed parameters in createAppender method from log4j.xml.
Exception:
Parameter 0 of constructor in ru.appliedtech.desktopapp.client.ui.menu.JTextAreaAppender required a bean of type 'java.lang.String' that could not be found. Consider defining a bean of type 'java.lang.String' in your configuration.
@Plugin(name = "JTextAreaAppender", category = "Core", elementType = "appender", printObject = true)
@Configuration
@ComponentScan("ru.appliedtech")
public class JTextAreaAppender extends AbstractAppender
{
private int maxLines = 0;
private static volatile ArrayList<JTextArea> jTextAreaList = new ArrayList<>();
protected JTextAreaAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions, Property[] properties)
{
super(name, filter, layout, ignoreExceptions, properties);
}
@PluginFactory
public static JTextAreaAppender createAppender(@PluginAttribute("name") String name,
@PluginAttribute("ignoreExceptions") boolean ignoreExceptions,
@PluginElement("Layout") Layout<?> layout,
@PluginElement("Filters") Filter filter,
@PluginElement("Properties") Property[] properties) {
return new JTextAreaAppender(name, filter, layout, ignoreExceptions, properties);
}
/**
* add the target JTextArea to be populated and updated by the logging information
* @param textArea text area
*/
public static void addTextArea(final JTextArea textArea) {
JTextAreaAppender.jTextAreaList.add(textArea);
}
@Override
public void append(LogEvent event)
{
//
}
}
log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ERROR" packages="ru.appliedtech.desktopapp.client.ui.menu">
<Properties>
<Property name="APP_LOG_ROOT">ru.appliedtech</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<JTextAreaAppender name="textArea">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss} %msg%n">
</PatternLayout>
</JTextAreaAppender>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="console" level="ERROR"/>
<AppenderRef ref="textArea" level="ERROR"/>
</Root>
</Loggers>
</Configuration>
rgoers is right that it's strange that you have Spring annotations on your Log4j Plugin class.
@Configuration
and @ComponentScan
should not be there. Remove those first.
By the way, it's sometimes useful (while debugging log4j2 configs) to set the status to DEBUG
like
<Configuration status="ERROR"
If your plugin is not being recognised, it might be that you don't have annotation processing working. If you are using Gradle, be sure to add log4j-core
as an annotationProcessor
dependency.
annotationProcessor 'org.apache.logging.log4j:log4j-core'
If you are using IntelliJ, be sure to enable Annotation processing, in Preferences -> Build,Execution, Deployment -> Compiler -> Annotation Processors -> Enable annotation processing.
If that's all okay, Log4J should detect your Appender Plugin, which is the topic of this question.