前言
log4j2配置文件有不少配置,下面罗列一些,方便快速查看。
简单的配置
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="MyFile" fileName="all.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
严格模式的配置
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorinterval="30" status="info" strict="true">
<Properties>
<Property name="filename">log/CelsiusConverter.log</Property>
</Properties>
<Appenders>
<Appender type="Console" name="Console">
<Layout type="PatternLayout" pattern="%d %p [%t] %m%n" />
</Appender>
<Appender type="Console" name="FLOW">
<Layout type="PatternLayout" pattern="%C{1}.%M %m %ex%n" />
</Appender>
<Appender type="File" name="File" fileName="${filename}">
<Layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n" />
</Appender>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="File" />
<AppenderRef ref="Console" />
<!-- Use FLOW to trace down exact method sending the msg -->
<!-- <AppenderRef ref="FLOW" /> -->
</Root>
</Loggers>
</Configuration>
完整配置
<?xml version="1.0" encoding="UTF-8"?>
<!--Configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置-->
<!--monitorInterval:Log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数-->
<configuration status="WARN" monitorInterval="30">
<!--先定义所有的appender-->
<appenders>
<!--输出到控制台-->
<console name="Console" target="SYSTEM_OUT">
<!--输出日志的格式-->
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</console>
<!--打印到文件中,这个log每次运行程序会自动清空,由append属性决定-->
<File name="log" fileName="log/test.log" append="false">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File>
<!--文件滚动, 这个会打印出所有的info及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
<RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/info.log"
filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<DefaultRolloverStrategy max="20"/> <!--文件夹下最多的文件个数-->
<TimeBasedTriggeringPolicy interval="2 hour"/> <!--多长时间滚动一次-->
<SizeBasedTriggeringPolicy size="100 MB"/> <!-- 一个日志文件的最大大小 -->
</Policies>
</RollingFile>
</appenders>
<!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
<loggers>
<!--过滤掉spring和mybatis的一些无用的DEBUG信息-->
<logger name="org.springframework" level="INFO"></logger>
<logger name="org.mybatis" 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>
tomcat web 配置
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>/WEB-INF/conf/log4j2.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
项目中的配置
dev环境
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="warn">
<Appenders>
<Console name="STOUT" target="SYSTEM_OUT">
<PatternLayout
pattern="%highlight{%d [%t] %-5level: %msg%n%throwable}{FATAL=white, ERROR=red, WARN=blue, INFO=black, DEBUG=green, TRACE=blue}"/>
</Console>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="STOUT"/>
</Root>
</Loggers>
</Configuration>
test环境
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="warn">
<properties>
<property name="logPath">
/project/backend/code/
</property>
</properties>
<customLevels>
<customLevel name="BIZ" intLevel="350"/>
</customLevels>
<Appenders>
<RollingRandomAccessFile name="ERROR_LOGGER" fileName="${logPath}/logs2/error/error.log"
filePattern="${logPath}/logs2/error/error.log.%d{yyyy-MM-dd-HH}">
<PatternLayout>
<Pattern>[date:%d{DEFAULT}] [%t] %5level %c{1}: %msg%n</Pattern>
</PatternLayout>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="400 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="BIZ_LOGGER" fileName="${logPath}/logs2/biz/biz.log"
filePattern="${logPath}/logs2/biz/biz.log.%d{yyyy-MM-dd-HH}">
<PatternLayout>
<Pattern>[date:%d{DEFAULT}] [%t] %5level %c{1}: %msg%n</Pattern>
</PatternLayout>
<filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="biz" onMatch="ACCEPT" onMismatch="DENY"/>
</filters>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="400 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="INFO_LOGGER" fileName="${logPath}/logs2/info/info.log"
filePattern="${logPath}/logs2/info/info.log.%d{yyyy-MM-dd-HH}">
<PatternLayout>
<Pattern>[date:%d{DEFAULT}] [%t] %5level %c{1}: %msg%n</Pattern>
</PatternLayout>
<filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="biz" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</filters>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="400 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingRandomAccessFile>
<!--###################################-->
<RollingRandomAccessFile name="TRACE_ERROR_LOGGER" fileName="${logPath}/logs2/trace/error.log"
filePattern="${logPath}/logs2/trace/trace.log.%d{yyyy-MM-dd-HH}">
<PatternLayout>
<Pattern>[date:%d{DEFAULT}] [%t] %5level %c{1}: %msg%n</Pattern>
</PatternLayout>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="400 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="TRACE_SQL_LOGGER" fileName="${logPath}/logs2/trace/sql.log"
filePattern="${logPath}/logs2/sql/sql.log.%d{yyyy-MM-dd-HH}">
<PatternLayout>
<Pattern>[date:%d{DEFAULT}] [%t] %5level %c{1}: %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="400 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Logger name="com.alibaba.druid" additivity="false" level="info">
<AppenderRef ref="TRACE_SQL_LOGGER"/>
</Logger>
<Root level="info">
<AppenderRef ref="ERROR_LOGGER" level="error"/>
<AppenderRef ref="BIZ_LOGGER" level="biz"/>
<AppenderRef ref="INFO_LOGGER" level="info"/>
</Root>
</Loggers>
</Configuration>