I have a web app running on Jboss EAP 7.0.2
. In my app I want to use Log4j2
for logging. So, I added log4j2
and log4j-slf4j-impl
as dependencies:
build.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'
Put file 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>
Created log4j2.xml (I omit its content, but it writes the log to the file lea.log).
Also, I have 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");
}
}
This test method writes to 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
but it doesn't log the exception!
In server.log I see:
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
...
Questions:
You're throwing the exception from the REST endpoint and not handling it. Therefore the server handles it for you which cannot log to your applications log configuration.
Excluding the logging subsystem will stop your deployment from being processed by the logging subsystem. (Which does mean you don't need those module exclusions). However the server itself will still use the configuration from the logging subsystem.
If you want to catch exceptions of a specific type you could use a javax.ws.rs.ext.ExceptionMapper
to log the exception then create a response for it.