我开发了一个使用 logback 和 slf4j 的应用程序。这是我的 logback.xml :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/tmp/alerttest.log</file>
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<!-- Strictly speaking, the level attribute is not necessary since -->
<!-- the level of the root level is set to DEBUG by default. -->
<root level="DEBUG">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
因此它将日志写入 /tmp/alerttest.log
。当我在 Eclipse 中运行它时,它会运行并创建一个日志文件并将日志写入其中。但是现在我创建了一个名为 alerttesting.jar
的项目的可执行 Jar 文件。当我使用 java -jar Alerttesting.jar
运行它时,它运行成功但不创建/tmp/
中的文件。我是不是做错了什么?
最佳答案
logback.xml 应该位于您的类路径中或使用“-Dlogback.configurationFile=/path/to/config.xml”
除此之外,请确保您是通过 Maven 而不是 Eclipse 打包它。
关于java - 我的 Jar 没有创建日志文件,而普通应用程序却创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37590317/