gpt4 book ai didi

How to use the Log4j2 SpringProfile arbiter to alter a property value(如何使用Log4j2 SpringProfile仲裁器更改属性值)

转载 作者:bug小助手 更新时间:2023-10-25 11:49:31 35 4
gpt4 key购买 nike



According to the docs it should be possible to use the SpringProfile arbiter to choose whether a given "node" is included, based on the active Spring Boot profile; so I assumed this should work:

根据文档,应该可以使用SpringProfile仲裁器根据活动的Spring Boot配置文件来选择是否包括给定的“节点”;所以我假设这应该是可行的:


    <Properties>
<SpringProfile name="dev">
<Property name="logdir" value="/tmp/log"/>
</SpringProfile>
<SpringProfile name="docker">
<Property name="logdir" value="/var/log"/>
</SpringProfile>
<Property name="logfile" value="users.log"/>
</Properties>

However, this does not appear to configure the property (at all) as expected (although, in a different part of my configuration, this works):

然而,这似乎根本没有按照预期配置该属性(尽管,在我的配置的不同部分中,这是可行的):


    <Loggers>
<!-- LOG everything at INFO level, unless the `debug` Spring Profile is used -->
<SpringProfile name="debug">
<Root level="debug">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingFile"/>
</Root>
</SpringProfile>
<SpringProfile name="!debug">
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingFile"/>
</Root>
</SpringProfile>
</Loggers>

My guess is that either (a) I'm doing it wrong in the Properties or (b) somehow the Properties nodes are not affected by the SpringProfile arbiter.

我的猜测是(A)我在Properties中做错了,或者(B)Properties节点不知何故不受SpringProfile仲裁器的影响。


I am aware that I could just duplicate all the Appender configuration to make it do what I want (or use different files, for that matter); but it seems... well, wasteful - all I want is a different directory for my logs, based on the Spring Profile.

我知道我可以只复制所有的附加器配置,让它做我想做的事情(或者使用不同的文件);但看起来……嗯,太浪费了--我想要的只是一个不同的目录来存放我的日志,基于Spring配置文件。


Please note that I am aware of all the possible weird and wonderful ways in which this can be otherwise accomplished; here, I'm only interested in whether I can influence the value of Log4j Property via a SpringProfile arbiter - many thanks for any advice you may have!

请注意,我知道所有可能的奇怪和奇妙的方式,否则可以实现这一点;在这里,我只感兴趣的是我是否可以通过SpringProfile仲裁器影响Log4j属性的价值-非常感谢您的任何建议!


更多回答
优秀答案推荐

You can freely use SpringProfile inside a <Properties> node, but you need to remember that a log4j2.xml configuration file is used twice during the lifecycle of a Spring Boot application:

您可以在 节点内自由使用SpringProfile,但您需要记住,在Spring Boot应用程序的生命周期中,会使用两次log4j2.xml配置文件:



  • Log4j Core is initialized almost as soon as your application starts. At this time no Spring Environment is available and ${logdir} expands to ${logdir},

  • as soon as the ApplicationEnvironmentPreparedEvent event is triggered, Spring Boot reconfigures Log4j Core again. From this moment ${logdir} will expand properly.


Therefore you also need to set a value for logdir that will be used in the initial phase. Due to issue #1783, the cleanest solution does not work:

因此,您还需要为将在初始阶段使用的logdir设置一个值。由于问题#1783,最干净的解决方案不起作用:


<Select>
<SpringProfile name="dev">
<Property name="logdir" value="/tmp/log"/>
</SpringProfile>
<SpringProfile name="docker">
<Property name="logdir" value="/var/log"/>
</SpringProfile>
<DefaultArbiter>
<Property name="logdir" value="..."/>
</DefaultArbiter>
</Select>

but as a workaround you can use:

但作为一种解决办法,您可以使用:


<Property name="logdir" value="..."/>
<SpringProfile name="dev">
<Property name="logdir" value="/tmp/log"/>
</SpringProfile>
<SpringProfile name="docker">
<Property name="logdir" value="/var/log"/>
</SpringProfile>

and ignore the error messages.

并忽略错误消息。


Remark: you can also switch to two configuration files:

备注:您也可以切换到两个配置文件:



  • a log4j2.xml for the startup phase of your application (no SpringProfiles allowed),

  • a log4j2-spring.xml for the runtime phase of your application.


更多回答

Thanks, @piotr - appreciated. In the end, I switched back to logback: the effort of using log4j2 didn't really justified. One thing that is (somewhat) annoying is that the documentation, while explictly describing the use of SpringProfile entirely ignores this issue, which then leads to a lot of wasted time.

“谢谢,”皮奥特--很感激。最后,我切换回Logback:使用log4j2的努力并不真正合理。令人(有些)恼火的是,文档虽然明确地描述了SpringProfile的使用,但完全忽略了这个问题,这会导致大量时间的浪费。

@MarcoMassenzio: thanks for confirming that documentation is our Achilles' heel. In the near future we'll spend a lot of time improving it.

@MarcoMassenzio:感谢您确认文档是我们的致命弱点。在不久的将来,我们将花费大量时间来改进它。

also, for the record, you guys are awesome :) I love OSS (and when time allows, I try to either contribute or publish my own code as Apache 2 licensed). Documentation is hard! I don't really think that we (as an industry) have really cracked the problem, although now LLMs may pave the way to have the machine do the tedious work! Thanks again, this is really appreciated <3

另外,为了记录在案,你们真的很棒:)我喜欢OSS(当时间允许的时候,我会尝试贡献或发布我自己的代码作为Apache 2许可)。文档很难!我真的不认为我们(作为一个行业)已经真正解决了这个问题,虽然现在LLM可能铺平了道路,让机器做繁琐的工作!再次感谢,真的很感谢<3

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com