一、仅LOG4J2,只需要加上这个包即可。
1.在POM.XML中引入如下包。
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.12.1</version>
</dependency>
2.在resource目录下新建log4j2.xml,在log4j初始化context时会自动寻找resource下的log4j2.xml文件进行配置,其它配置无效。(log4j2.properties,lo4j2.json,log4j2.yaml这几个都可以)
<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--
自定义命名格式:
%d:发生时间,%d{yyyy-MM-dd HH:mm:ss,SSS},输出类似:2020-02-20 22:10:28,921
%F:输出所在的类文件名
%t:线程名称
%p:日志级别
%c:日志消息所在类名
%m:消息内容
%M:输出所在函数名
%x: 输出和当前线程相关联的NDC(嵌套诊断环境),尤其用到像java servlets这样的多客户多线程的应用中。
%l:执行的函数名(类名称:行号)com.core.LogHelper.aroundService(LogHelper.java:32)
%n:换行
%i:从1开始自增数字
%-5level:输出日志级别,-5表示左对齐并且固定输出5个字符,如果不足在右边补0
“${sys:user.home}”是HOME目录 如:C:\Users\heave, 此处指定任意目录如:D:\logs
-->
<!--设置log4j2自身log级别为warn,每间隔30秒数自动检测配置是否发生修改,并重新配置-->
<configuration status="DEBUG" monitorInterval="30">
<Properties>
<!-- 日志显示模板,显示内容的格式如下 -->
<!-- [21:55:33:047] [INFO] - org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:173) - Initializing Spring embedded WebApplicationContext -->
<Property name="log_pattern" value="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<!-- 保存日志文件目录 -->
<!--<Property name="file_path" value="${sys:user.home}/logs"/>-->
<Property name="file_path" value="./logs"/>
<!-- 日志文件的最大容量,超过该值就进行备份 -->
<Property name="file_max_size" value="30MB"/>
<!-- 备份的文件夹名称 如下为:2020-02 -->
<Property name="backup_folder" value="$${date:yyyy-MM}"/>
<!-- 备份文件的后缀,日志文件超过file_max_size会备份到filePattern指定的目录下 -->
<Property name="backup_file_suffix" value="-%d{yyyy-MM-dd}-%i.log"/>
</Properties>
<!--定义appender-->
<appenders>
<!--控制台的输出配置-->
<console name="Console" target="SYSTEM_OUT">
<!-- 设置控制台只输出info及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
<!--输出日志的格式-->
<PatternLayout pattern="${log_pattern}"/>
</console>
<!-- 所有的日志信息会打印到此文件中,append=false每次启动程序会自动清空 -->
<!--<File name="all" fileName="${file_path}/all.log" append="true">-->
<!--<PatternLayout pattern="${log_pattern}"/>-->
<!--</File>-->
<!--
该RollingFile存储INFO级别的日志,
默认存储到 fileName 文件中
超过SizeBasedTriggeringPolicy的设定值,则存储到 filePattern 文件中
-->
<RollingFile name="RollingFileDebug" fileName="${file_path}/debug.log"
filePattern="${file_path}/${backup_folder}/debug${backup_file_suffix}">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
<!-- 写入日志文件的模板 -->
<PatternLayout pattern="${log_pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="${file_max_size}"/>
</Policies>
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,超过该数量,会滚动删除前面的记录 -->
<DefaultRolloverStrategy max="20"/>
</RollingFile>
<RollingFile name="RollingFileInfo" fileName="${file_path}/info.log"
filePattern="${file_path}/${backup_folder}/info${backup_file_suffix}">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<!-- 写入日志文件的模板 -->
<PatternLayout pattern="${log_pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="${file_max_size}"/>
</Policies>
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,超过该数量,会滚动删除前面的记录 -->
<DefaultRolloverStrategy max="20"/>
</RollingFile>
<RollingFile name="RollingFileWarn" fileName="${file_path}/warn.log"
filePattern="${file_path}/${backup_folder}/warn${backup_file_suffix}">
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${log_pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="${file_max_size}"/>
</Policies>
</RollingFile>
<RollingFile name="RollingFileError" fileName="${file_path}/error.log" filePattern="${file_path}/${backup_folder}/error${backup_file_suffix}">
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${log_pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="${file_max_size}"/>
</Policies>
</RollingFile>
</appenders>
<!-- 只有定义了logger并使用appender-ref,appender才会生效 -->
<loggers>
<!--过滤掉spring和hibernate的一些无用的debug信息-->
<logger name="org.springframework" level="TRACE"/>
<logger name="org.mybatis" level="INFO">
<!-- 添加如下设置,控制台会再打印一次 -->
<AppenderRef ref="Console"/>
</logger>
<root level="DEBUG">
<appender-ref ref="Console"/>
<appender-ref ref="RollingFileDebug"/>
<appender-ref ref="RollingFileInfo"/>
<appender-ref ref="RollingFileWarn"/>
<appender-ref ref="RollingFileError"/>
</root>
</loggers>
</configuration>
3.1.在log4j2-core中会强制写死在resource目录下寻找带log4j2的配置。由spring-jcl包中的apache通用日志库调用到LogFactory.getLog(Class<?> clazz)
Log4jAdapter.createLog(name),
org.apache.commons.logging.LogAdapter
public static Log createLog(String name) {
switch (logApi) {
case LOG4J:
return Log4jAdapter.createLog(name);
case SLF4J_LAL:
return Slf4jAdapter.createLocationAwareLog(name);
case SLF4J:
return Slf4jAdapter.createLog(name);
default:
// Defensively use lazy-initializing adapter class here as well since the
// java.logging module is not present by default on JDK 9. We are requiring
// its presence if neither Log4j nor SLF4J is available; however, in the
// case of Log4j or SLF4J, we are trying to prevent early initialization
// of the JavaUtilLog adapter - e.g. by a JVM in debug mode - when eagerly
// trying to parse the bytecode for all the cases of this switch clause.
return JavaUtilAdapter.createLog(name);
}
}
最后调用到Log4jAdapter适配器的创建LOG。
private static class Log4jAdapter {
public static Log createLog(String name) {
return new Log4jLog(name);
}
}
上述中间会调用log4j2-api中的logcontext 进行初始化。
private static final LoggerContext loggerContext =
LogManager.getContext(Log4jLog.class.getClassLoader(), false);
private final ExtendedLogger logger;
public Log4jLog(String name) {
LoggerContext context = loggerContext;
if (context == null) {
// Circular call in early-init scenario -> static field not initialized yet
context = LogManager.getContext(Log4jLog.class.getClassLoader(), false);
}
this.logger = context.getLogger(name);
}
最终会调用到log4j2-core中的org.apache.logging.log4j.core.LoggerContext.reconfigure接口读取log4j2配置进行初始化。
log4j2内部的日志输出器为org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely,可以在这里面进行断点,查看日志加载流程。以下是加载流程。
2021-07-13 16:44:15,619 RMI TCP Connection(3)-127.0.0.1 DEBUG Apache Log4j Core 2.12.1 initializing configuration XmlConfiguration[location=E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml]
2021-07-13 16:44:15,627 RMI TCP Connection(3)-127.0.0.1 DEBUG Installed 1 script engine
2021-07-13 16:44:15,946 RMI TCP Connection(3)-127.0.0.1 DEBUG Oracle Nashorn version: 1.8.0_161, language: ECMAScript, threading: Not Thread Safe, compile: true, names: [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript], factory class: jdk.nashorn.api.scripting.NashornScriptEngineFactory
2021-07-13 16:44:15,947 RMI TCP Connection(3)-127.0.0.1 DEBUG PluginManager 'Core' found 118 plugins
2021-07-13 16:44:15,947 RMI TCP Connection(3)-127.0.0.1 DEBUG PluginManager 'Level' found 0 plugins
2021-07-13 16:44:15,956 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=property, class=org.apache.logging.log4j.core.config.Property].
2021-07-13 16:44:15,977 RMI TCP Connection(3)-127.0.0.1 DEBUG PluginManager 'TypeConverter' found 26 plugins
2021-07-13 16:44:15,993 RMI TCP Connection(3)-127.0.0.1 DEBUG createProperty(name="log_pattern", value="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n")
2021-07-13 16:44:15,993 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=property, class=org.apache.logging.log4j.core.config.Property].
2021-07-13 16:44:15,993 RMI TCP Connection(3)-127.0.0.1 DEBUG createProperty(name="file_path", value="./logs")
2021-07-13 16:44:15,994 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=property, class=org.apache.logging.log4j.core.config.Property].
2021-07-13 16:44:15,994 RMI TCP Connection(3)-127.0.0.1 DEBUG createProperty(name="file_max_size", value="30MB")
2021-07-13 16:44:15,995 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=property, class=org.apache.logging.log4j.core.config.Property].
2021-07-13 16:44:15,996 RMI TCP Connection(3)-127.0.0.1 DEBUG createProperty(name="backup_folder", value="${date:yyyy-MM}")
2021-07-13 16:44:15,999 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=property, class=org.apache.logging.log4j.core.config.Property].
2021-07-13 16:44:15,999 RMI TCP Connection(3)-127.0.0.1 DEBUG createProperty(name="backup_file_suffix", value="-%d{yyyy-MM-dd}-%i.log")
2021-07-13 16:44:16,000 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=properties, class=org.apache.logging.log4j.core.config.PropertiesPlugin].
2021-07-13 16:44:16,007 RMI TCP Connection(3)-127.0.0.1 DEBUG configureSubstitutor(={log_pattern=[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n, file_path=./logs, file_max_size=30MB, backup_folder=${date:yyyy-MM}, backup_file_suffix=-%d{yyyy-MM-dd}-%i.log}, Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml))
2021-07-13 16:44:16,007 RMI TCP Connection(3)-127.0.0.1 DEBUG PluginManager 'Lookup' found 13 plugins
2021-07-13 16:44:16,009 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=filter, class=org.apache.logging.log4j.core.filter.ThresholdFilter].
2021-07-13 16:44:16,015 RMI TCP Connection(3)-127.0.0.1 DEBUG createFilter(level="DEBUG", onMatch="ACCEPT", onMismatch="DENY")
2021-07-13 16:44:16,016 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2021-07-13 16:44:16,022 RMI TCP Connection(3)-127.0.0.1 DEBUG PatternLayout$Builder(pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n", PatternSelector=null, Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2021-07-13 16:44:16,024 RMI TCP Connection(3)-127.0.0.1 DEBUG PluginManager 'Converter' found 44 plugins
2021-07-13 16:44:16,047 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
2021-07-13 16:44:16,058 RMI TCP Connection(3)-127.0.0.1 DEBUG ConsoleAppender$Builder(target="SYSTEM_OUT", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout([%d{HH:mm:ss:SSS}] [%p] - %l - %m%n), name="Console", Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml), ThresholdFilter(DEBUG), ={})
2021-07-13 16:44:16,061 RMI TCP Connection(3)-127.0.0.1 DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false
2021-07-13 16:44:16,061 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=filter, class=org.apache.logging.log4j.core.filter.ThresholdFilter].
2021-07-13 16:44:16,061 RMI TCP Connection(3)-127.0.0.1 DEBUG createFilter(level="DEBUG", onMatch="ACCEPT", onMismatch="DENY")
2021-07-13 16:44:16,062 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2021-07-13 16:44:16,062 RMI TCP Connection(3)-127.0.0.1 DEBUG PatternLayout$Builder(pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n", PatternSelector=null, Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2021-07-13 16:44:16,063 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=TimeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy].
2021-07-13 16:44:16,068 RMI TCP Connection(3)-127.0.0.1 DEBUG TimeBasedTriggeringPolicy$Builder(interval="null", modulate="null", maxRandomDelay="null")
2021-07-13 16:44:16,069 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=SizeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy].
2021-07-13 16:44:16,069 RMI TCP Connection(3)-127.0.0.1 DEBUG createPolicy(size="30MB")
2021-07-13 16:44:16,071 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=Policies, class=org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy].
2021-07-13 16:44:16,072 RMI TCP Connection(3)-127.0.0.1 DEBUG createPolicy(={TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=31457280)})
2021-07-13 16:44:16,072 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=DefaultRolloverStrategy, class=org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy].
2021-07-13 16:44:16,077 RMI TCP Connection(3)-127.0.0.1 DEBUG DefaultRolloverStrategy$Builder(max="20", min="null", fileIndex="null", compressionLevel="null", ={}, stopCustomActionsOnError="null", tempCompressedFilePattern="null", Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml))
2021-07-13 16:44:16,078 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.RollingFileAppender].
2021-07-13 16:44:16,081 RMI TCP Connection(3)-127.0.0.1 DEBUG RollingFileAppender$Builder(fileName="./logs/debug.log", filePattern="./logs/2021-07/debug-%d{yyyy-MM-dd}-%i.log", append="null", locking="null", Policies(CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=31457280)])), DefaultRolloverStrategy(DefaultRolloverStrategy(min=1, max=20, useMax=true)), advertise="null", advertiseUri="null", createOnDemand="null", filePermissions="null", fileOwner="null", fileGroup="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout([%d{HH:mm:ss:SSS}] [%p] - %l - %m%n), name="RollingFileDebug", Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml), ThresholdFilter(DEBUG), ={})
2021-07-13 16:44:16,091 RMI TCP Connection(3)-127.0.0.1 DEBUG Returning file creation time for D:\software2\tomcat\apache-tomcat-8.5.59-windows-x64\apache-tomcat-8.5.59\bin\.\logs\debug.log
2021-07-13 16:44:16,091 RMI TCP Connection(3)-127.0.0.1 DEBUG Starting RollingFileManager ./logs/debug.log
2021-07-13 16:44:16,094 RMI TCP Connection(3)-127.0.0.1 DEBUG PluginManager 'FileConverter' found 2 plugins
2021-07-13 16:44:16,097 RMI TCP Connection(3)-127.0.0.1 DEBUG Setting prev file time to 2021-07-13T16:22:18.003+0800
2021-07-13 16:44:16,102 RMI TCP Connection(3)-127.0.0.1 DEBUG Initializing triggering policy CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=31457280)])
2021-07-13 16:44:16,103 RMI TCP Connection(3)-127.0.0.1 DEBUG Initializing triggering policy TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false)
2021-07-13 16:44:16,104 RMI TCP Connection(3)-127.0.0.1 DEBUG Initializing triggering policy SizeBasedTriggeringPolicy(size=31457280)
2021-07-13 16:44:16,104 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=filter, class=org.apache.logging.log4j.core.filter.ThresholdFilter].
2021-07-13 16:44:16,105 RMI TCP Connection(3)-127.0.0.1 DEBUG createFilter(level="INFO", onMatch="ACCEPT", onMismatch="DENY")
2021-07-13 16:44:16,105 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2021-07-13 16:44:16,106 RMI TCP Connection(3)-127.0.0.1 DEBUG PatternLayout$Builder(pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n", PatternSelector=null, Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2021-07-13 16:44:16,107 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=TimeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy].
2021-07-13 16:44:16,108 RMI TCP Connection(3)-127.0.0.1 DEBUG TimeBasedTriggeringPolicy$Builder(interval="null", modulate="null", maxRandomDelay="null")
2021-07-13 16:44:16,108 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=SizeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy].
2021-07-13 16:44:16,109 RMI TCP Connection(3)-127.0.0.1 DEBUG createPolicy(size="30MB")
2021-07-13 16:44:16,109 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=Policies, class=org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy].
2021-07-13 16:44:16,110 RMI TCP Connection(3)-127.0.0.1 DEBUG createPolicy(={TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=31457280)})
2021-07-13 16:44:16,110 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=DefaultRolloverStrategy, class=org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy].
2021-07-13 16:44:16,110 RMI TCP Connection(3)-127.0.0.1 DEBUG DefaultRolloverStrategy$Builder(max="20", min="null", fileIndex="null", compressionLevel="null", ={}, stopCustomActionsOnError="null", tempCompressedFilePattern="null", Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml))
2021-07-13 16:44:16,111 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.RollingFileAppender].
2021-07-13 16:44:16,111 RMI TCP Connection(3)-127.0.0.1 DEBUG RollingFileAppender$Builder(fileName="./logs/info.log", filePattern="./logs/2021-07/info-%d{yyyy-MM-dd}-%i.log", append="null", locking="null", Policies(CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=31457280)])), DefaultRolloverStrategy(DefaultRolloverStrategy(min=1, max=20, useMax=true)), advertise="null", advertiseUri="null", createOnDemand="null", filePermissions="null", fileOwner="null", fileGroup="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout([%d{HH:mm:ss:SSS}] [%p] - %l - %m%n), name="RollingFileInfo", Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml), ThresholdFilter(INFO), ={})
2021-07-13 16:44:16,113 RMI TCP Connection(3)-127.0.0.1 DEBUG Returning file creation time for D:\software2\tomcat\apache-tomcat-8.5.59-windows-x64\apache-tomcat-8.5.59\bin\.\logs\info.log
2021-07-13 16:44:16,113 RMI TCP Connection(3)-127.0.0.1 DEBUG Starting RollingFileManager ./logs/info.log
2021-07-13 16:44:16,113 RMI TCP Connection(3)-127.0.0.1 DEBUG PluginManager 'FileConverter' found 2 plugins
2021-07-13 16:44:16,114 RMI TCP Connection(3)-127.0.0.1 DEBUG Setting prev file time to 2021-07-13T16:22:18.026+0800
2021-07-13 16:44:16,114 RMI TCP Connection(3)-127.0.0.1 DEBUG Initializing triggering policy CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=31457280)])
2021-07-13 16:44:16,114 RMI TCP Connection(3)-127.0.0.1 DEBUG Initializing triggering policy TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false)
2021-07-13 16:44:16,114 RMI TCP Connection(3)-127.0.0.1 DEBUG Initializing triggering policy SizeBasedTriggeringPolicy(size=31457280)
2021-07-13 16:44:16,114 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=filter, class=org.apache.logging.log4j.core.filter.ThresholdFilter].
2021-07-13 16:44:16,115 RMI TCP Connection(3)-127.0.0.1 DEBUG createFilter(level="WARN", onMatch="ACCEPT", onMismatch="DENY")
2021-07-13 16:44:16,115 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2021-07-13 16:44:16,117 RMI TCP Connection(3)-127.0.0.1 DEBUG PatternLayout$Builder(pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n", PatternSelector=null, Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2021-07-13 16:44:16,118 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=TimeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy].
2021-07-13 16:44:16,118 RMI TCP Connection(3)-127.0.0.1 DEBUG TimeBasedTriggeringPolicy$Builder(interval="null", modulate="null", maxRandomDelay="null")
2021-07-13 16:44:16,118 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=SizeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy].
2021-07-13 16:44:16,119 RMI TCP Connection(3)-127.0.0.1 DEBUG createPolicy(size="30MB")
2021-07-13 16:44:16,120 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=Policies, class=org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy].
2021-07-13 16:44:16,120 RMI TCP Connection(3)-127.0.0.1 DEBUG createPolicy(={TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=31457280)})
2021-07-13 16:44:16,120 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.RollingFileAppender].
2021-07-13 16:44:16,122 RMI TCP Connection(3)-127.0.0.1 DEBUG RollingFileAppender$Builder(fileName="./logs/warn.log", filePattern="./logs/2021-07/warn-%d{yyyy-MM-dd}-%i.log", append="null", locking="null", Policies(CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=31457280)])), Strategy=null, advertise="null", advertiseUri="null", createOnDemand="null", filePermissions="null", fileOwner="null", fileGroup="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout([%d{HH:mm:ss:SSS}] [%p] - %l - %m%n), name="RollingFileWarn", Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml), ThresholdFilter(WARN), ={})
2021-07-13 16:44:16,124 RMI TCP Connection(3)-127.0.0.1 DEBUG Returning file creation time for D:\software2\tomcat\apache-tomcat-8.5.59-windows-x64\apache-tomcat-8.5.59\bin\.\logs\warn.log
2021-07-13 16:44:16,125 RMI TCP Connection(3)-127.0.0.1 DEBUG Starting RollingFileManager ./logs/warn.log
2021-07-13 16:44:16,125 RMI TCP Connection(3)-127.0.0.1 DEBUG PluginManager 'FileConverter' found 2 plugins
2021-07-13 16:44:16,125 RMI TCP Connection(3)-127.0.0.1 DEBUG Setting prev file time to 2021-07-13T16:22:18.036+0800
2021-07-13 16:44:16,126 RMI TCP Connection(3)-127.0.0.1 DEBUG Initializing triggering policy CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=31457280)])
2021-07-13 16:44:16,126 RMI TCP Connection(3)-127.0.0.1 DEBUG Initializing triggering policy TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false)
2021-07-13 16:44:16,126 RMI TCP Connection(3)-127.0.0.1 DEBUG Initializing triggering policy SizeBasedTriggeringPolicy(size=31457280)
2021-07-13 16:44:16,127 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=filter, class=org.apache.logging.log4j.core.filter.ThresholdFilter].
2021-07-13 16:44:16,127 RMI TCP Connection(3)-127.0.0.1 DEBUG createFilter(level="ERROR", onMatch="ACCEPT", onMismatch="DENY")
2021-07-13 16:44:16,127 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2021-07-13 16:44:16,128 RMI TCP Connection(3)-127.0.0.1 DEBUG PatternLayout$Builder(pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n", PatternSelector=null, Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2021-07-13 16:44:16,128 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=TimeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy].
2021-07-13 16:44:16,128 RMI TCP Connection(3)-127.0.0.1 DEBUG TimeBasedTriggeringPolicy$Builder(interval="null", modulate="null", maxRandomDelay="null")
2021-07-13 16:44:16,129 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=SizeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy].
2021-07-13 16:44:16,129 RMI TCP Connection(3)-127.0.0.1 DEBUG createPolicy(size="30MB")
2021-07-13 16:44:16,129 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=Policies, class=org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy].
2021-07-13 16:44:16,130 RMI TCP Connection(3)-127.0.0.1 DEBUG createPolicy(={TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=31457280)})
2021-07-13 16:44:16,130 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.RollingFileAppender].
2021-07-13 16:44:16,131 RMI TCP Connection(3)-127.0.0.1 DEBUG RollingFileAppender$Builder(fileName="./logs/error.log", filePattern="./logs/2021-07/error-%d{yyyy-MM-dd}-%i.log", append="null", locking="null", Policies(CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=31457280)])), Strategy=null, advertise="null", advertiseUri="null", createOnDemand="null", filePermissions="null", fileOwner="null", fileGroup="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout([%d{HH:mm:ss:SSS}] [%p] - %l - %m%n), name="RollingFileError", Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml), ThresholdFilter(ERROR), ={})
2021-07-13 16:44:16,132 RMI TCP Connection(3)-127.0.0.1 DEBUG Returning file creation time for D:\software2\tomcat\apache-tomcat-8.5.59-windows-x64\apache-tomcat-8.5.59\bin\.\logs\error.log
2021-07-13 16:44:16,132 RMI TCP Connection(3)-127.0.0.1 DEBUG Starting RollingFileManager ./logs/error.log
2021-07-13 16:44:16,133 RMI TCP Connection(3)-127.0.0.1 DEBUG PluginManager 'FileConverter' found 2 plugins
2021-07-13 16:44:16,133 RMI TCP Connection(3)-127.0.0.1 DEBUG Setting prev file time to 2021-07-13T16:22:18.045+0800
2021-07-13 16:44:16,134 RMI TCP Connection(3)-127.0.0.1 DEBUG Initializing triggering policy CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=31457280)])
2021-07-13 16:44:16,134 RMI TCP Connection(3)-127.0.0.1 DEBUG Initializing triggering policy TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false)
2021-07-13 16:44:16,134 RMI TCP Connection(3)-127.0.0.1 DEBUG Initializing triggering policy SizeBasedTriggeringPolicy(size=31457280)
2021-07-13 16:44:16,135 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
2021-07-13 16:44:16,135 RMI TCP Connection(3)-127.0.0.1 DEBUG createAppenders(={Console, RollingFileDebug, RollingFileInfo, RollingFileWarn, RollingFileError})
2021-07-13 16:44:16,135 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
2021-07-13 16:44:16,137 RMI TCP Connection(3)-127.0.0.1 DEBUG createLogger(additivity="true", level="TRACE", name="org.springframework", includeLocation="null", ={}, ={}, Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml), Filter=null)
2021-07-13 16:44:16,141 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2021-07-13 16:44:16,141 RMI TCP Connection(3)-127.0.0.1 DEBUG createAppenderRef(ref="Console", level="null", Filter=null)
2021-07-13 16:44:16,142 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
2021-07-13 16:44:16,143 RMI TCP Connection(3)-127.0.0.1 DEBUG createLogger(additivity="true", level="INFO", name="org.mybatis", includeLocation="null", ={Console}, ={}, Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml), Filter=null)
2021-07-13 16:44:16,144 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
2021-07-13 16:44:16,144 RMI TCP Connection(3)-127.0.0.1 DEBUG createAppenderRef(ref="Console", level="null", Filter=null)
2021-07-13 16:44:16,144 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
2021-07-13 16:44:16,145 RMI TCP Connection(3)-127.0.0.1 DEBUG createAppenderRef(ref="RollingFileDebug", level="null", Filter=null)
2021-07-13 16:44:16,145 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
2021-07-13 16:44:16,145 RMI TCP Connection(3)-127.0.0.1 DEBUG createAppenderRef(ref="RollingFileInfo", level="null", Filter=null)
2021-07-13 16:44:16,146 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
2021-07-13 16:44:16,146 RMI TCP Connection(3)-127.0.0.1 DEBUG createAppenderRef(ref="RollingFileWarn", level="null", Filter=null)
2021-07-13 16:44:16,146 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
2021-07-13 16:44:16,147 RMI TCP Connection(3)-127.0.0.1 DEBUG createAppenderRef(ref="RollingFileError", level="null", Filter=null)
2021-07-13 16:44:16,147 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
2021-07-13 16:44:16,148 RMI TCP Connection(3)-127.0.0.1 DEBUG createLogger(additivity="null", level="DEBUG", includeLocation="null", ={Console, RollingFileDebug, RollingFileInfo, RollingFileWarn, RollingFileError}, ={}, Configuration(E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml), Filter=null)
2021-07-13 16:44:16,149 RMI TCP Connection(3)-127.0.0.1 DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
2021-07-13 16:44:16,150 RMI TCP Connection(3)-127.0.0.1 DEBUG createLoggers(={org.springframework, org.mybatis, root})
2021-07-13 16:44:16,152 RMI TCP Connection(3)-127.0.0.1 DEBUG Configuration XmlConfiguration[location=E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml] initialized
2021-07-13 16:44:16,152 RMI TCP Connection(3)-127.0.0.1 DEBUG Starting configuration XmlConfiguration[location=E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml]
2021-07-13 16:44:16,154 RMI TCP Connection(3)-127.0.0.1 DEBUG Log4j2 ConfigurationScheduler starting 1 threads
2021-07-13 16:44:16,155 RMI TCP Connection(3)-127.0.0.1 DEBUG Started configuration XmlConfiguration[location=E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml] OK.
2021-07-13 16:44:16,156 RMI TCP Connection(3)-127.0.0.1 DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
2021-07-13 16:44:16,156 RMI TCP Connection(3)-127.0.0.1 DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false-1, all resources released: true
2021-07-13 16:44:16,156 RMI TCP Connection(3)-127.0.0.1 DEBUG Appender DefaultConsole-1 stopped with status true
2021-07-13 16:44:16,156 RMI TCP Connection(3)-127.0.0.1 DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@4849a828 OK
2021-07-13 16:44:16,162 RMI TCP Connection(3)-127.0.0.1 DEBUG Registering MBean org.apache.logging.log4j2:type=49a59ff6
2021-07-13 16:44:16,166 RMI TCP Connection(3)-127.0.0.1 DEBUG Registering MBean org.apache.logging.log4j2:type=49a59ff6,component=StatusLogger
2021-07-13 16:44:16,168 RMI TCP Connection(3)-127.0.0.1 DEBUG Registering MBean org.apache.logging.log4j2:type=49a59ff6,component=ContextSelector
2021-07-13 16:44:16,170 RMI TCP Connection(3)-127.0.0.1 DEBUG Registering MBean org.apache.logging.log4j2:type=49a59ff6,component=Loggers,name=
2021-07-13 16:44:16,171 RMI TCP Connection(3)-127.0.0.1 DEBUG Registering MBean org.apache.logging.log4j2:type=49a59ff6,component=Loggers,name=org.springframework
2021-07-13 16:44:16,172 RMI TCP Connection(3)-127.0.0.1 DEBUG Registering MBean org.apache.logging.log4j2:type=49a59ff6,component=Loggers,name=org.mybatis
2021-07-13 16:44:16,174 RMI TCP Connection(3)-127.0.0.1 DEBUG Registering MBean org.apache.logging.log4j2:type=49a59ff6,component=Appenders,name=Console
2021-07-13 16:44:16,175 RMI TCP Connection(3)-127.0.0.1 DEBUG Registering MBean org.apache.logging.log4j2:type=49a59ff6,component=Appenders,name=RollingFileDebug
2021-07-13 16:44:16,175 RMI TCP Connection(3)-127.0.0.1 DEBUG Registering MBean org.apache.logging.log4j2:type=49a59ff6,component=Appenders,name=RollingFileInfo
2021-07-13 16:44:16,175 RMI TCP Connection(3)-127.0.0.1 DEBUG Registering MBean org.apache.logging.log4j2:type=49a59ff6,component=Appenders,name=RollingFileWarn
2021-07-13 16:44:16,175 RMI TCP Connection(3)-127.0.0.1 DEBUG Registering MBean org.apache.logging.log4j2:type=49a59ff6,component=Appenders,name=RollingFileError
2021-07-13 16:44:16,180 RMI TCP Connection(3)-127.0.0.1 DEBUG org.apache.logging.log4j.core.util.SystemClock does not support precise timestamps.
2021-07-13 16:44:16,181 RMI TCP Connection(3)-127.0.0.1 DEBUG Reconfiguration complete for context[name=49a59ff6] at URI E:\web\source\zhaocaifeng\coding\newday\oldday\target\oldday\WEB-INF\classes\log4j2.xml (org.apache.logging.log4j.core.LoggerContext@2abad743) with optional ClassLoader: null
2021-07-13 16:44:16,181 RMI TCP Connection(3)-127.0.0.1 DEBUG Shutdown hook enabled. Registering a new one.
2021-07-13 16:44:16,182 RMI TCP Connection(3)-127.0.0.1 DEBUG LoggerContext[name=49a59ff6, org.apache.logging.log4j.core.LoggerContext@2abad743] started OK.
真正读取log4j配置的代码如下
org.apache.logging.log4j.core.config.ConfigurationFactory
在log4j-core的包中
private Configuration getConfiguration(LoggerContext loggerContext, boolean isTest, String name) {
boolean named = Strings.isNotEmpty(name);
ClassLoader loader = LoaderUtil.getThreadContextClassLoader();
Iterator var6 = getFactories().iterator();
while(true) {
ConfigurationFactory factory;
String prefix;
String[] types;
do {
if (!var6.hasNext()) {
return null;
}
factory = (ConfigurationFactory)var6.next();
prefix = isTest ? "log4j2-test" : "log4j2";
types = factory.getSupportedTypes();
} while(types == null);
String[] var11 = types;
int var12 = types.length;
for(int var13 = 0; var13 < var12; ++var13) {
String suffix = var11[var13];
if (!suffix.equals("*")) {
String configName = named ? prefix + name + suffix : prefix + suffix;
ConfigurationSource source = ConfigurationSource.fromResource(configName, loader);
if (source != null) {
if (!factory.isActive()) {
LOGGER.warn("Found configuration file {} for inactive ConfigurationFactory {}", configName, factory.getClass().getName());
}
return factory.getConfiguration(loggerContext, source);
}
}
}
}
}
调用堆栈
二、使用slf4j+log4j
pom
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.13.3</version>
</dependency>