使用 logback 配置在 Ubuntu 上部署 SpringBoot 失败并出现 FileNotFoundException(权限被拒绝)

SpringBoot deployment on Ubuntu with logback configuration fails with FileNotFoundException (Permission Denied)

我正在尝试将简单的 SpringBoot 应用程序作为 Upstart 服务部署到 ubuntu 服务器上。 此应用程序具有 logback-spring.xml 配置,指向创建服务日志文件的完全相同的位置,即 /var/log/upstart 目录。

但是当我部署这个应用程序时它失败并出现以下错误,表明存在一些权限问题 (Permission Denied),

Exception in thread "main" java.lang.IllegalStateException: Logback configuration error detected: java.lang.IllegalStateException: Logback configuration error detected: ERROR in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - openFile(/var/log/upstart/app.log,true) call failed. java.io.FileNotFoundException: /var/log/upstart.app.log (Permission denied) ERROR in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - openFile(/var/log/upstart/app.log,true) call failed. java.io.FileNotFoundException: /var/log/upstart/app.log (Permission denied) at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:153) at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:153) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:71) at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:71) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:49) at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:49) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:106) at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:106) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:262) at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:262) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:233) at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:233) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:200) at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:200) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:176) at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:176) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:119) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:119) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:111) at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:65) at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:111) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]

Logback-spring.xml 文件,

<?xml version="1.0" encoding="UTF-8"?>
<!-- Enable jmxConfiguration to allow dynamic level change through spring boot admin -->
<jmxConfigurator/>

<property resource="log.properties" />

<!-- Standard console appender used in all environments. Upstart catches logs and stores in /var/log/upstart-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>[%-5level] [%t] %d %logger{35} - %msg%n</pattern>
    </encoder>
</appender>

<springProfile name="local">
    <appender name="local" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.local.path}/${log.name.async}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${log.local.path}/%d{yyyy-MM-dd,aux}/${log.name.async}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <!-- keep 30 days' worth of history -->
            <maxHistory>7</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- or whenever the file size reaches 256MB -->
                <maxFileSize>256MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%-5level [%t] %d{yyyy-MM-dd_HH:mm:ss.SSS} %logger{35} - %msg%n</Pattern>
        </encoder>
    </appender>
</springProfile>

<root level="INFO">
    <appender-ref ref="console"/>
    <appender-ref ref="local"/>
</root>

log.properties 文件,

log.local.path=/var/log/upstart
log.name.async=propspace-async

我终于想通了这个问题。虽然错误非常简单,但有些事情我需要重新研究。

用户 'appuser' 在 ubuntu 上 service owned

日志指向的目录 (/var/log/upstart/) 不允许用户“appuser”使用。日志目录归用户“root”所有。因此,我创建了另一个目录 (/var/log/app/),它也属于同一用户,即 'appuser' 并启动了应用程序。

问题解决了。