收到错误'当log4j 1.x到2.x迁移

时间:2017-01-24 04:58:12

标签: java logging log4j log4j2

我收到错误'线程中的异常“main”java.lang.ExceptionInInitializerError 引起:org.apache.logging.log4j.core.config.ConfigurationException:根据https://logging.apache.org/log4j/2.x/manual/migration.html使用Log4j 1.x桥从log4j1.x迁移到log4j 2.x时,AsyncAppender ASYNC'没有可用的appender'

我试图在'ASYNC'appender的appender-ref中添加'CONSOLE'appender但它对我没用。(参考:https://issues.apache.org/jira/browse/LOG4J2-894

完成堆栈跟踪:

线程“main”java.lang.ExceptionInInitializerError中的异常 引起:org.apache.logging.log4j.core.config.ConfigurationException:AsyncAppender ASYNC没有可用的appender         在org.apache.logging.log4j.core.appender.AsyncAppender.start(AsyncAppender.java:117)         在org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:255)         在org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:530)         在org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:603)         在org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:620)         在org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:226)         在org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152)         at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)         在org.apache.logging.log4j.LogManager.getContext(LogManager.java:307)         at org.apache.log4j.LogManager $ PrivateManager.getContext(LogManager.java:215)         在org.apache.log4j.LogManager.getLogger(LogManager.java:74)         at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:73)         在org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:277)         在org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:155)         在org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)         在org.apache.commons.logging.LogFactory.getLog(LogFactory.java:657)         在org.springframework.context.support.AbstractApplicationContext。(AbstractApplicationContext.java:159)         在org.springframework.context.support.AbstractApplicationContext。(AbstractApplicationContext.java:223)         在org.springframework.context.support.AbstractRefreshableApplicationContext。(AbstractRefreshableApplicationContext.java:88)         在org.springframework.context.support.AbstractRefreshableConfigApplicationContext。(AbstractRefreshableConfigApplicationContext.java:58)         在org.springframework.context.support.AbstractXmlApplicationContext。(AbstractXmlApplicationContext.java:61)         在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:136)         在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)         在com.sm.common.business.security.StrongMailSymmetricEncryptionUtility。(StrongMailSymmetricEncryptionUtility.java:53)

**我的log4j2.xml **:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <Async name="ASYNC">
      <AppenderRef ref="R"/>
      <AppenderRef ref="CONSOLE"/>
    </Async>
   <Async name="ASYNC_WARN">
     <ThresholdFilter level="WARN"/>
     <AppenderRef ref="R"/>
     <AppenderRef ref="CONSOLE"/>
   </Async
    <Console name="CONSOLE" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p [%t] %C{2} - %m%n"/>
    </Console>   
 </Appenders>

 <Loggers>    
    <Logger name="com.sm" level="WARN" additivity="false">
      <AppenderRef ref="ASYNC"/>
    </Logger> 
    <Logger name="com.sm.random" level="WARN" additivity="false">
      <AppenderRef ref="ASYNC_WARN"/>
    </Logger> 
    <Logger name="com.sm.services" level="WARN" additivity="false">
      <AppenderRef ref="ASYNC"/>
    </Logger>

    <Root level="WARN">
      <AppenderRef ref="ASYNC"/>
    </Root>
  </Loggers>
</Configuration>

感谢。

3 个答案:

答案 0 :(得分:1)

{<1}} appender应该在它引用的Appender之后定义。例如,控制台appender是在Async appender之后定义的;它应该在之前。

此外,Async appender有一个<Async name="ASYNC">...,它引用一个名为R的不存在的appender。您需要删除此AppenderRef或定义appender。

答案 1 :(得分:1)

发生此异常可能有多种原因。以下是一些:

1)项目中或您正在使用的相关jar内部有重复的log4j配置

2)在log4j文件中没有添加appender

3)如果正确添加了appender,则两个或多个log4j配置发生冲突。

就我而言,由于配置了两个log4j2.xml文件,这种情况正在发生。

答案 2 :(得分:-1)

就我而言,这是由于拒绝了日志目录访问权限而发生的