英文:
Log4j2 isn´t working in JAX-RS servlet project on JBoss 7.4
问题
I am working on a JAX-RS servlet project, which is deployed on JBoss (7.4) server, I want to implement Log4j2 (2.20) for application log handling, but when I use the info
method, it does not show anything in the console or in the server.log
file of the server.
I added the Log4j2 dependencies to lib
folder on my project.
log4j-api-2.20.0.jar
log4j-core-2.20.0.jar
This is the configuration for Log4j2 in src\main\resources\log4j2.xml
.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="LogToConsole" target="SYSTEM_OUT">
<PatternLayout pattern="%m%n" />
</Console>
<File name="LogToFile" fileName="${sys:jboss.server.log.dir}/server.log" append="true">
<PatternLayout>
<Pattern>%m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="com.sematext.blog" level="info" additivity="true">
<AppenderRef ref="LogToConsole"/>
</Logger>
<Root level="info">
<AppenderRef ref="LogToFile"/>
</Root>
</Loggers>
</Configuration>
I excluded the JBoss logging subsystem in the src\main\webapp\WEB-INF\jboss-deployment-structure.xml
file:
<jboss-deployment-structure>
<deployment>
<exclude-subsystems>
<subsystem name="logging" />
</exclude-subsystems>
</deployment>
</jboss-deployment-structure>
This is the class where I'm using it.
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
public class ExampleServiceImpl implements ExampleService {
private static final Logger LOGGER = LogManager.getLogger(ExampleServiceImpl.class);
@GET
@Path("/getListData")
@Produces(MediaType.APPLICATION_JSON)
@Override
public ExampleResp getListData() {
LOGGER.info("Inside of getListData method");
ExampleDAOImpl exampleDAOImpl = new ExampleDAOImpl();
ExampleResp resp = new ExampleResp();
resp.setListData(exampleDAOImpl.getData());
return resp;
}
}
英文:
I am working on a JAX-RS servlet project, which is deployed on JBoss (7.4) server, I want to implement Log4j2 (2.20) for application log handling, but when I use the info
method, it does not show anything in the console or in the server.log
file of the server.
I added the Log4j2 dependencies to lib
folder on my project.
log4j-api-2.20.0.jar
log4j-core-2.20.0.jar
This is the configuration for Log4j2 in src\main\resources\log4j2.xml
.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="LogToConsole" target="SYSTEM_OUT">
<PatternLayout pattern="%m%n" />
</Console>
<File name="LogToFile" fileName="${sys:jboss.server.log.dir}/server.log" append="true">
<PatternLayout>
<Pattern>%m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="com.sematext.blog" level="info" additivity="true">
<AppenderRef ref="LogToConsole"/>
</Logger>
<Root level="info">
<AppenderRef ref="LogToFile"/>
</Root>
</Loggers>
</Configuration>
I excluded the JBoss logging subsystem in the src\main\webapp\WEB-INF\jboss-deployment-structure.xml
file:
<jboss-deployment-structure>
<deployment>
<exclude-subsystems>
<subsystem name="logging" />
</exclude-subsystems>
</deployment>
</jboss-deployment-structure>
This is the class where I'm using it.
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
public class ExampleServiceImpl implements ExampleService {
private static final Logger LOGGER = LogManager.getLogger(ExampleServiceImpl.class);
@GET
@Path("/getListData")
@Produces(MediaType.APPLICATION_JSON)
@Override
public ExampleResp getListData() {
LOGGER.info("Inside of getListData method");
ExampleDAOImpl exampleDAOImpl = new ExampleDAOImpl();
ExampleResp resp = new ExampleResp();
resp.setListData(exampleDAOImpl.getData());
return resp;
}
}
答案1
得分: 2
请注意,以下是您要求的翻译部分:
不要尝试使用不同的日志管理器,它会写入server.log
。两个日志管理器可能会覆盖文件,导致日志消息丢失或混乱。
有一个名为log4j2-jboss-logmanager的项目,它会将log4j2的日志消息写入JBoss日志管理器。该模块已经存在于JBoss EAP 7.4中。这意味着您应该只需要执行以下操作:
- 删除
jboss-deployment-structure.xml
,或至少删除模块排除项 - 在
org.apache.logging:log4j-api
Maven依赖项中添加<scope>provided</scope>
- 删除
org.apache.logging:log4j-core
这不支持使用配置文件配置log4j。它使用日志子系统中的配置。不过,根据您的配置,这应该是可以的。
英文:
You do not want to try to use a different log manager which writes to the server.log
. The two log managers could end up writing over the file which could result in missing or jumbled log messages.
There is log4j2-jboss-logmanager project which will write log4j2 log messages to the JBoss Log Manager. The module is already present in JBoss EAP 7.4. This means all you should need to do is:
- Remove the
jboss-deployment-structure.xml
or at least remove module exclusions - Add the
<scope>provided</scope>
to theorg.apache.logging:log4j-api
Maven dependency - Remove the
org.apache.logging:log4j-core
This does not support configuring log4j with a configuration file. It uses the configuration in the logging subsystem for configuration. However, given your configuration this should be fine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论