log4j2使用总结
官网:https://logging.apache.org/log4j/2.x/manual/customloglevels.html
这篇也很详细http://blog.csdn.net/u010201484/article/details/51723455
log4j2的jar包
log4j-api-2.3.jar
log4j-core-2.3.jar
log4j2的配置文件
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="off" monitorInterval="3600"> <properties> <!--定义变量,亦可以调用系统变量,比如${env:TESTLOG}/test/logs,其中TESTLOG是自己添加的系统环境变量 --> <property name="LOG_HOME">/var/test/logs</property> <property name="FILE_NAME">testFile</property> </properties> <!-- 自定义等级(原有的level 为 OFF 0、FATAL 100、ERROR 200、WARN 300、INFO 400、DEBUG 500、TRACE 600、 ALL) --> <CustomLevels> <CustomLevel name="myLevel" intLevel="99" /> </CustomLevels> <Appenders> <!-- 打印到控制台 --> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout charset="UTF-8" pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> <!-- 输出到文件 --> <!-- filePattern是log自动压缩后的压缩包的格式 PatternLayout是log信息的输出的格式 Policies输出策略 <TimeBasedTriggeringPolicy interval="1" modulate="true"/> 意思是间隔为1,所谓的间隔是filePattern中的%d{yyyy-MM-dd}精确到的单位, 如果是%d{yyyy-MM-dd-HH-mm},那么就是一分钟产生一个压缩文件 <SizeBasedTriggeringPolicy size="10 MB" />是指每10M生成一个新的文件 <DefaultRolloverStrategy max="50" />意思是只保留最近的50个文件 --> <RollingRandomAccessFile name="running-log" fileName="${LOG_HOME}/${FILE_NAME}.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/${FILE_NAME}-%d{yyyy-MM-dd}-%i.log.gz"> <PatternLayout charset="UTF-8" pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%c:%line] - %msg%n" /> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <SizeBasedTriggeringPolicy size="10 MB" /> </Policies> <DefaultRolloverStrategy max="50" /> <filters> <!-- <ThresholdFilter level="myLevel" onMatch="DENY" onMismatch="ACCEPT"/> 等级<=99,拒绝,其他的接受 <ThresholdFilter level="ERROR" onMatch="ACCEPT" /> 等级<=200,接受 这两个过滤器取交集 --> <ThresholdFilter level="myLevel" onMatch="DENY" onMismatch="ACCEPT"/> <ThresholdFilter level="ERROR" onMatch="ACCEPT" /> </filters> </RollingRandomAccessFile> <!--也可以这样写--> <!-- <File name="running-log" fileName="${LOG_HOME}/${FILE_NAME}.log"> <PatternLayout charset="UTF-8" pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%c:%line] - %msg%n" /> <Filters> <ThresholdFilter level="myLevel" onMatch="DENY" onMismatch="ACCEPT"/> <ThresholdFilter level="ERROR" onMatch="ACCEPT" /> </Filters> </File> --> <!-- 将log输入到远程的服务器中 --> <Syslog name="syslog" host="localhost" format="RFC5424" mdcId="mdc" enterpriseNumber="18060" appName="XXXX" facility="LOCAL0" port="9080" newline="true" protocol="TCP"> <LoggerFields> <KeyValuePair key="category" value="%c" /> <KeyValuePair key="priority" value="%p" /> </LoggerFields> </Syslog> </Appenders> <Loggers> <!-- <Root level="trace"> <AppenderRef ref="Console" /> </Root> --> <!-- Logger标签可以有多个 只输出等级高于error的log--> <Logger name="syslog" level="error"> <AppenderRef ref="syslog" /> </Logger> <!-- Root标签只可以有一个 只输出等级高于info的log--> <Root level="info"> <AppenderRef ref="running-log" /> </Root> </Loggers> </Configuration>
java代码调用方法
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Level; class LogLevel { public static final Level myLevel = Level.forName("AUDIT_INFO", 50); } public class HelloAnt { // private static Logger log = LoggerFactory.getLogger(HelloAnt.class); private static final Logger log = LogManager.getLogger(HelloAnt.class); public static void main(String[] args){ for(int i = 0;i<10;i++){ log.error(""+i); } log.info("hello log4j!");
// 自定义等级输出 log.log(LogLevel.myLevel, "qznRubish"); } }