Maven中pom.xml引用
<log4j2.version>2.7</log4j2.version>版本以上
<!-- log4j2 高并发造成死锁 -->
<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>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.lmax/disruptor -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.6</version>
</dependency>
配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,你会看到log4j2内部各种详细输出-->
<!--monitorInterval:Log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数-->
<configuration status="WARN" monitorInterval="30">
<!--先定义所有的appender-->
<appenders>
<!--这个输出控制台的配置-->
<!-- <console name="Console" target="SYSTEM_OUT">
输出日志的格式
<PatternLayout charset="GBK" pattern="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</console> -->
<!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用-->
<!-- <File name="log" fileName="log/test.log" append="false">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File> -->
<!-- 这个会打印出所有的info及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
<!-- ${sys:user.home} 当前用户目录,但创建文件夹没权限失败的情况 -->
<RollingRandomAccessFile name="RollingRandomAccessFile-RcsDataSys-Debug" fileName="/logs/RcsDataSys/RcsDataSys-debug.log"
filePattern="/logs/RcsDataSys/$${date:yyyy-MM}/RcsDataSys-debug-%d{yyyy-MM-dd}-%i.log" immediateFlush="true" Append="true">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<Filters>
<!-- 禁止error日志进入 纯的info日志-->
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<DefaultRolloverStrategy max="15">
<Delete basePath="/logs/RcsDataSys" maxDepth="2">
<IfFileName glob="*/RcsDataSys-debug*.log" />
<IfLastModified age="3d" />
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile >
<RollingRandomAccessFile name="RollingRandomAccessFile-RcsDataSys-Info" fileName="/logs/RcsDataSys/RcsDataSys-info.log"
filePattern="/logs/RcsDataSys/$${date:yyyy-MM}/RcsDataSys-info-%d{yyyy-MM-dd}-%i.log" immediateFlush="true" >
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<Filters>
<!-- 禁止error日志进入 纯的info日志-->
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="3">
<Delete basePath="/logs/RcsDataSys" maxDepth="2">
<IfFileName glob="*/RcsDataSys-info*.log" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile >
<RollingRandomAccessFile name="RollingRandomAccessFile-RcsDataSys-Warn" fileName="/logs/RcsDataSys/RcsDataSys-warn.log"
filePattern="/logs/RcsDataSys/$${date:yyyy-MM}/RcsDataSys-warn-%d{yyyy-MM-dd}-%i.log" immediateFlush="true">
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20 -->
<DefaultRolloverStrategy max="2"/>
</RollingRandomAccessFile >
<RollingRandomAccessFile name="RollingRandomAccessFile-RcsDataSys-Error" fileName="/logs/RcsDataSys/RcsDataSys-error.log"
filePattern="/logs/RcsDataSys/$${date:yyyy-MM}/RcsDataSys-error-%d{yyyy-MM-dd}-%i.log" immediateFlush="true">
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<DefaultRolloverStrategy max="3">
<Delete basePath="/logs/RcsDataSys" maxDepth="2">
<IfFileName glob="*/RcsDataSys-error*.log" />
<IfLastModified age="3d" />
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile >
</appenders>
<!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
<loggers>
<!--过滤掉spring和mybatis的一些无用的DEBUG信息-->
<logger name="org.springframework" level="INFO"></logger>
<logger name="org.mybatis" level="INFO"></logger>
<logger name="org.apache.ibatis" level="INFO"></logger>
<logger name="org.apache.ibatis.logging.jdbc.BaseJdbcLogger" additivity="false"></logger>
<asyncRoot level="DEBUG" includeLocation="true">
<appender-ref ref="RollingRandomAccessFile-RcsDataSys-Debug"/>
<appender-ref ref="RollingRandomAccessFile-RcsDataSys-Info"/>
<appender-ref ref="RollingRandomAccessFile-RcsDataSys-Error"/>
</asyncRoot>
<logger name="druid.sql.Statement" level="info" additivity="false">
</logger>
<logger name="druid.sql.ResultSet" level="info" additivity="false">
</logger>
<logger name="com.alibaba.druid" level="info" additivity="false">
</logger>
<logger name="org.quartz" level="info" additivity="false">
</logger>
</loggers>
</configuration>