首页 文章

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

提问于
浏览
0

我正在将我的日志框架从log4j升级到log4j2 . 我已经按照apache的步骤here进行了操作 .

  • 我将log4j.xml修改为新标准(特别是对于Appenders)

  • 将我的gradle文件升级为新的依赖项

  • 重建了我的spring-boot packager项目并进行了部署

我在部署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 回答

  • 2

    这适用于Gradle 3.2.1和Spring Boot 1.4.2 .

    • 您必须导入 spring-boot-starter-log4j2 并排除 spring-boot-starter-logging

    • log4j.xml 重命名为 log4j2.xml 并相应地进行修改(我想你已经这样做了)

    • 复制/包含 src/main/resources 中的 log4j2.xml 文件或使用 -Dlogging.config (因为它是Spring Boot应用程序时使用的不是 log4j.configurationFile )来引用它

    最后,您的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 只有"activated"):

    <?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>
    

    ...如果您在 src/main/resources 中没有 log4j2.xml 文件,那么默认情况下会选择它,或者如果您需要指定另一个,请使用 --logging.config 指令:

    $ ./gradlew bootRun -Dspring.profiles.active=default -Dfile.encoding=UTF-8 -Dlogging.config assets/log4j2.xml
    

相关问题