I am facing challenges on creating a custom appender for my spring boot application. The following exception is thrown:
2019-11-08 10:33:27,897 main ERROR Unable to locate plugin type for
mycustomhandlerclass
2019-11-08 10:33:27,961 main ERROR Unable to locate plugin for
mycustomhandlerclass
2019-11-08 10:33:27,961 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
Spring boot version : 2.1.7
Maven plugin version: 3.1.1
Mycustomhandlerclass.java:
import java.io.Serializable;
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.LogManager;
import org.apache.logging.log4j.Logger;
@Plugin(category = "Core", name = "Mycustomhandlerclass", elementType = "appender")
public class Mycustomhandlerclass extends AbstractAppender {
private final Logger logger = LogManager.getLogger(getClass().getName());
@SuppressWarnings("deprecation")
protected AmtAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions) {
super(name, filter, layout,ignoreExceptions);
// TODO Auto-generated constructor stub
}
@PluginFactory
public static Mycustomhandlerclass createAppender(@PluginAttribute("name") String name, @PluginAttribute("ignoreExceptions") boolean ignore, @PluginElement("Filter") Filter filter, @PluginElement("Layout") Layout<? extends Serializable> layout) {
return new Mycustomhandlerclass(name, filter, layout, ignore);
}
@Override
public void append(LogEvent event) {
// TODO Auto-generated method stub
logger.info("Entered CustomAppender");
}
}
log4j2.properties:
name=PropertiesConfig
appenders = file, errorFile, customAppender
property.filename = D:/logdestinationfolder
appender.file.type = RollingFile
appender.file.name = OUTFILE
appender.file.fileName = ${filename}/console.log
appender.file.filePattern = ${filename}/console%d{yyyy-MM-dd}.log
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %-5p %d{dd-MM-yyyy HH:mm:ss} %t %c{1} %m%n
appender.file.policies.type = Policies
appender.file.policies.time.type = TimeBasedTriggeringPolicy
appender.file.policies.time.interval = 1
appender.file.strategy.type=DefaultRolloverStrategy
appender.file.strategy.max=5
appender.errorFile.type = RollingFile
appender.errorFile.name = ERROROUTFILE
appender.errorFile.fileName = ${filename}/error.log
appender.errorFile.filePattern = ${filename}/error%d{yyyy-MM-dd}.log
appender.errorFile.layout.type = PatternLayout
appender.errorFile.layout.pattern = %-5p %d{dd-MM-yyyy HH:mm:ss} %t %c{1} %M() :: %L %m%n
appender.errorFile.policies.type = Policies
appender.errorFile.policies.time.type = TimeBasedTriggeringPolicy
appender.errorFile.policies.time.interval = 1
appender.errorFile.strategy.type=DefaultRolloverStrategy
appender.errorFile.strategy.max=5
appender.errorFile.filter.1.type=ThresholdFilter
appender.errorFile.filter.1.onMatch=ACCEPT
appender.errorFile.filter.1.level=ERROR
appender.customAppender.type = Mycustomhandlerclass
appender.customAppender.name = CUSTOMAPPENDER
appender.customAppender.filter.1.type=ThresholdFilter
appender.customAppender.filter.1.onMatch=ACCEPT
appender.customAppender.filter.1.level=WARN
rootLogger.level = ERROR
loggers=appLogger
logger.appLogger.name = com.solartis
logger.appLogger.level = DEBUG
logger.appLogger.appenderRefs=errorFile,file,customAppender
logger.appLogger.appenderRef.errorFile.ref = ERROROUTFILE
logger.appLogger.appenderRef.file.ref = OUTFILE
logger.appLogger.appenderRef.customAppender.ref = CUSTOMAPPENDER
Note: On removing the appender references in log4j2.properties, the logs are written to files in the mentioned path in log4j2.properties. So, the problem is with customappender integration.
You have to add the package of your appender class in log4j.properties e.g. packages = org.home.appenders