这适用于 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 引用它(不是您使用的log4j.configurationFile,因为它是一个Spring Boot 应用程序)
最后,您的 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 配置文件应如下所示(这只有登录到控制台的附加程序,并且 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>
...如果您在src/main/resources 中没有log4j2.xml 文件,那么默认情况下会选择它,或者如果您需要指定其他文件,请使用--logging.config 指令:
$ ./gradlew bootRun -Dspring.profiles.active=default -Dfile.encoding=UTF-8 -Dlogging.config assets/log4j2.xml