Spring-Boot项目log4j到log4j2转换问题

时间:2017-01-20 19:49:01

标签: java spring-boot log4j log4j2

我正在将日志框架从log4j升级到log4j2。我遵循了apache的here步骤。

  1. 我将log4j.xml修改为新标准(特别是对于Appenders)
  2. 将我的gradle文件升级为新的依赖项
  3. 重建我的spring-boot packager项目并进行部署
  4. 我在部署jar时使用这些系统属性:

      

    java Dspring.profiles.active = dev -Dlog4j.configurationFile = log4j2.xml -jar application.jar

    但是,我的应用程序继续运行log4j而不是log4j2。设置调试属性Dlog4j.debug时,我可以看到log4j正在尝试查找xml,属性等,然后说

    No appenders could be found for logger. See http://logging.apache.org/log4j/1.2/faq.

    所以在某个地方,我正在推出1.2版本?

    我认为log4j2正在运行的唯一原因是当我将log4j2.xml的名称更改为log4j.xml时,我得到一个log4j警告说明 log4j2.xml could not be found. Only displying error messages to the console.即使log4j随之吐出消息。

    当从log4j和log4j2切换时,任何人都会遇到类似的东西并且可以提供一些帮助吗?

1 个答案:

答案 0 :(得分:2)

这适用于Gradle 3.2.1和Spring Boot 1.4.2。

  1. 您必须导入spring-boot-starter-log4j2并排除spring-boot-starter-logging
  2. log4j.xml重命名为log4j2.xml并相应修改(我认为您已经这样做了)
  3. 复制/包含log4j2.xml内的src/main/resources文件或使用-Dlogging.config(而不是log4j.configurationFile引用它,因为它是春天启动应用程序)
  4. 最后,您的Gradle配置文件应该如下(摘录):

    buildscript {
      repositories {
        mavenLocal()
      }
    
      dependencies {
        classpath('io.spring.gradle:dependency-management-plugin:0.6.0.RELEASE')
        classpath('org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE')
      }
    }
    
    plugins {
      // ...
    }
    
    //apply from: 'gradle/database.gradle'
    
    apply plugin: 'idea'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    
    repositories {
      mavenCentral()
      jcenter()
    }
    
    configurations {
      all*.exclude module: 'spring-boot-starter-logging'
      //all*.exclude module: 'jboss-logging-annotations'
      //all*.exclude module: 'jboss-logging'
    }
    
    dependencyManagement {
      imports {
        mavenBom("org.springframework.boot:spring-boot-dependencies:1.4.2.RELEASE")
      }
    }
    
    dependencies {
      compile 'com.lmax:disruptor:3.3.5'
      compile 'org.springframework.boot:spring-boot-starter-actuator'
      compile 'org.springframework.boot:spring-boot-starter-log4j2'
      compile 'org.springframework.boot:spring-boot-starter-undertow'
      compile 'org.springframework:spring-webmvc'
    }
    
    task wrapper(type: Wrapper) {
      gradleVersion '3.2.1'
    }
    

    ...你的Log4j 2.x配置文件看起来应该是这样的(这只有登录到控制台的appender,DEBUG级别只有"激活"对于这个命名空间/包io.shido):

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration>
      <Properties>
        <Property name="log-pattern">%d{MM-dd-yyyy HH:mm:ss.SSS} |- %highlight{%5p}{TRACE=blue, DEBUG=green, INFO=green, WARN=yellow, ERROR=red, FATAL=red} in %style{%C{1}:%L}{cyan} [%style{%t#${sys:PID}}{magenta}] - %m%n</Property>
      </Properties>
    
      <Appenders>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
          <PatternLayout pattern="${log-pattern}" />
        </Console>
      </Appenders>
    
      <!-- Logger levels: ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
      <Loggers>
        <AsyncLogger name="io.shido" level="DEBUG" additivity="false" includeLocation="true">
          <AppenderRef ref="Console" />
        </AsyncLogger>
    
        <Root level="WARN">
          <AppenderRef ref="Console" />
        </Root>
      </Loggers>
    </Configuration>
    

    ...如果您在log4j2.xml内没有src/main/resources文件,那么默认情况下会将其选中,或者如果您需要指定其他文件,请使用{{ 1}}指令:

    --logging.config