Spring Boot 在测试期间使用 /tmp/spring.log 文件

问题描述 投票:0回答:3

我的一个 Spring Boot 应用程序在 Maven test 阶段出现问题。

在测试和“常规”应用程序运行时,Spring Boot 应用程序都使用与

src/main/resources/logback-spring.xml
非常相似的 logback 配置文件。此配置文件(传递性)包括 logback 配置文件
base.xml
file-appender.xml
。这些配置文件设置了 logback 属性
LOG_FILE=/tmp/spring.log

我想最好的做法是文件

/tmp/server.log
由用户和组
${MY_SPRING_BOOT_APPLICATION}
拥有。

Jenkins 以用户

jenkins
身份运行。
jenkins
没有
/tmp/server.log
的写入权限。因此,JUnit 测试在由 Jenkins 执行时会失败。

  • 配置日志记录的最佳方法是什么,以便它在 Jenkins build-with-tests and 期间正常工作,以便在利用 Spring Boot 的 SysV init.d 服务功能时设置每日滚动日志记录 (将日志放入
    /var/log/
    )?
  • 如果同时运行两个或多个 Spring Boot 应用程序,文件
    /tmp/spring.log
    会同时被修改(并因此被破坏)吗?
logging spring-boot logback
3个回答
16
投票

在我的 Spring Boot 应用程序中,我已将

<property name="LOG_TEMP" value="./logs"/>
添加到
src/test/resources/logback-test.xml

<?xml version="1.0" encoding="UTF-8"?>

<configuration scan="true">
    <property name="LOG_TEMP" value="./logs"/>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>

    <logger name="com.example" level="INFO"/>


    <root level="WARN">
        <appender-ref ref="CONSOLE"/>
    </root>

</configuration>

这样,在 Maven 测试期间,将在当前(测试)工作目录中创建一个单独的日志文件。

支持welcor帮忙


2
投票

我们遇到了一个问题,即在同一服务器上运行多个 Spring Boot 模块会导致多个进程尝试写入

/tmp/spring.log
。我们通过更新
logback-spring.xml
以在日志名称中包含
${PID}
来解决此问题:

<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>

<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring-${PID}.log}"/>

相关 Spring 文档:https://docs.spring.io/spring-boot/docs/1.5.x/reference/html/howto-logging.html#howto-configure-logback-for-logging


0
投票

如果权限不够,可以使用chmod命令修改:

 chmod +r /tmp/spring.log
© www.soinside.com 2019 - 2024. All rights reserved.