我的 Jar 没有创建日志文件,而普通应用程序会创建

My Jar not creating a log file whereas normal application does

我开发了一个将 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 中 运行 它时,它 运行s 并创建一个日志文件并将日志写入其中。但是现在我创建了一个名为 alerttesting.jar. 的项目的可执行 Jar 文件当我 运行 它使用 java -jar alerttesting.jar 时,它 运行s 成功但没有在 /tmp/。我做错了什么吗?

logback.xml 应该在您的类路径中或使用“-Dlogback.configurationFile=/path/to/config.xml”

除此之外,请确保您是通过 maven 而不是 eclipse 打包它。