log4j2:运行时重新配置日志记录器的问题

huangapple go评论81阅读模式
英文:

log4j2 : Problem with reconfiguration of the loggers during runtime

问题

这与 log4j2 版本 2.13.2 有关。

我们正在使用一个带有自己的日志记录器配置文件的 API,它称为 log4j2_api.xml
作为应用程序开发人员,我们在类路径中有自己的 log4j2.xml

我们正在使用的 API 在内部从 log4j2_api.xml 加载 log4j2 实体(日志记录器和附加器)。
似乎当这种情况发生时,我们自己的来自 log4j2.xml 的配置被 log4j2_api.xml 覆盖,我们的应用程序不再产生日志。

以下是一系列事件的简要 事件序列 -

  1. 应用程序启动并从类路径加载 log4j2.xml
  2. 应用程序按预期进行适当的日志记录。
  3. 第一个 API 调用发生。内部加载了 log4j2_api.xml,并根据其配置进行 API 日志记录。
  4. 应用程序从 API 接收所需的数据并继续进行,但没有所需的日志记录。

我的期望是 - 如果在新配置中找到了预加载(通过初始配置)的日志记录器/附加器,它们应该被更新,其他不应该被触碰。当然,新配置的日志记录器/附加器应该被添加。

这个理解是否正确?

提前感谢。

配置文件 -

log4j2.xml

<Configuration status="warn">
	<Appenders>
		<RollingFile name="applicationAppender" fileName="sample-log-executor-2008.log" filePattern="sample-log-executor-2008-%i.log">		
			<PatternLayout>
				<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
			</PatternLayout>
			
			<Policies>
				<SizeBasedTriggeringPolicy size="5 MB" />
			</Policies>
		</RollingFile>
		
		<Console name="console_window" target="SYSTEM_OUT">
			<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
		</Console>
	</Appenders>
	
	<Loggers>
		<Logger name="com.abc.samplecode" level="trace" additivity="false">
			<appender-ref ref="applicationAppender" level="trace" />
		</Logger>

		<Root level="error" additivity="false">
			<appender-ref ref="console_window" />
		</Root>
	</Loggers>
</Configuration>

log4j_api.xml

<Configuration>
	<Appenders>
		<Console name="Console" target="SYSTEM_OUT">
			<PatternLayout
				pattern="%d %-5p [%t] %C (%F:%L) - %m%n" />
		</Console>
		
		<RollingFile name="xyzAPIAppender" fileName="xyzAPILog.log"
			filePattern="xyzAPILog-%i.log">
			<PatternLayout
				pattern="%d{MMM dd yyyy HH:mm:ss.SSS} %-5p [%t] (%27F:%-5L) - %m%n" />
			<Policies>
				<SizeBasedTriggeringPolicy size="5 MB" />
			</Policies>
			<DefaultRolloverStrategy max="10" />
		</RollingFile>
	</Appenders>
	
	<Loggers>
		<logger name="com.xyz.api" level="warn"
			additivity="false">
			<AppenderRef ref="xyzAPIAppender" />
		</logger>
	</Loggers>
</Configuration>
英文:

This is related to log4j2 version 2.13.2.

We are consuming an API which comes with it's own logger configuration file say - log4j2_api.xml.
As an application developer, we have our own log4j2.xml in the classpath.

The API we are consuming is internally loading log4j2 entities (loggers and appenders) from log4j2_api.xml.
It looks like when that happens, our own configuration from log4j2.xml gets overwritten by log4j2_api.xml and our application produces no further logs.

Here is a brief sequence of events -

  1. Application starts and log4j2.xml gets loaded from classpath.
  2. Application proceeds with appropriate logging as expected.
  3. First API call happens. Internally log4j2_api.xml gets loaded and API logging happens as per it's configuration.
  4. Application receives the needed data from the API and proceeds further, but without desired logging.

My exepectation here is - if pre-loaded ( by initial configuration) loggers/appenders are found in the new configuration, they should be updated and other ones should remain untouched. And ofcourse, the newly configured loggers/appenders should get added.

Is this understanding correct?

Thanks in advance.

Configuration Files -

log4j2.xml

<Configuration status="warn">
	<Appenders>
		<RollingFile name="applicationAppender" fileName="sample-log-executor-2008.log" filePattern="sample-log-executor-2008-%i.log">		
			<PatternLayout>
				<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
			</PatternLayout>
			
			<Policies>
				<SizeBasedTriggeringPolicy size="5 MB" />
			</Policies>
		</RollingFile>
		
		<Console name="console_window" target="SYSTEM_OUT">
			<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
		</Console>
	</Appenders>
	
	<Loggers>
		<Logger name="com.abc.samplecode" level="trace" additivity="false">
			<appender-ref ref="applicationAppender" level="trace" />
		</Logger>

		<Root level="error" additivity="false">
			<appender-ref ref="console_window" />
		</Root>
	</Loggers>
</Configuration>

log4j_api.xml

<Configuration>
	<Appenders>
		<Console name="Console" target="SYSTEM_OUT">
			<PatternLayout
				pattern="%d %-5p [%t] %C (%F:%L) - %m%n" />
		</Console>
		
		<RollingFile name="xyzAPIAppender" fileName="xyzAPILog.log"
			filePattern="xyzAPILog-%i.log">
			<PatternLayout
				pattern="%d{MMM dd yyyy HH:mm:ss.SSS} %-5p [%t] (%27F:%-5L) - %m%n" />
			<Policies>
				<SizeBasedTriggeringPolicy size="5 MB" />
			</Policies>
			<DefaultRolloverStrategy max="10" />
		</RollingFile>
	</Appenders>
	
	<Loggers>
		<logger name="com.xyz.api" level="warn"
			additivity="false">
			<AppenderRef ref="xyzAPIAppender" />
		</logger>
	</Loggers>
</Configuration>

答案1

得分: 1

我怀疑您的类路径中缺少log4j2核心jar包。

请参阅以下链接以获取更多详细信息:
http://logging.apache.org/log4j/2.x/faq.html#which_jars

英文:

I suspect you are missing log4j2 core jar in your class path.

Please see the arch for more details
http://logging.apache.org/log4j/2.x/faq.html#which_jars

答案2

得分: 1

根据您的描述,您正在使用的库在调用Log4j时会使用自己的配置进行配置。这是非常不礼貌的,因为它导致了您遇到的问题。当它加载自己的配置时,它会重新配置Log4j,因此您的配置被删除了。

您需要获取有关您调用的库的更多信息,并找出如何让它停止重新初始化Log4j。

英文:

From what you are describing the library you are consuming is calling Log4j to configure with its own configuration. This is very rude as it causes the problem you are running into. When it loads its configuration it is reconfiguring Log4j so your configuration is being removed.

You need to get more information on the library you are calling and find out how to get it to stop reinitializing Log4j.

huangapple
  • 本文由 发表于 2020年8月7日 02:27:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63289661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定