log4j2 overview, configuration details, set log output level

Log4j: It is specially used for recording logs of our javat program; it is jar package + configuration file.
So when log4j is not used, how to print information?

System.out.println(====);

log4j2 official website: http://logging.apache.org/log4j/2.x/
Insert picture description here
File directory introduction:
Insert picture description here
deployment in the project:
Insert picture description here
configuration file log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- status=debug 可以查看log4j的装配过程 -->
<configuration status="off" monitorInterval="1800">
	<appenders>
		<!-- 定义控制台输出 -->
		<Console name="Console" target="SYSTEM_OUT" follow="true">
			<PatternLayout
				pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />
		</Console>

		<!-- 系统打印日志 ,按照天记录日志;filePattern文件的格式-->
		<RollingRandomAccessFile name="System"
			fileName="logs/system.log" filePattern="logs/system_%d{yyyy-MM-dd}_%i.log">
			<!-- 日志文件中内容的格式 -->
			<PatternLayout
				pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />
			<Policies>
				<!-- 一天记录的日志如果大于100m,重新再创建一个新的日志 -->
				<TimeBasedTriggeringPolicy interval="1"
					modulate="true" />
				<SizeBasedTriggeringPolicy size="100M" />
			</Policies>
		</RollingRandomAccessFile>
	</appenders>
	<loggers>
		<!-- Root Logger -->
		<root level="info">
			<!-- 引入了一个Console和System -->
			<appender-ref ref="Console" />
			<appender-ref ref="System" />
		</root>

		<!-- 外部日志,只记录到文件中,不记录到控制台中 -->
		<logger name="SystemLog" level="info" additivity="false">
			<appender-ref ref="System" />
		</logger>
	</loggers>
</configuration>

Log level:

ALL level: 是最低等级的,用于打开所有日志记录.
DEBUG Level: 指出细粒度信息事件对调试应用程序是非常有帮助的,就是输出debug的信息.
INFO level: 表明消息在粗粒度级别上突出强调应用程序的运行过程,就是输出提示信息.
WARN level: 表明会出现潜在错误的情形,就是显示警告信息.
ERROR level: 指出虽然发生错误事件,但仍然不影响系统的继续运行.就是显示错误信息.
FATAL level: 指出每个严重的错误事件将会导致应用程序的退出.
OFF level: 是最高等级的,用于关闭所有日志记录.
  • If the log level is set at a certain level, logs with a higher priority than this level can be printed
  • For example, if the priority is set to WARN, the log of 4 levels of OFF FATAL ERROR WARN can be output normally, while the log of INFO DEBUG TRACE ALL level will be ignored.

Guess you like

Origin blog.csdn.net/weixin_46122692/article/details/109187457