Mở rộng câu trả lời Gili của một chút; sử dụng thuộc tính-maven-plugin là một cách thuận tiện để thiết lập các thuộc tính hệ thống thay vì phải chỉ định chúng trên dòng lệnh. Tôi cung cấp các ví dụ cho cả logback và log4j. Thêm khối plugin này vào tệp pom.xml của bạn ngoài cấu hình plugin jetty-maven trong câu trả lời của Gili.
Logback:
<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>
<!-- makes jetty log the exception if it fails to initialize slf4j -->
<property>
<name>org.eclipse.jetty.util.log.IGNORED</name>
<value>true</value>
</property>
<!-- Location of logback config -->
<property>
<name>logback.configurationFile</name>
<value>/path/to/logback.xml</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>
Log4j:
<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>
<!-- makes jetty log the exception if it fails to initialize slf4j -->
<property>
<name>org.eclipse.jetty.util.log.IGNORED</name>
<value>true</value>
</property>
<!-- this tells where the log4j configuration is -->
<property>
<name>log4j.configuration</name>
<value>file:./src/main/resources/log4j.properties</value>
</property>
<!-- this can be uncommented to debug startup log4j itself,
e.g. how it locates log4j.properties etc -->
<!--
<property>
<name>log4j.debug</name>
<value></value>
</property>
-->
</properties>
</configuration>
</execution>
</executions>
</plugin>
Cũng cho log4j, tự nhiên sử dụng phụ thuộc sau cho cầu cảng-maven-plugin thay vì logback-cổ điển:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
...
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
</plugin>
Nguồn
2013-09-15 19:05:18
Lưu ý, để định cấu hình ghi nhật ký chỉ cần cho maven biết nơi tập tin cấu hình của bạn là: mvn verify -Dlogback.configurationFile =/path/to/logback.xml – Mike
Có bản cập nhật cho phiên bản 9 không? Tôi không thấy bất kỳ nhật ký nào từ Jetty khi tôi sử dụng cấu hình này. – user64141
@ user64141 Tôi không biết bất kỳ thay đổi nào. Điều đó nói rằng, tôi không còn sử dụng plugin mứt Jetty nữa. Bây giờ tôi gọi Jetty từ một lớp Java bình thường. – Gili