打印到控制台有效,但使用 logback 打印到文件无效

Printing to console works but printing to file is not working using logback

下面是 logback.xml 文件:

尝试使用 log4、logback 访问而不是 logback classic 但是,我无法将其打印到文件中。我试图在一周内解决这个问题,但没有成功

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <!-- Tests run on modern PCs show that buffering related property -->
    <!-- "ImmediateFlush" has negligible impact and will be ignored.  -->
    <Append>true</Append>
    <File>log.txt</File>
    <encoder>
      <pattern>%d{dd MMM yyyy HH:mm:ss,SSS} %c %p %t %m%n%n</pattern>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>debug</level>
    </filter>
  </appender>
  <appender name="theConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%-4r [%t] %-5p %c %x - %m%n</pattern>
    </encoder>
  </appender>
  <logger name="com.wataniya.Wataniya_WebServiceSkeleton" level="DEBUG">
    <appender-ref ref="FILE"/>
  </logger>
  <root level="DEBUG">
    <appender-ref ref="FILE"/>
    <appender-ref ref="theConsoleAppender"/>
  </root>
</configuration>

您确定您的 logback.xml 文件位于正确的位置并且正在被检测到吗?
尝试故意在配置文件中输入错误,并查看您是否在控制台中收到消息(Logback 会自动执行此操作)。

如果没有,您可以查看 Logback configuration 如何通过或 运行 您的申请,如下所示。

java -Dlogback.configurationFile=/path/to/logback.xml YourMainClass

Let us begin by discussing the initialization steps that logback follows to try to configure itself:

  1. Logback tries to find a file called logback.groovy in the classpath.
  2. If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
  3. If no such file is found, it checks for the file logback.xml in the classpath.