如何使用 Spring Boot 设置 log4j2 自定义附加程序?
How to setup a log4j2 custom appender with Spring Boot?
我在为我的 spring 引导应用程序创建自定义附加程序时面临挑战。抛出以下异常:
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 引导版本:2.1.7
Maven插件版本: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
注意:删除 log4j2.properties 中的 appender 引用后,日志将写入 log4j2.properties 中提到的路径中的文件。因此,问题在于 customappender 集成。
您必须在 log4j.properties 中添加您的 appender class 的包,例如
包 = org.home.appenders
错误在这段代码中:-
protected AmtAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions) {
super(name, filter, layout,ignoreExceptions);
// TODO Auto-generated constructor stub
}
什么是AmtAppender
?构造函数名称应与 class 名称相同。您需要以这种方式提供构造函数:-
@SuppressWarnings("deprecation")
protected MyCustomAppender(String name, Filter filter)
{
super(name, filter, null);
}
我在为我的 spring 引导应用程序创建自定义附加程序时面临挑战。抛出以下异常:
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 引导版本:2.1.7
Maven插件版本: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
注意:删除 log4j2.properties 中的 appender 引用后,日志将写入 log4j2.properties 中提到的路径中的文件。因此,问题在于 customappender 集成。
您必须在 log4j.properties 中添加您的 appender class 的包,例如 包 = org.home.appenders
错误在这段代码中:-
protected AmtAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions) {
super(name, filter, layout,ignoreExceptions);
// TODO Auto-generated constructor stub
}
什么是AmtAppender
?构造函数名称应与 class 名称相同。您需要以这种方式提供构造函数:-
@SuppressWarnings("deprecation")
protected MyCustomAppender(String name, Filter filter)
{
super(name, filter, null);
}