Log4j2 在 JBoss 7.4 上的 JAX-RS servlet 项目中不起作用。

huangapple go评论73阅读模式
英文:

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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;Configuration status=&quot;WARN&quot;&gt;
	&lt;Appenders&gt;
		&lt;Console name=&quot;LogToConsole&quot; target=&quot;SYSTEM_OUT&quot;&gt;
			&lt;PatternLayout pattern=&quot;%m%n&quot; /&gt;
		&lt;/Console&gt;
		&lt;File name=&quot;LogToFile&quot; fileName=&quot;${sys:jboss.server.log.dir}/server.log&quot; append=&quot;true&quot;&gt;
			&lt;PatternLayout&gt;
				&lt;Pattern&gt;%m%n&lt;/Pattern&gt;
			&lt;/PatternLayout&gt;
		&lt;/File&gt;
	&lt;/Appenders&gt;
	&lt;Loggers&gt;
		&lt;Logger name=&quot;com.sematext.blog&quot; level=&quot;info&quot; additivity=&quot;true&quot;&gt;
            &lt;AppenderRef ref=&quot;LogToConsole&quot;/&gt;
        &lt;/Logger&gt;
        &lt;Root level=&quot;info&quot;&gt;
            &lt;AppenderRef ref=&quot;LogToFile&quot;/&gt;
        &lt;/Root&gt;
	&lt;/Loggers&gt;
&lt;/Configuration&gt;

I excluded the JBoss logging subsystem in the src\main\webapp\WEB-INF\jboss-deployment-structure.xml file:

&lt;jboss-deployment-structure&gt;
	&lt;deployment&gt;
		&lt;exclude-subsystems&gt;
		    &lt;subsystem name=&quot;logging&quot; /&gt;
	    &lt;/exclude-subsystems&gt;
	&lt;/deployment&gt;
&lt;/jboss-deployment-structure&gt;

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(&quot;/getListData&quot;)
	@Produces(MediaType.APPLICATION_JSON)
	@Override
	public ExampleResp getListData() {
        LOGGER.info(&quot;Inside of getListData method&quot;);
		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中。这意味着您应该只需要执行以下操作:

  1. 删除jboss-deployment-structure.xml,或至少删除模块排除项
  2. org.apache.logging:log4j-api Maven依赖项中添加&lt;scope&gt;provided&lt;/scope&gt;
  3. 删除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:

  1. Remove the jboss-deployment-structure.xml or at least remove module exclusions
  2. Add the &lt;scope&gt;provided&lt;/scope&gt; to the org.apache.logging:log4j-api Maven dependency
  3. 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.

huangapple
  • 本文由 发表于 2023年3月31日 03:42:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定