欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

个人项目下超实用的一个简单log4j2日志配置

程序员文章站 2022-05-24 15:41:19
...

支持按照年月日及不同级别日志生成目录及文件

1、强烈建议根据官网学习

搜索官网的链接把https://logging.apache.org/log4j/2.x/
(偷懒的话,直接拷贝我的配置把~)

2、使用slf4jAPI,log4j2实现

引入依赖

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>${log4j.version}</version>
		</dependency>

3、配置

直接上配置

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" strict="false" name="sawFetchLog">
	<Properties>

		<!--定义基本日志参数-->
		<Property name="year">$${date:yyyy}</Property>
		<Property name="month">$${date:MM}</Property>
		<Property name="day">$${date:dd}</Property>
		<Property name="smoothPattern">$${date:yyyyMMdd}</Property>
		<Property name="everyPattern">$${date:yyyy-MM-dd}</Property>

		<Property name="DailylogDir">log/${year}/${month}/${day}</Property>

		<Property name="sawDebug">${DailylogDir}/fetchDebug</Property>
		<Property name="sawInfo">${DailylogDir}/fetchInfo</Property>
		<Property name="sawWarn">${DailylogDir}/fetchWarn</Property>
		<Property name="sawError">${DailylogDir}/fetchError</Property>

		<!--
			DEFAULT=yyyy-MM-dd HH:mm:ss.SSS
			%l 等价 %C.%M(%F:%L)
		-->
		<Property name="logPatternDefault">%d{DEFAULT} [%t] %-5level %l - %m%n</Property>
		<Property name="logPattern">%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}.(%M:%L) - %m%n</Property>

		<Property name="intervalValue">4</Property>
		<Property name="fileSize">10MB</Property>
		<Property name="MaxCount">20</Property>
		<Property name="fileEncoding">utf-8</Property>
	</Properties>

	<Appenders>
		<!--
			ConsoleAppender                     -   控制台打印,默认 target=SYSTEM_OUT
			FileAppender                        -   输出至单个文件,采用OutputStream
			RandomAccessFileAppender            -   输出值文件,采用 RandomAccessFile 在Buffer下相比较FileAppender效率高
			RollingFileAppender                 -   滚动输出
			RollingRandomAccessFileAppender     -   滚动输出,采用 RandomAccessFile
		-->
		<Console name="console">
			<PatternLayout pattern="${logPatternDefault}"/>
		</Console>
		<!--debug-->
		<RollingRandomAccessFile name="sawRollingFileDebug" fileName="${sawDebug}.log"
		                         filePattern="${DailylogDir}/${sawDebug}-%d{yyyy-MM-dd}-%i.log"> <!--.log.gz则压缩-->
			<PatternLayout pattern="${logPatternDefault}" charset="${fileEncoding}"/>
			<Policies>
				<TimeBasedTriggeringPolicy interval="${intervalValue}" modulate="true"/> <!--每4个小时翻滚,modulate从4小时开始-->
				<SizeBasedTriggeringPolicy size="${fileSize}"/><!--到达10MB翻滚文件-->
			</Policies>
			<DefaultRolloverStrategy max="${MaxCount}"/><!--最大翻滚次数,到达上限即从1开始覆盖-->

			<!--只输出debug 从高到低-->
			<Filters>
				<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
				<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
				<ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/>
				<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
			</Filters>
		</RollingRandomAccessFile>
		<!--info-->
		<RollingRandomAccessFile name="sawRollingFileInfo" fileName="${sawInfo}.log"
		                         filePattern="${DailylogDir}/${sawInfo}-%d{yyyy-MM-dd}-%i.log"> <!--.log.gz则压缩-->
			<PatternLayout pattern="${logPatternDefault}" charset="${fileEncoding}"/>
			<Policies>
				<TimeBasedTriggeringPolicy interval="${intervalValue}" modulate="true"/> <!--每4个小时翻滚,modulate从4小时开始-->
				<SizeBasedTriggeringPolicy size="${fileSize}"/><!--到达指定大小翻滚文件-->
			</Policies>
			<DefaultRolloverStrategy max="${MaxCount}"/><!--最大翻滚次数,到达上限即从1开始覆盖-->

			<!--只输出info 从高到低-->
			<Filters>
				<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
				<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
				<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
			</Filters>
		</RollingRandomAccessFile>

		<!--warn-->
		<RollingRandomAccessFile name="sawRollingFileWarn" fileName="${sawWarn}.log"
		                         filePattern="${DailylogDir}/${sawWarn}-%d{yyyy-MM-dd}-%i.log"> <!--.log.gz则压缩-->
			<PatternLayout pattern="${logPatternDefault}" charset="${fileEncoding}"/>
			<Policies>
				<TimeBasedTriggeringPolicy interval="${intervalValue}" modulate="true"/> <!--每4个小时翻滚,modulate从4小时开始-->
				<SizeBasedTriggeringPolicy size="${fileSize}"/><!--到达指定大小翻滚文件-->
			</Policies>
			<DefaultRolloverStrategy max="${MaxCount}"/><!--最大翻滚次数,到达上限即从1开始覆盖-->

			<!--只输出warn 从高到低-->
			<Filters>
				<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
				<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
			</Filters>
		</RollingRandomAccessFile>

		<!--error-->
		<RollingRandomAccessFile name="sawRollingFileError" fileName="${sawError}.log"
		                         filePattern="${DailylogDir}/${sawError}-%d{yyyy-MM-dd}-%i.log"> <!--.log.gz则压缩-->
			<PatternLayout pattern="${logPatternDefault}" charset="${fileEncoding}"/>
			<Policies>
				<TimeBasedTriggeringPolicy interval="${intervalValue}" modulate="true"/> <!--每4个小时翻滚,modulate从4小时开始-->
				<SizeBasedTriggeringPolicy size="${fileSize}"/><!--到达指定大小翻滚文件-->
			</Policies>
			<DefaultRolloverStrategy max="${MaxCount}"/><!--最大翻滚次数,到达上限即从1开始覆盖-->

			<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
		</RollingRandomAccessFile>
	</Appenders>

	<Loggers>
		<!--name可以指定包路径,某一类的class是某一个级别的日志-->
		<!--		<logger name="" level=""/>-->
		<Root level="debug">
			<AppenderRef ref="sawRollingFileDebug" additivity="false"/>
			<AppenderRef ref="sawRollingFileInfo" additivity="false"/>
			<AppenderRef ref="sawRollingFileWarn" additivity="false"/>
			<AppenderRef ref="sawRollingFileError" additivity="false"/>
			<AppenderRef ref="console" additivity="false"/>
		</Root>
	</Loggers>

</Configuration>

4、本地截图

简单易用
个人项目下超实用的一个简单log4j2日志配置

参考链接:
链接:
[1]https://logging.apache.org/log4j/2.x/manual/configuration.html
[2]https://www.cnblogs.com/wuqinglong/p/9516529.html

相关标签: log4j2