8

我想使用带有 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>
4

4 回答 4

6

它适用于 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>
于 2013-03-28T16:25:42.447 回答
4

使用旧的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>
于 2011-11-18T15:59:56.693 回答
3

您可以使用 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
它并不完美,但它应该可以工作。

于 2013-07-11T11:35:08.717 回答
2

我遇到过同样的问题。作为一种解决方法,我使用 slf4j-simple 代替 logback。slf4j-simple 有一个默认配置设置为 INFO 级别,但不是非常可配置,所以它可能会也可能不会满足您的需求。在插件配置中,替换logback-classic为:

      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.6.4</version>
      </dependency>
于 2012-07-04T15:00:15.313 回答