Java log4j2弹簧引导查找can';无法从application.properties读取属性
我希望log4j2-spring.xml从application.properties文件中读取属性。但log4j2-spring.xml似乎无法读取此信息。我已经读过书来实现这一点 我在这个网站上看到过。我也这样试过。但这对我没有帮助 我的build.gradle是这样的:Java log4j2弹簧引导查找can';无法从application.properties读取属性,java,spring-boot,gradle,log4j2,Java,Spring Boot,Gradle,Log4j2,我希望log4j2-spring.xml从application.properties文件中读取属性。但log4j2-spring.xml似乎无法读取此信息。我已经读过书来实现这一点 我在这个网站上看到过。我也这样试过。但这对我没有帮助 我的build.gradle是这样的: plugins { id 'org.springframework.boot' version '2.5.0-SNAPSHOT' id 'io.spring.dependency-management' v
plugins {
id 'org.springframework.boot' version '2.5.0-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
ext {
log4j2version = '2.14.1'
}
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.18'
annotationProcessor 'org.projectlombok:lombok:1.18.18'
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
implementation "org.apache.logging.log4j:log4j-spring-cloud-config-client:${log4j2version}"
implementation "org.apache.logging.log4j:log4j-api:${log4j2version}"
implementation "org.apache.logging.log4j:log4j-core:${log4j2version}"
}
configurations {
all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: "ch.qos.logback", module: "logback-classic"
exclude group: "org.springframework.cloud", module: "spring-cloud-bus"
}
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss:SSS} [%thread] %-5level %logger{2}:%L - %msg%n"/>
</Console>
<RollingFile name="CATALINA-FILE" fileName="$${spring:commission.base.path1}/logs/catalina.log"
filePattern="$${spring:commission.base.path1}/logs/archived/catalina_%d{yyyy-MM-dd}.log.gz">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{20}:%L - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="1 GB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<root level="debug">
<appender-ref ref="CATALINA-FILE"/>
<appender-ref ref="console"/>
</root>
</Loggers>
</configuration>
application.properties文件是:
commission.base.path1=C:/Users/user1/Downloads/demo1
logging.config=classpath:log4j2-spring.xml
spring.cloud.config.enabled=false
log4j2-spring.xml如下所示:
plugins {
id 'org.springframework.boot' version '2.5.0-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
ext {
log4j2version = '2.14.1'
}
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.18'
annotationProcessor 'org.projectlombok:lombok:1.18.18'
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
implementation "org.apache.logging.log4j:log4j-spring-cloud-config-client:${log4j2version}"
implementation "org.apache.logging.log4j:log4j-api:${log4j2version}"
implementation "org.apache.logging.log4j:log4j-core:${log4j2version}"
}
configurations {
all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: "ch.qos.logback", module: "logback-classic"
exclude group: "org.springframework.cloud", module: "spring-cloud-bus"
}
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss:SSS} [%thread] %-5level %logger{2}:%L - %msg%n"/>
</Console>
<RollingFile name="CATALINA-FILE" fileName="$${spring:commission.base.path1}/logs/catalina.log"
filePattern="$${spring:commission.base.path1}/logs/archived/catalina_%d{yyyy-MM-dd}.log.gz">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{20}:%L - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="1 GB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<root level="debug">
<appender-ref ref="CATALINA-FILE"/>
<appender-ref ref="console"/>
</root>
</Loggers>
</configuration>
如何解决这个问题?提前感谢。问题是您是否需要在Spring云上配置Log4j 问题 如果不是的话,我会说,org.apache.logging.log4j:2.14.1依赖性是一种过分的行为。它带来了您不需要的Spring云依赖性。在某种程度上,我还没有弄清楚,它还与spring-boot-starter-log4j2发生了干涉,导致多次初始化日志上下文,然后作为一个副作用,您在启动时有这个例外,因为spring的属性没有解决 解决方案 请注意,您不需要整个log4jspring云配置客户端甚至spring-boot-starter-log4j2 以下依赖项将设置日志记录上下文:
- log4j
- log4j-slf4j-impl
- log4j弹簧靴
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.18'
annotationProcessor 'org.projectlombok:lombok:1.18.18'
// Spring Boot dependency
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// Logging dependencies
// Base Log4j 2
implementation "org.apache.logging.log4j:log4j-api:${log4j2version}"
// Will resolve spring properties
implementation "org.apache.logging.log4j:log4j-spring-boot:${log4j2version}"
// Log4j 2 to Slf4j binder
implementation "org.apache.logging.log4j:log4j-slf4j-impl:${log4j2version}"
}
应该说,log4j spring boot依赖关系足以从spring配置文件解析查找变量
但是,如果您确实需要通过云配置配置Log4J,那么您必须再次仔细查看依赖关系是如何解决的,以及您真正需要哪些依赖关系。试试这一个$istead$$ fileName=“${spring:commission.base.path1}/logs/catalina.log” filePattern=“${spring:commission.base.path1}/logs/archived/catalina_%d{yyyy-MM-dd}.log.gz”> 实现“org.apache.logging.log4j:log4j spring boot” 在build.gradle中,它现在正在工作 实施 实现'org.springframework.boot:spring-boot-starter-log4j2' 足够使用log4j2版本2.14.1,因为spring引导版本是2.5.0-SNAPSHOT。无需为此显式添加对org.apache.logging.log4j的依赖项 所以我的最终版本是:
plugins {
id 'org.springframework.boot' version '2.5.0-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.18'
annotationProcessor 'org.projectlombok:lombok:1.18.18'
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
implementation "org.apache.logging.log4j:log4j-spring-boot"
}
configurations {
all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
}
而无需要将其保存在应用程序中。属性:
spring.cloud.config.enabled=false
是的,应该添加log4j spring boot依赖项足以实现spring查找。尝试将log4j2-spring.xml中的$$转换为单个$@SachinI看到LoggingApplicationListener类初始化记录器两次。第一个默认SpringBoot记录器没有解析的applocation配置属性,因此它尝试查找引发异常的文本路径“${spring:commission.base.path1}/logs/catalina.log”。然后,第二次使用解析路径正确配置的记录器被注入。另外,安装看起来很粗糙,但是找不到更好的例子来说明如何正确使用log4j-spring-cloud-config客户端。