spring-boot-logger.log 已部署 spring 启动应用程序的文件位置

spring-boot-logger.log file location for deployed spring boot app

我按照 this 教程在我的应用程序中设置了 spring 启动日志记录,它在我的开发环境中正常工作,但在我部署该应用程序之后就不行了。

部署应用程序时,spring 应用程序日志文件应转到 ${CATALINA_BASE}/logs/spring-boot-logger.log。我该如何配置?

我的logback-spring.xml:

<property name="LOGS" value="./logs" />

<appender name="Console"
          class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
        </Pattern>
    </layout>
</appender>

<appender name="RollingFile"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOGS}/spring-boot-logger.log</file>
    <encoder
            class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
    </encoder>

    <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily and when the file reaches 10 MegaBytes -->
        <fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>

<!-- LOG everything at INFO level -->
<root level="info">
    <appender-ref ref="RollingFile" />
    <appender-ref ref="Console" />
</root>

<!-- LOG "com.baeldung*" at TRACE level -->
<logger name="xxx.app" level="trace" additivity="false">
    <appender-ref ref="RollingFile" />
    <appender-ref ref="Console" />
</logger>

当我使用此配置在部署服务器上启动应用程序时,我得到:

java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[RollingFile] - Failed to create parent directories for [/./logs/spring-boot-logger.log]
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[RollingFile] - openFile(./logs/spring-boot-logger.log,true) call failed. java.io.FileNotFoundException: ./logs/spring-
boot-logger.log (No such file or directory)

我也试过按照建议 在 application.properties 中配置 logging.file.name = ${catalina.base}/logs/${service.name}.log 但是当 运行 mvn clean install 在本地

时我收到一个错误
[ERROR] contextLoads  Time elapsed: 0.004 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'catalina.base' in value "${catalina.base}/logs/${service.name}.log"

您想将日志文件写入 ${CATALINA_BASE}/logs 目录,但您在配置文件中使用了 ./logs。如果您的 Tomcat 服务器的工作目录是 ${CATALINA_BASE},这当然会起作用,但是假设 Tomcat 的 工作目录 是不安全的=] 服务器(在你的情况下它不是 ${CATALINA_BASE})。

在您的 logback-spring.xml 文件中使用 variable substitution

<property name="LOGS" value="${catalina.base:-.}/logs" />

当没有catalina.base 属性时(即使用内嵌的Tomcat),将使用默认值.