log4j2 不记录到控制台、文件或系统日志
log4j2 not logging to console, file or syslog
我正在努力让 log4j2 进行任何类型的日志记录。即使我将 log4j 配置调到跟踪级别,我也没有得到任何可用于调试的跟踪。
这正在使用(或者说试图使用)servlets 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>
仅供参考,log4j2.1 中似乎存在一个错误,如果您在 XML header (<?xml version="1.0" encoding="UTF-8"?>
) 之前有空格,它将默默地消失!
我正在努力让 log4j2 进行任何类型的日志记录。即使我将 log4j 配置调到跟踪级别,我也没有得到任何可用于调试的跟踪。
这正在使用(或者说试图使用)servlets 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>
仅供参考,log4j2.1 中似乎存在一个错误,如果您在 XML header (<?xml version="1.0" encoding="UTF-8"?>
) 之前有空格,它将默默地消失!