gpt4 book ai didi

java - 启动应用程序时出现 FileNotFoundException (Logback)

转载 作者:行者123 更新时间:2023-11-28 23:13:45 24 4
gpt4 key购买 nike

在 tomcat 8.5.0 上运行 SpringBoot 2.0.3 应用程序时出现以下错误:

ERROR in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - openFile(C:\Program Files\Apache Software Foundation\Tomcat 9.0\temp/spring.log,true) call failed. java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\Tomcat 9.0\temp\spring.log (Access is denied)

该模块的

logback-spring.xml如下:

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

<springProperty scope="context" name="springAppName" source="spring.application.name"/>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<include resource="logback-json.xml"/>
<property name="LOG_PATTERN"
value="%date{yyyy-MM-dd HH:mm:ss.SSS}|${springAppName}|[%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]|%thread|%.-1level|%logger{10}|%msg%n"/>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>

<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/${springAppName}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/${springAppName}-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<maxFileSize>200MB</maxFileSize>
</rollingPolicy>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>

<logger name="pl.X" level="debug"/>

<logger name="jdbc.sqltiming" level="info"/>

<appender name="ASYNC_REQUEST_RESPONSE_JSON_LOG" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="REQUEST_RESPONSE_JSON_LOG"/>
</appender>

<appender name="ASYNC_ROLLING" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="ROLLING"/>
</appender>
<appender name="ASYNC_SERVER_JSON_LOG" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="SERVER_JSON_LOG"/>
</appender>
<logger name="somePakaes.trace.XLoggingFilter" additivity="false" level="DEBUG">
<appender-ref ref="ASYNC_REQUEST_RESPONSE_JSON_LOG"/>
</logger>
<root level="WARN">
<appender-ref ref="ASYNC_ROLLING"/>
<appender-ref ref="ASYNC_SERVER_JSON_LOG"/>
<appender-ref ref="CONSOLE"/>
</root>

这里是上面文件引用的logback-json.xml:

<?xml version="1.0" encoding="UTF-8"?>
<included>
<springProperty scope="context" name="springAppName" source="spring.application.name"/>

<appender name="SERVER_JSON_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/${springAppName}.json</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/${springAppName}-%d{yyyy-MM-dd}.%i.json.zip</fileNamePattern>
<maxFileSize>200MB</maxFileSize>
<maxHistory>2</maxHistory>
</rollingPolicy>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<pattern>
<pattern>
{
"date":"%date",
"severity": "%level",
"service": "${springAppName}",
"trace": "%X{X-B3-TraceId:-}",
"span": "%X{X-B3-SpanId:-}",
"parent": "%X{X-B3-ParentSpanId:-}",
"exportable": "%X{X-Span-Export:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger{26}",
"message": "%message",
"ex": "%ex"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>

<logger name="org.springframework.cloud.sleuth.instrument.web.TraceFilter" level="error"/>
<logger name="org.springframework.cloud.sleuth.instrument.web.TraceHandlerInterceptor" level="error"/>


<appender name="REQUEST_RESPONSE_JSON_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/${springAppName}_request_response.json</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/${springAppName}_request_response-%d{yyyy-MM-dd}.%i.json.zip</fileNamePattern>
<maxFileSize>200MB</maxFileSize>
<maxHistory>2</maxHistory>
</rollingPolicy>

<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<pattern>
<pattern>
{
"date":"%date",
"service": "${springAppName}",
"severity": "%level",
"trace": "%X{X-B3-TraceId:-}",
"span": "%X{X-B3-SpanId:-}",
"parent": "%X{X-B3-ParentSpanId:-}",
"pid": "${PID:-}",
"thread": "%thread",
"message": "%message"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
</included>

但当以 admin 身份运行 InteliJ 时,问题就消失了。

最佳答案

您可能遇到这种情况的两个原因:

  1. 看看你是否有权限读取 spring.log 文件这是java.io.FileNotFoundException 最明显的原因:(访问是denied) 异常,我没有足够的权限,所以我立即提供对日志的读、写和执行权限文件并重试。

  2. 查看是否有其他进程正在使用该文件这可能是另一个进程java.io.FileNotFoundException 的可能原因:(访问是拒绝)。

关于java - 启动应用程序时出现 FileNotFoundException (Logback),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53285083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com