Runnable JAR未检测到logback.xml配置。

huangapple go评论75阅读模式
英文:

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"”,但我收到以下错误:

无法实例化[ch.qos.logback.classic.LoggerContext],报告的异常:java.lang.IllegalArgumentException: name
at sun.misc.URLClassPath$Loader.findResource(Unknown Source)
at sun.misc.URLClassPath$1.next(Unknown Source)
...

我不知道我错过了什么,我希望能够从可运行的JAR文件中记录日志到日志文件中。

这很令人困惑,因为在Eclipse中可以正常工作,但在导出的可运行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>

如果您有关于这个问题的更多详细信息,欢迎与我分享。

英文:

I have a Java maven application in which I have created a swing GUI.

I am using logback-classic for logging purposes. logback.xml is present under the src/main/resources directory.

When I run the application using eclipse "Run As Java Application", it is working as intended and application is logging to the console and log file.

But after exporting the application as a runnable JAR file and double clicking to run the application, it is not logging to file.

When I opened command prompt and execute application using "java -jar myjarfile.jar", I am able to see the console logs, but it is using the default logback configuration.

I tried giving additional parameter, -Dlogback.configurationFile="path-to-file/logback.xml" and I am getting the below error,

> 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$1.next(Unknown Source)
> at sun.misc.URLClassPath$1.hasMoreElements(Unknown Source)
> at java.net.URLClassLoader$3$1.run(Unknown Source)
> at java.net.URLClassLoader$3$1.run(Unknown Source)
> at java.security.AccessController.doPrivileged(Native Method)
> at java.net.URLClassLoader$3.next(Unknown Source)
> at java.net.URLClassLoader$3.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.<clinit>(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.<clinit>(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)

I don't know what I am missing and I want to log from runnable JAR to the log file.

It is confusing because it is working from eclipse but not from the exported runnable JAR.

Below is the logback.xml file.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration&gt;
	&lt;property name=&quot;LOG_HOME&quot; value=&quot;C:/template converter logs&quot; /&gt;
	&lt;appender name=&quot;STDOUT&quot;
		class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
		&lt;encoder&gt;
			&lt;pattern&gt;%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n&lt;/pattern&gt;
		&lt;/encoder&gt;
	&lt;/appender&gt;
	&lt;appender name=&quot;rollingFile&quot;
		class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;
		&lt;rollingPolicy
			class=&quot;ch.qos.logback.core.rolling.TimeBasedRollingPolicy&quot;&gt;
			&lt;fileNamePattern&gt;${LOG_HOME}/applog-%d{yyyy-MM-dd}.log&lt;/fileNamePattern&gt;						
		&lt;/rollingPolicy&gt;
		&lt;encoder&gt;
			&lt;pattern&gt;%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n&lt;/pattern&gt;
		&lt;/encoder&gt;
	&lt;/appender&gt;    	
	&lt;root level=&quot;debug&quot;&gt;
		&lt;appender-ref ref=&quot;STDOUT&quot; /&gt;
		&lt;appender-ref ref=&quot;rollingFile&quot; /&gt;
	&lt;/root&gt;
&lt;/configuration&gt;

答案1

得分: 1

你所遇到的错误是因为存在两个logback.xml配置文件,一个位于你的类路径中,而你正在尝试使用-Dlogback.configurationFile参数来覆盖/添加另一个配置文件。

在我的情况下,在打包可运行的JAR文件时,我将pom配置更改为以下内容:

<plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
   <configuration>
      <executable>true</executable>
   </configuration>
</plugin>

注意executable设置为true,这确保向JAR包中添加了MANIFEST.MF文件。

因此,为了使这个配置生效,我在我的application.yml/application.properties文件中添加了以下属性:

logging:
  config: file:D:\\LogConfig\\logback.xml

然后,我使用cleaninstallmvn命令生成JAR文件。

从那里开始,使用java -jar myJar.jar命令运行JAR文件就能正常工作,无需添加参数来搜索application.ymllogback.xml文件。这是因为属性从JAR内部加载,并且日志配置从application.yml中指定的路径加载。

英文:

The error you are facing is because there is two logback.xml configuration, one is in your classpath and you are trying to override/add another one using -Dlogback.configurationFile

In my case when packaging the runnable JAR I changed the pom configuration to this.

&lt;plugin&gt;
   &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
   &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
   &lt;configuration&gt;
      &lt;executable&gt;true&lt;/executable&gt;
   &lt;/configuration&gt;
&lt;/plugin&gt;

Note the executable set to true, this makes sure that a MANIFEST.MF file is added to the JAR package.

So in order to make this work with this config I added this property to my application.yml/application.properties

logging:
  config: file:D:\\LogConfig\\logback.xml

Then I proceed to generate the JAR file using clean, install mvn commands.

From there running the JAR file using java -jar myJar.jar works fine and there is no need to add arguments to search the application.yml or logback.xml. That is because the properties is being loaded from within the jar and the log config from the specified route on the application.yml .

答案2

得分: 0

使用 maven shade plugin 生成一个包含依赖和自定义 logback.xml 的 shaded JAR 文件是我采用的一个可行解决方案。

现在,当我运行这个 JAR 文件(无论是从命令行还是双击运行),日志会如预期般在指定位置生成。

英文:

Using maven shade plugin to generate a shaded JAR file that includes the dependencies and the custom logback.xml is one working solution that I utilized.

Now when I run this JAR file (either from command line or by double-clicking it), logs are generated in the specified location as expected.

huangapple
  • 本文由 发表于 2020年4月11日 03:00:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/61146929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定