最近在使用log4j 2,可以在同一个类里面实现不同级别的日志记录到不同的日志文件里面,可以实现根据日期或者文件大小分文件。
一、log4j 2的maven配置 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.coderwood</groupId> <artifactId>netspider</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>netspider</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.10</junit.version> <log4j2.version>2.5</log4j2.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>${log4j2.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j2.version}</version> </dependency> <dependency> <groupId>us.codecraft</groupId> <artifactId>webmagic-core</artifactId> <version>0.5.3</version> </dependency> <dependency> <groupId>us.codecraft</groupId> <artifactId>webmagic-extension</artifactId> <version>0.5.3</version> </dependency> </dependencies> </project>
二、log4j 2配置文件 log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="OFF" monitorInterval="1800"> <properties> <property name="LOG_HOME">D://logs/</property> <!--<property name="LOG_HOME">/root/log</property>--> <property name="FILE_NAME">netspider</property> <property name="ROOLINGFILE_NAME">netspiderroll</property> <property name="ROOLINGERRORFILE_NAME">netspidererror</property> <property name="ROOLINGERRORDOWNFILE_NAME">netspidererrordown</property> </properties> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <!-- <ThresholdFilter level="error" onMatch="ACCEPT" onMisMatch="DENY" /> --> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" /> </Console> <Console name="Console1" target="SYSTEM_OUT"> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" /> </Console> <File name="log" fileName="${LOG_HOME}/${FILE_NAME}.log" append="true"> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%class:%line] - %msg%n" /> </File> <RollingFile name="RollingFile" fileName="${LOG_HOME}/${ROOLINGFILE_NAME}.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/${ROOLINGFILE_NAME}-%d{MM-dd-yyyy}-%i.log.gz"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n" /> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <!-- <SizeBasedTriggeringPolicy size="1MB" /> --> </Policies> </RollingFile> <RollingFile name="RollingErrorFile" fileName="${LOG_HOME}/${ROOLINGERRORFILE_NAME}.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/${ROOLINGERRORFILE_NAME}-%d{MM-dd-yyyy}-%i.log.gz"> <ThresholdFilter level="error" onMatch="ACCEPT" onMisMatch="DENY" /> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n" /> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <!-- <SizeBasedTriggeringPolicy size="1MB" /> --> </Policies> </RollingFile> <RollingFile name="RollingErrorDownFile" fileName="${LOG_HOME}/${ROOLINGERRORDOWNFILE_NAME}.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/${ROOLINGERRORDOWNFILE_NAME}-%d{MM-dd-yyyy}-%i.log.gz"> <ThresholdFilter level="error" onMatch="DENY" onMisMatch="ACCEPT" /> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n" /> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <!-- <SizeBasedTriggeringPolicy size="1MB" /> --> </Policies> </RollingFile> </Appenders> <Loggers> <logger name="com.coderwood.netspider" level="trace" additivity="false"> <AppenderRef ref="RollingErrorFile" /> <AppenderRef ref="RollingErrorDownFile" /> </logger> <Root level="error"> <AppenderRef ref="Console" /> </Root> </Loggers> </Configuration>
三、log4j 2的使用代码 NetSpider.java
package com.coderwood.netspider; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class NetSpider { private static Logger logger = LogManager.getLogger(App.class); public static void main(String[] args) { try { testLog(); } catch (Exception e) { logger.error(e); } } public static void testLog() { logger.trace("trace"); logger.debug("debug"); logger.info("info"); logger.warn("warn"); logger.error("error"); logger.fatal("fatal"); } }