编程文汇

典型的log4j2配置

  1. 这里设置的日志格式尽量紧凑 格式对齐, 方便在控制台查看.
  2. 日志文件单个大小1M
  3. 日志文件最大个数10个
    也就是最多保存10M日志
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
	<Properties>
		<property name="defaultPattern">%d{yyyyMMdd HHmmss.SSS} %-3.-3level [%-24thread] %36logger{36} - %msg%n</property>
		<property name="baseDir">logs</property>
		<property name="maxFileSize">1MB</property>
		<property name="maxFiles">10</property>
	</Properties>

	<Appenders>
		<Console name="Console" target="SYSTEM_OUT">
			<PatternLayout pattern="${defaultPattern}" />
		</Console>
		<RollingFile name="RollingFile"
			fileName="${baseDir}/log.txt"
			filePattern="${baseDir}/log-%d{yyyy-MM-dd}.%i.his.txt">
			<PatternLayout>
				<Pattern>${defaultPattern}</Pattern>
			</PatternLayout>
			<Policies>
				<SizeBasedTriggeringPolicy size="${maxFileSize}" />
			</Policies>
			<DefaultRolloverStrategy max="900000">
				<Delete basePath="${baseDir}" maxDepth="1">
					<IfFileName glob="*.his.txt" />
					<IfAccumulatedFileCount exceeds="${maxFiles}" />
				</Delete>
			</DefaultRolloverStrategy>
		</RollingFile>
	</Appenders>

	<Loggers>
		<logger name="com.ez" level="debug" />
		<Root level="INFO">
			<AppenderRef ref="Console" />
			<AppenderRef ref="RollingFile" />
		</Root>
	</Loggers>
</Configuration>