Skip to main content
 首页 » 编程设计

java之SpringBoot 1.5.x 不在 logging.file 写入文件

2023年09月11日77jillzhang

我有一个服务,在很多这样的地方都有记录器

private static final Logger LOGGER =  
LoggerFactory.getLogger(myclass.class);  

这些记录器正在写入我的控制台,但没有像这样写入我在 application.properties 文件中指定的文件

logging.file=my-service.log 

我的 pom.xml 文件

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.1.RELEASE</version> 
</parent> 
<dependencies> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-actuator</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.cloud</groupId> 
        <artifactId>spring-cloud-starter-config</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.cloud</groupId> 
        <artifactId>spring-cloud-context</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>com.microsoft.sqlserver</groupId> 
        <artifactId>sqljdbc</artifactId> 
        <version>4.2</version> 
    </dependency> 
    <dependency> 
        <groupId>org.jasypt</groupId> 
        <artifactId>jasypt-spring31</artifactId> 
        <version>1.9.2</version> 
    </dependency> 
    <dependency> 
        <groupId>org.apache.commons</groupId> 
        <artifactId>commons-lang3</artifactId> 
        <version>3.5</version> 
    </dependency> 
    <dependency> 
        <groupId>com.h2database</groupId> 
        <artifactId>h2</artifactId> 
        <version>1.4.193</version> 
    </dependency> 
    <dependency> 
        <groupId>com.maxmind.geoip</groupId> 
        <artifactId>geoip-api</artifactId> 
        <version>1.3.1</version> 
    </dependency> 
 
    <!-- AWS Dependencies --> 
    <dependency> 
        <groupId>org.springframework.cloud</groupId> 
        <artifactId>spring-cloud-aws-autoconfigure</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.cloud</groupId> 
        <artifactId>spring-cloud-aws-context</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.cloud</groupId> 
        <artifactId>spring-cloud-aws-actuator</artifactId> 
    </dependency> 
    <!-- END of AWS Dependencies --> 

我尝试添加我自己的 logback-spring.xml 文件,该文件只写入一个文件,似乎只停止打印到控制台,但它仍然不写入文件。在这里描述Spring Boot - no log file written (logging.file is not respected)

我单步执行代码并注意到 LoggingSystem 类正在正确设置属性。另外,如果我像这样排除启动器日志记录依赖性

<dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-data-jpa</artifactId> 
        <exclusions> 
            <exclusion> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-starter-logging</artifactId> 
            </exclusion> 
        </exclusions> 
    </dependency> 

然后服务日志文件由内部 Java 处理程序创建,但格式错误。我想使用 Spring 的,因为它可以在将日志级别设置为 TRACE 或 DEBUG 时显示更多信息。

有没有其他人看到这个问题并且知道如何让 Spring 将服务日志写入指定的文件?

更新

看来问题出在这些依赖项上

<dependency> 
        <groupId>org.springframework.cloud</groupId> 
        <artifactId>spring-cloud-starter-config</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.cloud</groupId> 
        <artifactId>spring-cloud-context</artifactId> 
    </dependency> 

似乎任何 springframework.cloud 依赖项都会导致项目无法创建 logging.file 文件。

请您参考如下方法:

我找到了问题和解决方案。 我需要将日志记录属性放在

logging.file=my-service.log 

bootstrap.properties 文件中。该文件应该放在 application.properties 文件所在的资源目录中。

似乎在使用 springframework.cloud 依赖时,首先调用 BootstrapApplicationListener 来初始化 LogbackLoggingSystem 类。然后,由于这些属性已初始化,因此应用程序会在 application.properties 文件中忽略它们。

更多信息 https://github.com/spring-projects/spring-boot/issues/7099