我想使用maven-jetty-plugin进行logback日志记录。显然,系统属性logback.configurationFile在 maven-jetty-plugin启动后读取并初始化了slf4j,因此jetty不读取./src/test/resources/logback.xml文件。 因此,我将所有日志消息设置为调试级别并打印到控制台(默认的logback配置)。用-Dlogback.configurationFile = ...启动maven解决了这个问题。但是,我更喜欢使用log4j和maven-jetty-plugin在pom中设置属性。有什么想法吗?
这是我的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>
答案 0 :(得分: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>
答案 1 :(得分:3)
使用较旧的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>
答案 2 :(得分: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
它并不完美,但应该有效。
答案 3 :(得分:2)
我遇到过同样的问题。作为一种解决方法,我使用slf4j-simple代替logback。 slf4j-simple的默认配置设置为INFO级别,但不是非常可配置,因此它可能满足您的需求,也可能不满足您的需求。在插件配置中,将logback-classic
替换为:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>