使用 maven-jetty-plugin 进行 logback 日志记录
我想通过 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它与 Jetty 9 和 jetty-maven-plugin 一起使用:
It works with Jetty 9 and the jetty-maven-plugin:
使用旧的
maven-jetty-plugin
而不是jetty-maven-plugin
对我有用:Using the older
maven-jetty-plugin
rather than thejetty-maven-plugin
works for me:您可以使用 Properties-Maven-Plugin:
文档: http://mojo.codehaus .org/properties-maven-plugin/usage.html
它并不完美,但应该有效。
you can use the Properties-Maven-Plugin:
Documentation: http://mojo.codehaus.org/properties-maven-plugin/usage.html
It's not perfect, but it should work.
我也遇到过同样的问题。作为解决方法,我使用 slf4j-simple 代替 logback。 slf4j-simple 的默认配置设置为 INFO 级别,但在其他方面不是很可配置,因此它可能会也可能不会满足您的需求。在插件配置中,将
logback-classic
替换为: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: