自定义log4j2附加程序在Java 11中不起作用

时间:2019-10-15 16:56:23

标签: java spring-boot log4j2

我正在使用JAVA-11开发spring-boot(2.1.9.RELEASE)应用程序,并使用log4j2,版本为2.12.1。我使用的是自定义log4j2附加程序,该附加程序实际上将日志保存在DB中。我尝试使用以下代码段,但无法正常工作。

public class TestApplication {
  static {
    System.setProperty("org.springframework.boot.logging.LoggingSystem", "none");
   }
   public static void main(String[] args) {
       SpringApplication.run(TestApplication.class, args);
   }
   @Bean
   public ApplicationContextProvider applicationContextProvider() {
      return new ApplicationContextProvider();
   }
}

log4j2.xml是

<?xml version="1.0" encoding="UTF-8"?>

<Configuration status="warn" monitorInterval="30" packages="test.package">
    <Appenders>
        <Console name="ConsoleAppender" target="system_out" packages="test.package">
            <PatternLayout
                pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %t %x [%X{process-param}] %c - %m%n" />
        </Console>
        <AccessLog name="accessLogAppender" />
        <Async name="asyncAccessLogAppender">
            <AppenderRef ref="accessLogAppender" />
        </Async>

        <Loggers>
        <Logger name="AccessFileLogger" level="debug" additivity="false">
            <AppenderRef ref="ConsoleAppender" />
            <AppenderRef ref="asyncAccessLogAppender" />
        </Logger>
        <Root level="off" />
    </Loggers>
</Configuration>

“自定义日志附加器”为

package test.package
@Plugin(name = "AccessLog", category = "Core", elementType = "appender", printObject = true)
public final class AccessLogAppender extends AbstractAppender
{
    private static final long serialVersionUID = 1L;
    private final ReadWriteLock rwLock = new ReentrantReadWriteLock ();
    private final Lock readLock = rwLock.readLock ();

    private Connection connection = null;

    protected AccessLogAppender (final String name)
    {
        super (name, null, null);

        try
        {   

                connection = // code snippet to get the DB Connection 

        }
        catch (SQLException e)
        {

        }
    }

    @PluginFactory
    public static AccessLogAppender createAppender (
            @PluginAttribute("name") String name)
    {
        return new AccessLogAppender (name);
    }

    @Override
    public void append (LogEvent event)
    {
        readLock.lock ();

        PreparedStatement ps = null;

        try
        {
            // code snippet to save the log into the DB

        }
        catch (SQLException e)
        {

        }
        finally
        {
            readLock.unlock ();
        }
    }

    @Override
    public void stop ()
    {
        if (connection != null)
        {
            try
            {
                connection.close ();
            }
            catch (SQLException e)
            {
                System.out.println (e.getMessage ());
            }
        }
    }
}

并出现错误。

Caused by: org.apache.logging.log4j.core.config.ConfigurationException: No appenders are available for AsyncAppender asyncAccessLogAppender

1 个答案:

答案 0 :(得分:0)

我通过将DBConnection片段作为延迟加载到追加程序中来解决了此问题。然后,当第一个呼叫转到Appener时,将建立DBConnection。它按预期工作。

package test.package
@Plugin(name = "AccessLog", category = "Core", elementType = "appender")
public final class AccessLogAppender extends AbstractAppender
{
    private static final long serialVersionUID = 1L;
    private final ReadWriteLock rwLock = new ReentrantReadWriteLock ();
    private final Lock readLock = rwLock.readLock ();

    private Connection connection = null;

    protected AccessLogAppender (final String name)
    {
        super (name, null, null);
    }
    private void init() {
if(connection == null) {
    try
        {   

                connection = // code snippet to get the DB Connection 

        }
        catch (SQLException e)
        {

        }
     }
}

    @PluginFactory
    public static AccessLogAppender createAppender (
            @PluginAttribute("name") String name)
    {
        return new AccessLogAppender (name);
    }

    @Override
    public void append (LogEvent event)
    {
        init();
        readLock.lock ();

        PreparedStatement ps = null;

        try
        {
            // code snippet to save the log into the DB

        }
        catch (SQLException e)
        {

        }
        finally
        {
            readLock.unlock ();
        }
    }

    @Override
    public void stop ()
    {
        if (connection != null)
        {
            try
            {
                connection.close ();
            }
            catch (SQLException e)
            {
                System.out.println (e.getMessage ());
            }
        }
    }
}