log4j2在web中的使用
一、依赖的包
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
<apache.log4j2.version>2.7</apache.log4j2.version> <slf4j.version>1.7.22</slf4j.version> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>${apache.log4j2.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${apache.log4j2.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>${apache.log4j2.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> <version>${apache.log4j2.version}</version> </dependency>
二、web.xml配置log4j2的监听器
<!--设置log4j的配置文件位置--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j2.xml</param-value> </context-param> <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>
三、如果项目是maven所建,把log4j2.xml放在根目录下
<?xml version="1.0" encoding="UTF-8"?> <configuration monitorInterval="30" status="DEBUG"> <Properties> <Property name="fileName">myLog.log</Property> <Property name="backupFilePatch">/Users/study-hello/spring-web/logs</Property> </Properties> <!--先定义所有的appender--> <appenders> <!--这个输出控制台的配置--> <Console name="Console" target="SYSTEM_OUT"> <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)--> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/> <!--这个都知道是输出日志的格式--> <PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %-10C %M %L: %m%n"/> </Console> <RollingFile fileName="${backupFilePatch}/debug.log" filePattern="${backupFilePatch}/$${date:yyyy-MM}/%d{yyyy-MM-dd}.debug.log" name="RollingFileDebug"> <Filters> <ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS z} [%-5level] %C %M %L: %m%n"/> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> <RollingFile fileName="${backupFilePatch}/info.log" filePattern="${backupFilePatch}/$${date:yyyy-MM}/%d{yyyy-MM-dd}.info.log" name="RollingFileInfo"> <Filters> <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS z} [%-5level] %C %M %L: %m%n"/> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> <RollingFile fileName="${backupFilePatch}/warn.log" filePattern="${backupFilePatch}/$${date:yyyy-MM}/%d{yyyy-MM-dd}.warn.log" name="RollingFileWarn"> <Filters> <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <!-- z 表示时区 --> <PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS z} [%-5level] %C %M %L: %m%n"/> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> <RollingFile fileName="${backupFilePatch}/error.log" filePattern="${backupFilePatch}/$${date:yyyy-MM}/%d{yyyy-MM-dd}.error.log" name="RollingFileError"> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> <!-- z 表示时区 --> <PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS z} [%-5level] %C %M %L: %m%n"/> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> </appenders> <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效--> <loggers> <Logger level="DEBUG" name="com.ibatis"> <AppenderRef ref="RollingFileDebug"/> <AppenderRef ref="RollingFileInfo"/> <AppenderRef ref="RollingFileWarn"/> <AppenderRef ref="RollingFileError"/> </Logger> <Root level="DEBUG"> <!--AppenderRef:Root的子节点,用来指定该日志输出到哪个Appender. --> <AppenderRef ref="Console"/> <AppenderRef ref="RollingFileDebug"/> <AppenderRef ref="RollingFileInfo"/> <AppenderRef ref="RollingFileWarn"/> <AppenderRef ref="RollingFileError"/> </Root> </loggers> </configuration>