可运行 JAR 未检测到 logback.xml 配置
Runnable JAR not detecting logback.xml configuration
我有一个 Java Maven 应用程序,我在其中创建了一个 swing GUI。
我正在使用 logback-classic 进行日志记录。 logback.xml 位于 src/main/resources 目录下。
当我 运行 使用 eclipse "Run As Java Application" 的应用程序时,它按预期工作并且应用程序正在记录到控制台和日志文件。
但是在将应用程序导出为 运行 可用的 JAR 文件并双击 运行 应用程序后,它没有记录到文件。
当我打开命令提示符并使用 "java -jar myjarfile.jar" 执行应用程序时,我能够看到控制台日志,但它使用的是默认的 logback 配置。
我尝试提供额外的参数,-Dlogback.configurationFile="path-to-file/logback.xml",但出现以下错误,
Failed to instantiate [ch.qos.logback.classic.LoggerContext] Reported
exception: java.lang.IllegalArgumentException: name
at sun.misc.URLClassPath$Loader.findResource(Unknown Source)
at sun.misc.URLClassPath.next(Unknown Source)
at sun.misc.URLClassPath.hasMoreElements(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.next(Unknown Source)
at java.net.URLClassLoader.hasMoreElements(Unknown Source)
at sun.misc.CompoundEnumeration.next(Unknown Source)
at sun.misc.CompoundEnumeration.hasMoreElements(Unknown Source)
at ch.qos.logback.core.util.Loader.getResources(Loader.java:73)
at ch.qos.logback.classic.util.ContextInitializer.multiplicityWarning(ContextInitializer.java:183)
at ch.qos.logback.classic.util.ContextInitializer.statusOnResourceSearch(ContextInitializer.java:175)
at ch.qos.logback.classic.util.ContextInitializer.findConfigFileURLFromSystemProperties(ContextInitializer.java:111)
at ch.qos.logback.classic.util.ContextInitializer.findURLOfDefaultConfigurationFile(ContextInitializer.java:120)
at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:148)
at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84)
at org.slf4j.impl.StaticLoggerBinder.(StaticLoggerBinder.java:55)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
at com.esignature.tools.templatemigration.ConverterGUI.(ConverterGUI.java:35)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:59)
我不知道我错过了什么,我想从 运行nable JAR 记录到日志文件。
这很令人困惑,因为它是在 eclipse 中工作,而不是在导出的 运行nable JAR 中工作。
下面是 logback.xml 文件。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_HOME" value="C:/template converter logs" />
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n</pattern>
</encoder>
</appender>
<appender name="rollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/applog-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="rollingFile" />
</root>
</configuration>
您遇到的错误是因为有两个 logback.xml 配置,一个在您的类路径中,您正在尝试 override/add 另一个使用 -Dlogback.configurationFile
在打包可运行的 JAR 时,我将 pom 配置更改为此。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
注意可执行文件设置为 true,这确保将 MANIFEST.MF 文件添加到 JAR 包中。
因此,为了使其与此配置一起工作,我将此 属性 添加到我的应用程序中。yml/application.properties
logging:
config: file:D:\LogConfig\logback.xml
然后我继续使用 clean、install mvn 命令生成 JAR 文件。
从那里 运行 使用 java -jar myJar.jar
的 JAR 文件工作正常,不需要 添加参数来搜索 application.yml 或logback.xml。这是因为属性是从 jar 中加载的,而日志配置是从 application.yml 上的指定路由加载的。
使用 maven shade plugin 生成包含依赖项和自定义项的着色 JAR 文件 logback.xml 是我使用的一种有效解决方案。
现在,当我 运行 这个 JAR 文件(从命令行或通过双击它)时,日志会按预期在指定位置生成。
我有一个 Java Maven 应用程序,我在其中创建了一个 swing GUI。
我正在使用 logback-classic 进行日志记录。 logback.xml 位于 src/main/resources 目录下。
当我 运行 使用 eclipse "Run As Java Application" 的应用程序时,它按预期工作并且应用程序正在记录到控制台和日志文件。
但是在将应用程序导出为 运行 可用的 JAR 文件并双击 运行 应用程序后,它没有记录到文件。
当我打开命令提示符并使用 "java -jar myjarfile.jar" 执行应用程序时,我能够看到控制台日志,但它使用的是默认的 logback 配置。
我尝试提供额外的参数,-Dlogback.configurationFile="path-to-file/logback.xml",但出现以下错误,
Failed to instantiate [ch.qos.logback.classic.LoggerContext] Reported exception: java.lang.IllegalArgumentException: name at sun.misc.URLClassPath$Loader.findResource(Unknown Source) at sun.misc.URLClassPath.next(Unknown Source) at sun.misc.URLClassPath.hasMoreElements(Unknown Source) at java.net.URLClassLoader.run(Unknown Source) at java.net.URLClassLoader.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.next(Unknown Source) at java.net.URLClassLoader.hasMoreElements(Unknown Source) at sun.misc.CompoundEnumeration.next(Unknown Source) at sun.misc.CompoundEnumeration.hasMoreElements(Unknown Source) at ch.qos.logback.core.util.Loader.getResources(Loader.java:73) at ch.qos.logback.classic.util.ContextInitializer.multiplicityWarning(ContextInitializer.java:183) at ch.qos.logback.classic.util.ContextInitializer.statusOnResourceSearch(ContextInitializer.java:175) at ch.qos.logback.classic.util.ContextInitializer.findConfigFileURLFromSystemProperties(ContextInitializer.java:111) at ch.qos.logback.classic.util.ContextInitializer.findURLOfDefaultConfigurationFile(ContextInitializer.java:120) at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:148) at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84) at org.slf4j.impl.StaticLoggerBinder.(StaticLoggerBinder.java:55) at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150) at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124) at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383) at com.esignature.tools.templatemigration.ConverterGUI.(ConverterGUI.java:35) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:59)
我不知道我错过了什么,我想从 运行nable JAR 记录到日志文件。
这很令人困惑,因为它是在 eclipse 中工作,而不是在导出的 运行nable JAR 中工作。
下面是 logback.xml 文件。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_HOME" value="C:/template converter logs" />
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n</pattern>
</encoder>
</appender>
<appender name="rollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/applog-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="rollingFile" />
</root>
</configuration>
您遇到的错误是因为有两个 logback.xml 配置,一个在您的类路径中,您正在尝试 override/add 另一个使用 -Dlogback.configurationFile
在打包可运行的 JAR 时,我将 pom 配置更改为此。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
注意可执行文件设置为 true,这确保将 MANIFEST.MF 文件添加到 JAR 包中。
因此,为了使其与此配置一起工作,我将此 属性 添加到我的应用程序中。yml/application.properties
logging:
config: file:D:\LogConfig\logback.xml
然后我继续使用 clean、install mvn 命令生成 JAR 文件。
从那里 运行 使用 java -jar myJar.jar
的 JAR 文件工作正常,不需要 添加参数来搜索 application.yml 或logback.xml。这是因为属性是从 jar 中加载的,而日志配置是从 application.yml 上的指定路由加载的。
使用 maven shade plugin 生成包含依赖项和自定义项的着色 JAR 文件 logback.xml 是我使用的一种有效解决方案。
现在,当我 运行 这个 JAR 文件(从命令行或通过双击它)时,日志会按预期在指定位置生成。