问题背景
最近使用springboot开发一个后台项目,使用的springboot版本是2.4.0
,使用Logback作为日志框架,项目启动的时候出现了java.lang.IllegalStateException
错误。不知道你能不能发现问题出在哪里了?
错误日志信息如下所示:
Logging system failed to initialize using configuration from 'null'
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[TRACE_FILE] - Failed to create parent directories for [C:\Program Files (x86)\Common Files\Oracle\Java\javapath;D:\Program Files (x86)\VMware\VMware Workstation\bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\gsudo\Current;C:\Program Files\nodejs\;D:\Program Files\Java\jdk-1.8\bin;D:\Program Files\Java\jdk-1.8\jre\bin;D:\Program Files\Git\cmd;D:\Program Files\Tencent\微信web开发者工具\dll;D:\Program Files\apache-maven-3.9.5\bin;D:\Program Files\MySQL\MySQL Server 8.0\bin;C:\Users\calos\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\calos\AppData\Local\Programs\Python\Python311\;C:\Users\calos\AppData\Local\Microsoft\WindowsApps;C:\Users\calos\AppData\Roaming\npm;C:\Users\calos\AppData\Local\Programs\Microsoft VS Code\bin\trace.log]
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[TRACE_FILE] - openFile(C:\Program Files (x86)\Common Files\Oracle\Java\javapath;D:\Program Files (x86)\VMware\VMware Workstation\bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\gsudo\Current;C:\Program Files\nodejs\;D:\Program Files\Java\jdk-1.8\bin;D:\Program Files\Java\jdk-1.8\jre\bin;D:\Program Files\Git\cmd;D:\Program Files\Tencent\微信web开发者工具\dll;D:\Program Files\apache-maven-3.9.5\bin;D:\Program Files\MySQL\MySQL Server 8.0\bin;C:\Users\calos\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\calos\AppData\Local\Programs\Python\Python311\;C:\Users\calos\AppData\Local\Microsoft\WindowsApps;C:\Users\calos\AppData\Roaming\npm;C:\Users\calos\AppData\Local\Programs\Microsoft VS Code\bin/trace.log,true) call failed. java.io.FileNotFoundException: C:\Program Files (x86)\Common Files\Oracle\Java\javapath;D:\Program Files (x86)\VMware\VMware Workstation\bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\gsudo\Current;C:\Program Files\nodejs\;D:\Program Files\Java\jdk-1.8\bin;D:\Program Files\Java\jdk-1.8\jre\bin;D:\Program Files\Git\cmd;D:\Program Files\Tencent\微信web开发者工具\dll;D:\Program Files\apache-maven-3.9.5\bin;D:\Program Files\MySQL\MySQL Server 8.0\bin;C:\Users\calos\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\calos\AppData\Local\Programs\Python\Python311\;C:\Users\calos\AppData\Local\Microsoft\WindowsApps;C:\Users\calos\AppData\Roaming\npm;C:\Users\calos\AppData\Local\Programs\Microsoft VS Code\bin\trace.log (文件名、目录名或卷标语法不正确。)
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[ERROR_FILE] - Failed to create parent directories for [C:\Program Files (x86)\Common Files\Oracle\Java\javapath;D:\Program Files (x86)\VMware\VMware Workstation\bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\gsudo\Current;C:\Program Files\nodejs\;D:\Program Files\Java\jdk-1.8\bin;D:\Program Files\Java\jdk-1.8\jre\bin;D:\Program Files\Git\cmd;D:\Program Files\Tencent\微信web开发者工具\dll;D:\Program Files\apache-maven-3.9.5\bin;D:\Program Files\MySQL\MySQL Server 8.0\bin;C:\Users\calos\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\calos\AppData\Local\Programs\Python\Python311\;C:\Users\calos\AppData\Local\Microsoft\WindowsApps;C:\Users\calos\AppData\Roaming\npm;C:\Users\calos\AppData\Local\Programs\Microsoft VS Code\bin\error.log]
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[ERROR_FILE] - openFile(C:\Program Files (x86)\Common Files\Oracle\Java\javapath;D:\Program Files (x86)\VMware\VMware Workstation\bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\gsudo\Current;C:\Program Files\nodejs\;D:\Program Files\Java\jdk-1.8\bin;D:\Program Files\Java\jdk-1.8\jre\bin;D:\Program Files\Git\cmd;D:\Program Files\Tencent\微信web开发者工具\dll;D:\Program Files\apache-maven-3.9.5\bin;D:\Program Files\MySQL\MySQL Server 8.0\bin;C:\Users\calos\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\calos\AppData\Local\Programs\Python\Python311\;C:\Users\calos\AppData\Local\Microsoft\WindowsApps;C:\Users\calos\AppData\Roaming\npm;C:\Users\calos\AppData\Local\Programs\Microsoft VS Code\bin/error.log,true) call failed. java.io.FileNotFoundException: C:\Program Files (x86)\Common Files\Oracle\Java\javapath;D:\Program Files (x86)\VMware\VMware Workstation\bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\gsudo\Current;C:\Program Files\nodejs\;D:\Program Files\Java\jdk-1.8\bin;D:\Program Files\Java\jdk-1.8\jre\bin;D:\Program Files\Git\cmd;D:\Program Files\Tencent\微信web开发者工具\dll;D:\Program Files\apache-maven-3.9.5\bin;D:\Program Files\MySQL\MySQL Server 8.0\bin;C:\Users\calos\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\calos\AppData\Local\Programs\Python\Python311\;C:\Users\calos\AppData\Local\Microsoft\WindowsApps;C:\Users\calos\AppData\Roaming\npm;C:\Users\calos\AppData\Local\Programs\Microsoft VS Code\bin\error.log (文件名、目录名或卷标语法不正确。)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:175)
at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:80)
at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:131)
at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:312)
at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:281)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:216)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:203)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:196)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:170)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:148)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:82)
at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:63)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:62)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:362)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298)
at com.xiaodou.WikiApplication.main(WikiApplication.java:13)
logback的简化配置文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 修改一下路径-->
<appender class="ch.qos.logback.core.ConsoleAppender" name="STDOUT">
<encoder>
<!-- <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-5level) %blue(%-50logger{50}:%-4line) %thread %green(%-18X{LOG_ID}) %msg%n</Pattern>-->
<Pattern>%d{ss.SSS} %highlight(%-5level) %blue(%-30logger{30}:%-4line) %thread %green(%-18X{LOG_ID}) %msg%n
</Pattern>
</encoder>
</appender>
<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="TRACE_FILE">
<file>${PATH}/trace.log</file>
<!-- ... 其他配置 ... -->
</appender>
<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="ERROR_FILE">
<file>${PATH}/error.log</file>
<!-- ... 其他配置 ... -->
</appender>
<property name="PATH" value=".\log"/>
<root level="ERROR">
<appender-ref ref="ERROR_FILE"/>
</root>
<root level="TRACE">
<appender-ref ref="TRACE_FILE"/>
</root>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
解决方案
错误信息表明Logback在尝试创建日志文件时遇到了问题。具体来说,Logback试图在PATH
变量指定的路径下创建日志文件,但是失败了。错误信息中的路径看起来像是环境变量的一部分,而不是一个有效的文件路径。这可能是因为${PATH}
变量没有被正确解析。
在Windows系统中,PATH
是一个环境变量,它包含了系统查找可执行文件的目录列表。在你的Logback配置中,你试图使用${PATH}
作为日志文件的路径,这可能导致了混淆。你应该避免使用与环境变量同名的变量名。
为了解决这个问题,你可以做以下更改:
- 更改变量名:将配置文件中的
${PATH}
变量名更改为其他名称,例如${LOG_PATH}
。 - 更新文件路径:确保你的日志文件路径是一个有效的目录路径。例如,你可以将日志文件放在项目目录下的
logs
文件夹中。
修改后的配置文件可能如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_PATH" value="./logs"/>
<!-- ... 其他配置 ... -->
<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="TRACE_FILE">
<file>${LOG_PATH}/trace.log</file>
<!-- ... 其他配置 ... -->
</appender>
<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="ERROR_FILE">
<file>${LOG_PATH}/error.log</file>
<!-- ... 其他配置 ... -->
</appender>
<!-- ... 其他配置 ... -->
</configuration>
确保你的项目目录下有一个名为logs
的文件夹,或者根据你的实际项目结构调整路径。这样,Logback应该能够在正确的位置创建日志文件,而不会遇到路径问题。