SSM环境下使用log4j2 需导入包
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.9.0</version>
</dependency>
在classpath下需创建log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--设置log4j2的自身log级别为DEBUG-->
<configuration status="DEBUG ">
<appenders>
<!-- 控制台输出 -->
<console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %n%msg%n"/>
</console>
<!-- fileName:输出路径 filePattern:命名规则 -->
<RollingFile name="RollingFileInfo" fileName="D:\\demo\\allOut.log"
filePattern="D:\\demo\\$${date:yyyy-MM-dd}/allOut-%d{yyyy-MM-dd}-%i.log.gz">
<Filters>
<ThresholdFilter level="all" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<!-- 输出格式 -->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
<Policies>
<TimeBasedTriggerzingPolicy interval="1"/>
<!-- SizeBasedTriggeringPolicy单个文件的大小限制 -->
<SizeBasedTriggeringPolicy size="2 MB"/>
</Policies>
<!-- DefaultRolloverStrategy同一个文件下的最大文件数 -->
<DefaultRolloverStrategy max="50"/>
</RollingFile>
<!-- <RollingFile name="RollingFileWarn" fileName="${sys:user.home}/logs/hpaasvc/warn.log"
filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="WARN"/>
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
</RollingFile>
<RollingFile name="RollingFileError" fileName="${sys:user.home}/logs/hpaasvc/error.log"
filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter level="ERROR"/>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
</RollingFile>
-->
</appenders>
<loggers>
<!--过滤掉spring和hibernate的一些无用的debug信息-->
<logger name="org.springframework" level="INFO">
</logger>
<logger name="org.hibernate" level="INFO">
</logger>
<root level="all">
<appender-ref ref="Console"/>
<appender-ref ref="RollingFileInfo"/>
<!-- <appender-ref ref="RollingFileWarn"/>
<appender-ref ref="RollingFileError"/> -->
</root>
</loggers>
</configuration>
简单的日志控制基类
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* 日志记录基类
* @author Administrator
*
*/
public abstract class BaseLogController {
private static Logger logger = LogManager.getLogger();
public BaseLogController() {
}
//级别从高到低:FATAL、ERROR、WARN、INFO、DEBUG、TRACE
public void fatal(Object message){
logger.fatal(message);
}
public void error(Object message){
logger.error(message);
}
public void warn(Object message){
logger.warn(message);
}
public void info(Object message){
logger.info(message);
}
public void debug(Object message){
logger.debug(message);
}
public void trace(Object message){
logger.trace(message);
}
}