logback logging with maven-jetty-plugin

て烟熏妆下的殇ゞ 提交于 2019-12-05 21:50:05

问题


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>

回答1:


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>



回答2:


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>



回答3:


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.




回答4:


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>


来源:https://stackoverflow.com/questions/8087794/logback-logging-with-maven-jetty-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!