日志未在logback- Java Web应用中打印

发布于 2025-01-22 04:52:17 字数 2342 浏览 0 评论 0原文

我有一个在Tomcat 9上运行的Java Web应用程序,我正在Unix机器上测试它。 我正在从log4j迁移到logback,但是当我在tomcat上运行应用程序时,我看不到附加在适当文件中的日志。 maven依赖项

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.3.0-alpha13</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.3.0-alpha13</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.32</version>
    </dependency>

这些是我的logback.xml的

<configuration scan="true" scanPeriod="10 seconds">
   <property name="LOG_FILE" value="myLogFile" />
   <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logDir}/${LOG_FILE}.log</file>
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
     <!-- daily rollover -->
     <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.gz</fileNamePattern>
     <!-- keep 30 days' worth of history capped at 3GB total size -->
     <maxHistory>30</maxHistory>
     <totalSizeCap>3GB</totalSizeCap>
  </rollingPolicy>
  <encoder>
     <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
  </encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
     <pattern>%-5level %logger{36} - %msg%n</pattern>
  </encoder>
  <!-- Only log level INFO and above -->
  <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
     <level>WARN</level>
  </filter>
</appender>
<root level="INFO">
  <appender-ref ref="FILE" />
  <appender-ref ref="STDOUT" />
</root>
</configuration>

,在我的tomcat sh脚本文件中,我有此配置,

JAVA_OPTS="$JAVA_OPTS -DlogDir=/home/logs -Dlogback.configurationFile=/home/app/WEB-INF/logback.xml"

我也尝试使用-dlogback.configuration,我得到了相同的结果,应用程序启动了,但未在日志中打印出来日志档案

I have a Java web app which runs on Tomcat 9, I am testing it on Unix machine.
I am migrating from log4j to logback, but when I run app on Tomcat, I can't see logs appended in proper file.
Those are Maven dependencies

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.3.0-alpha13</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.3.0-alpha13</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.32</version>
    </dependency>

That is my logback.xml

<configuration scan="true" scanPeriod="10 seconds">
   <property name="LOG_FILE" value="myLogFile" />
   <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logDir}/${LOG_FILE}.log</file>
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
     <!-- daily rollover -->
     <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.gz</fileNamePattern>
     <!-- keep 30 days' worth of history capped at 3GB total size -->
     <maxHistory>30</maxHistory>
     <totalSizeCap>3GB</totalSizeCap>
  </rollingPolicy>
  <encoder>
     <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
  </encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
     <pattern>%-5level %logger{36} - %msg%n</pattern>
  </encoder>
  <!-- Only log level INFO and above -->
  <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
     <level>WARN</level>
  </filter>
</appender>
<root level="INFO">
  <appender-ref ref="FILE" />
  <appender-ref ref="STDOUT" />
</root>
</configuration>

And in my Tomcat sh script file, I have this configuration

JAVA_OPTS="$JAVA_OPTS -DlogDir=/home/logs -Dlogback.configurationFile=/home/app/WEB-INF/logback.xml"

I also tried to use -Dlogback.configuration, and I get same result, Application is started properly, but logs are not printed in the log file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文