我有一个运行在Jboss EAP 7.0.2
. 在我的应用程序中,我想Log4j2
用于记录。所以,我添加了log4j2
和log4j-slf4j-impl
作为依赖项:
构建.gradle
compile 'org.apache.logging.log4j:log4j-core:2.7'
compile 'org.apache.logging.log4j:log4j-api:2.7'
compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.7'
放置文件jboss-deployment-structure.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclude-subsystems>
<subsystem name="logging" />
</exclude-subsystems>
<exclusions>
<module name="org.apache.commons.logging" />
<module name="org.apache.log4j" />
<module name="org.jboss.logging" />
<module name="org.jboss.logging.jul-to-slf4j-stub" />
<module name="org.jboss.logmanager" />
<module name="org.jboss.logmanager.log4j" />
<module name="org.slf4j" />
<module name="org.slf4j.impl" />
</exclusions>
</deployment>
</jboss-deployment-structure>
创建了 log4j2.xml(我省略了它的内容,但它将日志写入文件lea.log)。
另外,我有TestRestService
@Path("/test")
public class TestRestService {
private Logger logger = LogManager.getFormatterLogger(TestRestService.class);
private org.slf4j.Logger slf4jLogger = LoggerFactory.getLogger(TestRestService.class);
@GET
@Path("/logger")
public void testLogger() {
logger.info("Log4j2 log");
slf4jLogger.info("SLF4J log");
throw new RuntimeException("Test");
}
}
此测试方法写入lea.log
:
2016-11-07 15:25:37.673 [default task-7] INFO ru.rshb.test.TestRestService - Log4j2 log
2016-11-07 15:25:37.673 [default task-7] INFO ru.rshb.test.TestRestService - SLF4J log
但它不会记录异常!
在 server.log 我看到:
2016-11-07 15:25:37,673 INFO [stdout] (default task-7) INFO - Log4j2 log
2016-11-07 15:25:37,673 INFO [stdout] (default task-7) INFO - SLF4J log
2016-11-07 15:25:37,674 ERROR [io.undertow.request] (default task-7) UT005023: Exception handling request to /lea-core/rest/test/logger: org.jboss.resteasy.spi.UnhandledException: java.lang.RuntimeException: Test
...
问题:
- 如何将我的应用程序中发生的所有消息和异常定向到 lea.log(我的 log4j2 日志文件)?
- 即使我从部署中排除了日志记录子系统,它是否应该记录到 server.log 文件?