The springboot project integrates log4j2 into a jar package, imports the external log4j2 file, and the running log cannot be printed

The springboot project integrates log4j2 into a jar package, imports the external log4j2 file, and the running log cannot be printed

Problem Description

Because there are already log4j2 files in the project, they are also included in the typed jar. The external log4j2 file is introduced in application.yml. When the project starts, the log can be printed, but when the log file reaches the specified value or the date After the rollback, the project will not log

Cause Analysis

log4j2 uses plug-in programming. When the log4j2 package is compiled, or the package containing the log4j2 plug-in is compiled, the plug-in information to be loaded will be placed in META-INF/org/apache/logging/log4j/core/config/plugins/ Log4j2Plugins.dat file (including the official logj42 native plug-in), and then when the project starts, log4j2 will scan the plug-in information file in the META-INF directory of each jar package, and then load the plug-in. But when the project is packaged into a jar package, if there is a Log4j2Plugins.dat file in two different jar packages, there will be a problem, one of the files will be overwritten by the other, resulting in a file when the project starts The plugin cannot be loaded normally, resulting in an error.

problem solved

Delete the log4j2 file in the project to ensure that there is no log4j2 file in the jar

Attach the configuration of log4j2

<?xml version="1.0" encoding="UTF-8"?>

<Configuration status="warn" name="MyApp" monitorInterval="60">
  <properties>
        <property name="LOG_HOME">/var/log/koal</property>
		<property name="LOG_FILE">ipsec-rms</property>
        <property name="FILE_NAME">ipsec-rms</property>
    </properties>
  <Appenders>
    <!--Console:日志输出到控制台标准输出 -->
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout charset="UTF-8" pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
    <RollingFile name="RollingFile" fileName="${LOG_HOME}/${FILE_NAME}.log" filePattern="${LOG_HOME}/${LOG_FILE}/$${date:yyyy-MM}/${FILE_NAME}-%d{yyyy-MM-dd}-%i.log">
	   <!--
	   onMatch="ACCEPT" 表示匹配该级别及以上
	   onMismatch="DENY" 表示不匹配该级别以下的
	   -->
      <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
	  <!--
	  格式化输出:
	  %d 	输出时间
	  %L	输出行号
	  %m或%msg或%message  输出日志信息
	  %t或%tn或%thread或%threadName	 输出线程id
	  %-5level  输出日志级别,-5表示左对齐并且固定输出5个字符,如果不足在右边补0
	  -->
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%L] - %msg%n" />
      <Policies>
	     <!--
		 TimeBasedTriggeringPolicy基于时间的触发策略:
		 该策略主要是完成周期性的log文件封存工作。有两个参数:interval,integer型,指定两次封存动作之间的时间间隔。这个配置需要和filePattern结合使用,filePattern日期格式精确到哪一位,interval也精确到哪一个单位。
		 注意filePattern中配置的文件重命名规则是%d{
    
    yyyy-MM-ddHH-mm-ss}-%i,最小的时间粒度是ss,即秒钟。TimeBasedTriggeringPolicy默认的size是1,结合起来就是每1秒钟生成一个新文件。如果改成%d{
    
    yyyy-MM-dd HH},最小粒度为小时,则每一个小时生成一个文件-->
        <TimeBasedTriggeringPolicy/>
        <SizeBasedTriggeringPolicy size="20MB"/>
      </Policies>
      <!-- 最多同一文件夹下5个文件开始覆盖-->
	  <DefaultRolloverStrategy max="5">
	       <!-- 
		   Delete 删除策略
		   maxDepth要访问的目录的最大级别数。值为0表示仅访问起始文件,除非安全管理器拒绝。Integer.MAX_VALUE的值指示应访问所有级别。
		   默认值为1,表示仅指定基本路径中的文件。-->
		 <Delete basePath="${LOG_HOME}/${LOG_FILE}/$${date:yyyy-MM}/" maxDepth="2">
			<IfFileName glob="*.log" />
			<!--!Note: 这里的age必须和filePattern协调, 后者是精确到HH, 这里就要写成xH, xd就不起作用
			另外, 数字最好>2, 否则可能造成删除的时候, 最近的文件还处于被占用状态,导致删除不成功!-->
			<!--30天-->
			<IfLastModified age="30d" />
		 </Delete>
      </DefaultRolloverStrategy>
    </RollingFile>
    <Syslog name="SYSLOG" host="127.0.0.1" port="514" protocol="UDP"/>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="RollingFile"/>
      <AppenderRef ref="SYSLOG"/>
    </Root>
  </Loggers>
</Configuration>





Guess you like

Origin blog.csdn.net/sunrj_niu/article/details/128033321