NINGEN  

这里使用的是log4j2,需要引入相关jar包,设置配置文件

具体实现:

//声明logger对象

BaseLogger.java

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

 

public class BaseLogger {

protected final Logger logger = LogManager.getLogger(getClass());

}

 

//配置文件  log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" monitorInterval="0">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
<ThresholdFilter level="debug" onMatch="ACCEPT"
onMismatch="DENY" />
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>

<RollingFile name="RollingFileError" fileName="logs/log_error.log"
filePattern="logs/$${date-yyyy-MM}/log-%d{yyyy-MM-dd-HH}-%i.log.gz">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} %L %M - %msg%xEx%n" />
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<ThresholdFilter level="error" onMatch="ACCEPT"
onMismatch="DENY" />
<immediateFlush>true</immediateFlush>
</RollingFile>
<RollingFile name="RollingFileDebug" fileName="logs/log_debug.log"
filePattern="logs/$${date-yyyy-MM}/log-%d{yyyy-MM-dd-HH}-%i.log.gz">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} %L %M - %msg%xEx%n" />
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<bufferedIO>true</bufferedIO>
<ThresholdFilter level="debug" onMatch="ACCEPT"
onMismatch="DENY" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFileDebug" />
<AppenderRef ref="RollingFileError" />
</Root>
</Loggers>
</Configuration>

 

 

如果类中需要使用日志时,需继承日志的实现类,extends BaseLogger,然后直接用

logger.info();

即可

posted on 2017-01-06 11:00  NINGEN  阅读(149)  评论(0编辑  收藏  举报