Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logback logging with maven-jetty-plugin

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>
like image 400
Jihed Amine Avatar asked Nov 10 '11 23:11

Jihed Amine


2 Answers

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>
like image 192
jgeluk Avatar answered Sep 24 '22 14:09

jgeluk


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>
like image 37
Kkkev Avatar answered Sep 23 '22 14:09

Kkkev