我们在Log4J的使用中,偶尔会发生这样的场景:我们明明已经配置了log4j.properties文件 但是有时候就不按预期输出日志。
归其原因,有两种:
1.多种依赖jar包相互干扰。
2.其他的配置文件被加载覆盖了你自己的配置。
对于场景一:可以使用maven的dependency:tree查看依赖的jar包排除相关传递依赖就可以了
对于场景二: 可以打开Log4j的调试模式,运行程序然后通过日志确定加载的配置文件
System.setProperty("log4j.debug","true");
LogLog.setQuietMode(false);
Connected to the target VM, address: '127.0.0.1:51578', transport: 'socket'
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@18b4aac2.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@18b4aac2 class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@18b4aac2.
log4j: Using URL [file:/Users/shangkuidu/code/Cloud/dcom/alibaba-push-web/target/classes/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:/Users/shangkuidu/code/Cloud/dcom/alibaba-push-web/target/classes/log4j.properties
log4j: Parsing for [root] with value=[DEBUG,Console,syslog].
log4j: Level token is [DEBUG].
log4j: Category root set to DEBUG
log4j: Parsing appender named "Console".
log4j: Parsing layout options for "Console".
log4j: Setting property [conversionPattern] to [[%-d{yyyy-MM-dd HH:mm:ss}][%p][%t]%l-%m%n].
log4j: End of parsing for "Console".
log4j: Setting property [immediateFlush] to [true].
log4j: Setting property [target] to [System.out].
log4j: Parsed "Console" options.
log4j: Parsing appender named "syslog".
log4j: Parsing layout options for "syslog".
log4j: Setting property [conversionPattern] to [[%p][%-d{yyyy-MM-dd HH:mm:ssS}][%t]%l-%m%n].
log4j: End of parsing for "syslog".
log4j: Setting property [encoding] to [utf-8].
log4j: Setting property [file] to [logs/syslog.log].
log4j: Setting property [immediateFlush] to [true].
log4j: Setting property [append] to [true].
log4j: Setting property [datePattern] to ['.'yyyy-MM-dd].
log4j: Setting property [threshold] to [ERROR].
log4j: setFile called: logs/syslog.log, true
log4j: setFile ended
log4j: Appender [syslog] to be rolled at midnight.
log4j: Parsed "syslog" options.
log4j: Parsing for [net.sf.hibernate.cache] with value=[ERROR].
log4j: Level token is [ERROR].
log4j: Category net.sf.hibernate.cache set to ERROR
log4j: Handling log4j.additivity.net.sf.hibernate.cache=[null]
log4j: Finished configuring.
[2020-12-09 10:42:14][INFO][main]com.iwhalecloud.dcom.server.AlibabaPushWebApplication.main(AlibabaPushWebApplication.java:60)-initing
打成jar包后我们发现:
dushangkui:~ shangkuidu$ cd /Users/shangkuidu/code/Cloud/dcom/alibaba-push-web/target/
dushangkui:target shangkuidu$ java -jar alibaba-push-web-0.0.1-SNAPSHOT.jar
log4j: Trying to find [log4j.xml] using context classloader org.springframework.boot.loader.LaunchedURLClassLoader@6504e3b2.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@42a57993 class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader org.springframework.boot.loader.LaunchedURLClassLoader@6504e3b2.
log4j: Using URL [jar:file:/Users/shangkuidu/code/Cloud/dcom/alibaba-push-web/target/lib/opentsdb-client-3.1.jar!/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL jar:file:/Users/shangkuidu/code/Cloud/dcom/alibaba-push-web/target/lib/opentsdb-client-3.1.jar!/log4j.properties
log4j: Parsing for [root] with value=[INFO,console].
log4j: Level token is [INFO].
log4j: Category root set to INFO
log4j: Parsing appender named "console".
log4j: Parsing layout options for "console".
log4j: Setting property [conversionPattern] to [[%-5p] %d(%r) --> [%t] %l: %m %x %n].
log4j: End of parsing for "console".
log4j: Setting property [immediateFlush] to [true].
log4j: Setting property [threshold] to [INFO].
log4j: Setting property [target] to [System.out].
log4j: Parsed "console" options.
log4j: Finished configuring.
[INFO ] 2020-12-09 10:43:52,456(0) --> [main] com.iwhalecloud.dcom.server.AlibabaPushWebApplication.main(AlibabaPushWebApplication.java:60): initing
[ERROR] 2020-12-09 10:43:52,458(2) --> [main] com.iwhalecloud.dcom.server.AlibabaPushWebApplication.main(AlibabaPushWebApplication.java:61): inited
[INFO ] 2020-12-09 10:43:52,691(235) --> [background-preinit] org.hibernate.validator.internal.util.Version.<clinit>(Version.java:21): HV000001: Hibernate Validator 6.0.17.Final
从日志我们很明显的看到加载的配置文件不是我们自己的
log4j: Using URL [jar:file:/Users/shangkuidu/code/Cloud/dcom/alibaba-push-web/target/lib/opentsdb-client-3.1.jar!/log4j.properties] for automatic log4j configuration.
如果确定了配置被覆盖或者加载的不是自己的配置可以通过以下方式,重新加载配置
URL url = AlibabaPushWebApplication.class.getResource("/config/log4j.properties");
PropertyConfigurator.configure(url);