当前位置: 首页 > 知识库问答 >
问题:

log4j2Spring引导查找无法从application.properties读取属性

印晋
2023-03-14

我想要log4j2Spring。xml从应用程序中读取属性。属性文件。但似乎是Spring。xml无法读取此内容。我读过https://logging.apache.org/log4j/2.x/manual/lookups.html#SpringLookup为了实现这一点。

我在这个网站上看到了这个答案。我也这样试过。但这对我没有帮助。

我的身材。格雷德尔是这样的:

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"
    }
}

application.properties文件是:

commission.base.path1=C:/Users/user1/Downloads/demo1
logging.config=classpath:log4j2-spring.xml

spring.cloud.config.enabled=false

log4j2-spring.xml是这样的:

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

但每次我运行代码,我得到这个异常:

2021-05-21 21:28:38,409 main ERROR Unable to create file ${spring:commission.base.path1}/logs/catalina.log java.io.IOException: The filename, directory name, or volume label syntax is incorrect
    at java.io.WinNTFileSystem.canonicalize0(Native Method)
    at java.io.WinNTFileSystem.canonicalize(WinNTFileSystem.java:428)
    at java.io.File.getCanonicalPath(File.java:618)
    at java.io.File.getCanonicalFile(File.java:643)
    at org.apache.logging.log4j.core.util.FileUtils.makeParentDirs(FileUtils.java:135)
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager$RollingFileManagerFactory.createManager(RollingFileManager.java:720)
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager$RollingFileManagerFactory.createManager(RollingFileManager.java:704)
    at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:113)
    at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:100)
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.getFileManager(RollingFileManager.java:217)
    at org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:146)
    at org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:62)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1000)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:940)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:932)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:551)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:241)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:287)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:627)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:304)
    at org.apache.logging.log4j.spring.boot.Log4j2CloudConfigLoggingSystem.loadConfiguration(Log4j2CloudConfigLoggingSystem.java:123)
    at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.loadConfiguration(Log4J2LoggingSystem.java:181)
    at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:80)
    at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60)
    at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.initialize(Log4J2LoggingSystem.java:163)
    at org.apache.logging.log4j.spring.boot.Log4j2CloudConfigLoggingSystem.initialize(Log4j2CloudConfigLoggingSystem.java:80)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:312)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:281)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:216)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:82)
    at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:63)
    at java.util.ArrayList.forEach(ArrayList.java:1259)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:62)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:373)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:331)
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:144)
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:212)
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:117)
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:74)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:82)
    at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:63)
    at java.util.ArrayList.forEach(ArrayList.java:1259)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:62)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:373)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:331)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1336)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1325)
    at com.example.demo1.Demo1Application.main(Demo1Application.java:12)

如何解决这个问题?提前谢谢。

共有3个答案

舒阳州
2023-03-14

我刚才补充说

实现"org.apache.logging.log4j: log4j-Spring-引导"

内置的。gradle和它现在正在工作。

实施

实现'org.springframework.boot: sping-boo-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'
    }
}

没有必要把这个放在application.properties:

spring.cloud.config.enabled=false

是的,log4j的官方文档应该补充,log4j的Spring Boot依赖性足以实现spring查找。

陆烨磊
2023-03-14

试试这个$istead$$

fileName=“${spring:commission.base.path1}/logs/catalina.log”filePattern=“${spring:commission.base.path1}/logs/archived/catalina_u%d{yyyyy-MM-dd}.log.gz”

黄宏旷
2023-03-14

问题是您是否需要通过Spring Cloud进行Log4j配置?

问题

如果不是,我会说org。阿帕奇。登录中。log4j:2.14。1依赖是一种过分的行为。它带来了您不需要的Spring云依赖性。在某种程度上,我仍然没有弄清楚,它还与spring-boot-starter-log4j2相互干扰,导致多次初始化日志上下文,然后作为一个副作用,您在启动时有这个例外,因为spring的属性没有得到解决。

解决方案

请注意,您不需要整个log4j spring云配置客户端,甚至不需要spring-boot-starter-log4j2。

以下依赖项将设置日志上下文:

  • log4j
  • log4j-slf4j-impl
  • log4jSpring靴

我在GitHub存储库中放了一个示例程序。变量名略有更改,并且有注释解释每个依赖项的用途。

Gradle构建文件摘录

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引导依赖关系足以解决Spring配置文件中的查找变量。

但如果您真的需要通过云配置配置Log4J,那么您必须再次仔细查看如何解决依赖关系,以及您真正需要哪些依赖关系。

 类似资料:
  • 我有这种情况。我正在用Spring Boot开发一个REST服务,我有一个dependency jar,这是另一个Spring Boot项目,我使用以下方法将其作为一个依赖项包括在内: https://docs.spring.io/spring-boot/docs/2.1.4.release/maven-plugin/examples/repackage-classifier.html 一切都编译

  • 我无法找出是否可以在pom.xml文件中读取Spring的application.properties中的任何数据。 拜托,有人能帮我吗? 谢谢你。

  • 我是新来的node.js.我正在尝试创建一个注册页面,并使用mysql将输入的值保存在数据库中。 我可以通过在请求头上传递参数来实现这一点。但是,在请求头中传递参数始终不是一个好主意。所以尝试在请求体上传递参数。 我使用邮差测试我的node.js服务。 下面是我的演示应用程序的github链接 https://github.com/debasish283/node_gulp 我还需要做什么来传递请

  • 问题内容: 我正在尝试从无头CMS(Sanity.io)返回一些数据,但是我的React一直遇到错误。我已遵循此示例,但是现在出现错误 我的密码 关于我在做什么错的任何想法吗? 问题答案: 渲染组件时,可以将country定义为未定义或为null。确保它在初始渲染时是数组: 或者, 您可以将地图用于这样的国家/地区: 这样可以确保仅在有国家/地区时才运行地图。

  • 我想在spring boot web Project中使用来自Application.Properties的@Value注释获取值。我认为我可以使用@value注释,因为我认为application.properties中的变量是动态加载的。但是当我只写@value注释时,它就不起作用了。 我在申请中有一个属性。poperties 我如何解决这个问题??

  • 我的logback-spring.xml从application.properties中读取正常,但从application.yml中读取不正常。在我的项目中,我们被要求仅使用YAML格式,因为该格式正在同一项目中的其他微服务中使用,所以我无法添加属性文件。请帮助我为什么我的application.yml在logback.xml中没有被读取 我尝试了各种方法,并在stackoverflow上搜索了