I am migrating the log4j library from 1.2.17 to 2.14.1 and facing issues
with the custom appenders.
I have tried all the other solutions for the problem that I could find with
regards to syntax, classloader, using the 'packages' attribute. But I am
still unable to initialize the custom appender.
Please let me know what am I doing wrong here. Pasting the config files
below
*Here is my custom Appender:*
@Plugin(name = "BWLogFile", category = Core.CATEGORY_NAME, elementType
= AbstractAppender.ELEMENT_TYPE)
public class BWLogFileAppender extends AbstractAppender {
private BWLogFileAppender(String name, Filter filter,
Layout<? extends Serializable> layout, final boolean
ignoreExceptions) {
super(name, filter, layout, ignoreExceptions);
}
public void append(LogEvent event) {
//custom code here
}
@PluginFactory
public static BWLogFileAppender createAppender(
@PluginAttribute("name") String name,
@PluginElement("Layout") Layout<? extends Serializable>
layout,
@PluginElement("Filter") final Filter filter) {
if (name == null) {
LOGGER.error("No name provided for BWLogFileAppender");
return null;
}
if (layout == null) {
layout = PatternLayout.createDefaultLayout();
}
return new BWLogFileAppender(name, filter, layout, true);
}}
*Here is my XML File :*
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="trace" packages="com.share.util">
<properties>
<property name="log-pattern">"%d{HH:mm:ss.SSS} [%t] %-5level
%logger{36} %msg%n"</property>
</properties>
<appenders>
<BWLogFile name="bw_log">
<PatternLayout pattern="%msg%n" />
</BWLogFile>
<console name="bw_console" target="SYSTEM_OUT">
<patternLayout>
<pattern>${log-pattern}</pattern>
</patternLayout>
</console>
</appenders>
<loggers>
<logger name="httpclient.wire" level="warn">
<AppenderRef ref="bw_console"/>
</logger>
<logger name="org.apache.http.wire" level="debug">
<AppenderRef ref="bw_log"/>
</logger>
<root level="warn">
<AppenderRef ref="bw_console" />
</root>
</loggers>
</configuration>
*Here is my properties file :*
#Define
status = trace
name = PropertiesConfig
appenders = bw_console,bw_log
#Package to search for Custom Appenders
packages = com.share.util
#Appenders
appender.bw_console.type = Console
appender.bw_console.name = bw_console
appender.bw_console.target = SYSTEM_OUT
appender.bw_console.layout.type = PatternLayout
appender.bw_console.layout.pattern = %d{yyyy MMM dd HH:mm:ss:SSS z}
%X{engine} %X{role} %m %n
appender.bw_log.type = BWLogFile
appender.bw_log.name = bw_log
appender.bw_log.layout.type = PatternLayout
appender.bw_log.layout.pattern = %msg%n
#Root-Logger
rootLogger.level = WARN
rootLogger.appenderRef.bw_console.ref = bw_console
#HttpClient Logger
logger.bw_console.name = httpclient.wire
logger.bw_console.level = WARN
logger.bw_console.appenderRef.bw_console.ref = bw_console
logger.bw_log.name = org.apache.http.wire
logger.bw_log.level = DEBUG
logger.bw_log.appenderRef.bw_log.ref = bw_log
*I am initializing the log4j via the below code :*
private void initlog4j2System() {
String log4jConfigFile = "URI/to/log4j2.xml";
ConfigurationSource source = null;
try {
source = new ConfigurationSource(new
FileInputStream(log4jConfigFile)); //for XML file
//source = new ConfigurationSource(new
FileInputStream(log4jConfigFile), new File(log4jConfigFile)); //for
properties file
} catch (FileNotFoundException e) {
System.out.println(e.getStackTrace());
} catch (IOException e) {
System.out.println(e.getStackTrace());
}
Configurator.initialize(null, source);
}
*The error that I am getting when I initialize via XML is :*
Error processing element BWLogFile ([appenders: null]): CLASS_NOT_FOUND
*The error I am getting when I initialize via properties file is :*
2021-06-30 18:27:59,879 main ERROR Unable to locate plugin for BWLogFile
2021-06-30 18:27:59,880 main DEBUG Building Plugin[name=appenders,
class=org.apache.logging.log4j.core.config.AppendersPlugin].
2021-06-30 18:27:59,882 main ERROR Unable to invoke factory method in
class org.apache.logging.log4j.core.config.AppendersPlugin for element
Appenders: java.lang.NullPointerException java.lang.NullPointerException
at
org.apache.logging.log4j.core.config.plugins.visitors.PluginElementVisitor.visit(PluginElementVisitor.java:52)
at
org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.generateParameters(PluginBuilder.java:258)
at
org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:135)
at
org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1000)
at
org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:940)
at
org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:551)
at
org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:241)
at
org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:287)
at
org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:627)
at
org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:700)
at
org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:717)
at
org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:272)
at
org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:155)
at
org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
at
org.apache.logging.log4j.LogManager.getContext(LogManager.java:196)
at
org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:137)
at
org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:45)
at
org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47)
at
org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:30)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:363)