如何使用 Spring Boot 设置 log4j2 自定义附加程序?

Posted

技术标签:

【中文标题】如何使用 Spring Boot 设置 log4j2 自定义附加程序?【英文标题】:How to setup a log4j2 custom appender with Spring Boot? 【发布时间】:2020-03-04 18:27:42 【问题描述】:

我在为我的 Spring Boot 应用程序创建自定义附加程序方面面临挑战。抛出以下异常:

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

春季启动版本: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%dyyyy-MM-dd.log
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %-5p %ddd-MM-yyyy HH:mm:ss %t  %c1   %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%dyyyy-MM-dd.log
appender.errorFile.layout.type = PatternLayout
appender.errorFile.layout.pattern = %-5p %ddd-MM-yyyy HH:mm:ss %t %c1   %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 集成。

【问题讨论】:

【参考方案1】:

您必须在 log4j.properties 中添加 appender 类的包,例如 包 = org.home.appenders

【讨论】:

【参考方案2】:

错误出现在这段代码中:-

protected AmtAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions) 
    super(name, filter, layout,ignoreExceptions);
    // TODO Auto-generated constructor stub

AmtAppender 是什么?构造函数名称应与类名称相同。您需要以这种方式提供构造函数:-

@SuppressWarnings("deprecation")
protected MyCustomAppender(String name, Filter filter)


    super(name, filter, null);

【讨论】:

以上是关于如何使用 Spring Boot 设置 log4j2 自定义附加程序?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Spring Boot 将 Log4j 添加到 maven 项目

Java框架spring Boot学习笔记(十四):log4j介绍

如何使用 Spring Boot 将 log4j.xml、应用程序属性和 jar 外部化?

spring-boot - 外部 log4j 配置不起作用

如何使用 Spring Boot 微服务 Extern log4j.properties 文件并将其作为 Linux 服务运行?

Spring boot中使用log4j