我正在将日志框架从log4j升级到log4j2。我遵循了apache的here步骤。
我在部署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切换时,任何人都会遇到类似的东西并且可以提供一些帮助吗?
答案 0 :(得分:2)
这适用于Gradle 3.2.1和Spring Boot 1.4.2。
spring-boot-starter-log4j2
并排除spring-boot-starter-logging
log4j.xml
重命名为log4j2.xml
并相应修改(我认为您已经这样做了)log4j2.xml
内的src/main/resources
文件或使用-Dlogging.config
(而不是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
):
<?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