我正在努力让log4j2进行任何类型的日志记录。即使我将log4j配置调到跟踪级别,我也没有得到任何跟踪调试的痕迹。
这是使用(或者更确切地说是尝试使用)servlet 2.5和log4j 2.1
我有点失落,原因是因为我已经跟踪了所有的文档?
package myPkg;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.web.WebLoggerContextUtils;
@Path("myresource")
public class MyResource {
final Logger logger = LogManager.getLogger(MyResource.class);
logger.debug("test");
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
logger.debug("test");
logger.info("INFO");
logger.warn("WARN");
return "Got it!";
}
}
我已根据文档在web.xml中实现了更改,相关摘录如下:
<!-- log4j-->
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<filter>
<filter-name>log4jServletFilter</filter-name>
<filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log4jServletFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<context-param>
<description>log4j Context Name</description>
<param-name>log4jContextName</param-name>
<param-value>myWeb</param-value>
</context-param>
<context-param>
<description>log4j Configuration File Location</description>
<param-name>log4jConfiguration</param-name>
<param-value>file:///home/ME/testConfig/log4j.xml</param-value>
</context-param>
<!-- end log4j-->
我的log4j的pom.xml看起来像
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j.version}</version>
</dependency>
我的log4j.xml看起来像
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<!-- STDOUT -->
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%m%n"/>
</Console>
<!-- DEBUG FILE -->
<File name="debugFile" fileName="/home/ME/testConfig/debug.log">
<ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
<!-- SYSLOG -->
<Syslog name="RFC5424" format="RFC5424" host="localhost" port="514"
protocol="TCP" appName="VoIP" includeMDC="true"
facility="LOCAL0" enterpriseNumber="43053" newLine="true"
messageId="Audit" id="App"/>
<!-- DO LOGGING -->
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="debugFile" level="TRACE"/>
<AppenderRef ref="STDOUT" level="WARN"/>
<AppenderRef ref="RFC5424" level="INFO"/>
</Root>
</Loggers>
</Configuration>
答案 0 :(得分:0)
仅供参考,log4j2.1中似乎存在一个错误,如果你在XML标题(<?xml version="1.0" encoding="UTF-8"?>
)之前有空格,它会默默地死掉!