最近在做项目的时候,log4j的配置文件是log4j.properties。在做单元测试的时候,控制台出现一串难看的报错信息“ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.”
然后去查资料发现,log4j2只支持xml和json两种格式的配置,所以配置log4j.properties是没有作用的。
log4j的2.x版本在默认情况下,系统选择configuration文件的优先级如下
1.classpath下名为log4j-test.json或者log4j-test.jsn文件
2.classpath下名为log4j2-test.xml
3.classpath下名为log4j.json或者log4j.jsn文件
4.classpath下名为log4j2.xml
所以我把上面的log4j.properties的文件改为log4j2.xml文件,示例如下:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
</Console>
<RollingFile name="RollingFile" filename="log/test.log"
filepattern="${logPath}/%d{YYYYMMddHHmmss}-fargo.log">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>