使用 maven-jetty-plugin 进行 logback 日志记录

发布于 2024-12-15 04:18:41 字数 2051 浏览 3 评论 0原文

我想通过 maven-jetty-plugin 使用 logback 日志记录。显然,系统属性 logback.configurationFile 在 maven-jetty-plugin 启动并初始化 slf4j 之后被读取,因此文件 ./src/test/resources/logback.xml 不会被 jetty 读取。 结果,我将所有日志消息设置为调试级别并打印到控制台(默认 logback 配置)。使用 -Dlogback.configurationFile=... 启动 maven 可以解决该问题。但是,我更喜欢在 pom 中设置属性,因为可以使用 log4j 和 maven-jetty-plugin 来实现。有什么想法吗?

这是我的 pom.xml:

...
 <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.0.4.v20111024</version>
    <dependencies>
       <dependency>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
          <version>1.0.0</version>
       </dependency>
       <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.6.1</version>
        </dependency>
        </dependencies>
        <configuration>
          <systemProperties>
            <systemProperty>
               <name>logback.configurationFile</name>
               <value>./src/test/resources/logback.xml</value>
            </systemProperty>
           </systemProperties>
...

这是 logback.xml:

<configuration>

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>

  </appender>

  <root level="INFO">
    <appender-ref ref="FILE" />
  </root>

</configuration>

I want to use logback logging with maven-jetty-plugin. Apparently, the system property logback.configurationFile is read after maven-jetty-plugin is started and has initialized slf4j, so the file ./src/test/resources/logback.xml isn't read by jetty.
As a result, I get all log messages set to debug level and printed to console (a default logback configuration). Launching maven with -Dlogback.configurationFile=... resolves the problem. However, I'd prefer setting the property in the pom as it is possible with log4j and maven-jetty-plugin. Any ideas ?

Here is my pom.xml:

...
 <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.0.4.v20111024</version>
    <dependencies>
       <dependency>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
          <version>1.0.0</version>
       </dependency>
       <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.6.1</version>
        </dependency>
        </dependencies>
        <configuration>
          <systemProperties>
            <systemProperty>
               <name>logback.configurationFile</name>
               <value>./src/test/resources/logback.xml</value>
            </systemProperty>
           </systemProperties>
...

And here is logback.xml:

<configuration>

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>

  </appender>

  <root level="INFO">
    <appender-ref ref="FILE" />
  </root>

</configuration>

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌若浮生 2024-12-22 04:18:41

它与 Jetty 9 和 jetty-maven-plugin 一起使用:

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>${jetty.version}</version>
  <dependencies>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>${logback.version}</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-access</artifactId>
      <version>${logback.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
  </dependencies>
  <configuration>
    <scanIntervalSeconds>10</scanIntervalSeconds>
    <webAppSourceDirectory>src/main/resources/htdocs</webAppSourceDirectory>
    <webApp>
      <descriptor>src/main/webapp/WEB-INF/web.xml</descriptor>
    </webApp>
    <systemProperties>
      <systemProperty>
        <name>org.eclipse.jetty.util.log.Log</name>
        <value>org.eclipse.jetty.util.log.Slf4jLog</value>
      </systemProperty>
      <systemProperty>
        <name>logback.configurationFile</name>
        <value>src/main/resources/logback.xml</value>
      </systemProperty>
    </systemProperties>
  </configuration>
</plugin>

It works with Jetty 9 and the jetty-maven-plugin:

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>${jetty.version}</version>
  <dependencies>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>${logback.version}</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-access</artifactId>
      <version>${logback.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
  </dependencies>
  <configuration>
    <scanIntervalSeconds>10</scanIntervalSeconds>
    <webAppSourceDirectory>src/main/resources/htdocs</webAppSourceDirectory>
    <webApp>
      <descriptor>src/main/webapp/WEB-INF/web.xml</descriptor>
    </webApp>
    <systemProperties>
      <systemProperty>
        <name>org.eclipse.jetty.util.log.Log</name>
        <value>org.eclipse.jetty.util.log.Slf4jLog</value>
      </systemProperty>
      <systemProperty>
        <name>logback.configurationFile</name>
        <value>src/main/resources/logback.xml</value>
      </systemProperty>
    </systemProperties>
  </configuration>
</plugin>
迷鸟归林 2024-12-22 04:18:41

使用旧的 maven-jetty-plugin 而不是 jetty-maven-plugin 对我有用:

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <version>6.1.26</version>
  <configuration>
    <systemProperties>
      <systemProperty>
        <name>logback.configurationFile</name>
        <value>${project.build.outputDirectory}/jetty-logback.xml</value>
      </systemProperty>
    </systemProperties>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.6.4</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.0.0</version>
    </dependency>
  </dependencies>
</plugin>

Using the older maven-jetty-plugin rather than the jetty-maven-plugin works for me:

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <version>6.1.26</version>
  <configuration>
    <systemProperties>
      <systemProperty>
        <name>logback.configurationFile</name>
        <value>${project.build.outputDirectory}/jetty-logback.xml</value>
      </systemProperty>
    </systemProperties>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.6.4</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.0.0</version>
    </dependency>
  </dependencies>
</plugin>
抠脚大汉 2024-12-22 04:18:41

您可以使用 Properties-Maven-Plugin:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
          <execution>
            <goals>
              <goal>set-system-properties</goal>
            </goals>
            <configuration>
              <properties>
                <property>
                  <name>logback.configurationFile</name>
                  <value>src/test/resources/logback.xml</value>
                </property>
              </properties>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

文档: http://mojo.codehaus .org/properties-maven-plugin/usage.html
它并不完美,但应该有效。

you can use the Properties-Maven-Plugin:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
          <execution>
            <goals>
              <goal>set-system-properties</goal>
            </goals>
            <configuration>
              <properties>
                <property>
                  <name>logback.configurationFile</name>
                  <value>src/test/resources/logback.xml</value>
                </property>
              </properties>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Documentation: http://mojo.codehaus.org/properties-maven-plugin/usage.html
It's not perfect, but it should work.

像你 2024-12-22 04:18:41

我也遇到过同样的问题。作为解决方法,我使用 slf4j-simple 代替 logback。 slf4j-simple 的默认配置设置为 INFO 级别,但在其他方面不是很可配置,因此它可能会也可能不会满足您的需求。在插件配置中,将 logback-classic 替换为:

      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.6.4</version>
      </dependency>

I've encountered this same problem. As a workaround, I used slf4j-simple in place of logback. The slf4j-simple has a default configuration set to INFO level, but is not very configurable otherwise, so it may or may not meet your needs. In the plugin configuration, replace logback-classic with:

      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.6.4</version>
      </dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文