[英]Is it possible to filter key-value pairs of a ThreadContextMap with a Regex?
Hi all & Happy New Year!大家好,新年快乐!
I wish to filter certain logs if the "env" key in the ThreadContextMap
has a value of prod1
or qa1
.如果ThreadContextMap
中的“env”键的值为prod1
或qa1
,我希望过滤某些日志。 I already have something like the following set up:我已经有类似下面的设置:
<Console name="prodOutput" target="SYSTEM_OUT">
<PatternLayout pattern="..."/>
<Filters>
<ThreadContextMapFilter onMatch="DENY" onMismatch="NEUTRAL" operator="or">
<KeyValuePair key="ENV" value="qa1"/>
<KeyValuePair key="ENV" value="prod1"/>
</ThreadContextMapFilter>
</Filters>
</Console>
But this code "hardcodes" the values prod1
& qa1
.但是这段代码“硬编码”了值prod1
& qa1
。 In spirit of making the code extensible, I wanted to filter out those logs if they match the regex of "prod\d+"
or "qa\d+"
.本着使代码可扩展的精神,我想过滤掉那些匹配正则表达式"prod\d+"
或"qa\d+"
的日志。
Values that would match these regex's include "prod1"
, "qa2"
.与这些正则表达式匹配的值包括"prod1"
、 "qa2"
。
This is because we don't want those certain logs to appear on production & qa environments这是因为我们不希望那些特定的日志出现在生产和质量检查环境中
In the ThreadContextMap
, the key-value pairs we're checking for is ENV
- prod1
or ENV
- qa1
.在ThreadContextMap
中,我们要检查的键值对是ENV
- prod1
或ENV
- qa1
。
I tried searching on StackOverflow as well as the log4j2 documentation but there doesn't seem to be any mention of how such a thing can be done.我尝试在 StackOverflow 和 log4j2 文档上进行搜索,但似乎没有提及如何完成这样的事情。
Would anyone have any ideas on how I can approach this?有人对我如何解决这个问题有任何想法吗? Thanks for any inputs:) Am open to alternative ideas as well!感谢您的任何投入:)我也对其他想法持开放态度!
I managed to find a workaround solution, where I instead apply the regex to the value being inserted into the ThreadContextMap
我设法找到了一个变通解决方案,我改为将正则表达式应用于插入到ThreadContextMap
中的值
This way, I only ever have to check for 2 values in the ThreadContextMapFilter
, prod
& qa
这样,我只需要检查ThreadContextMapFilter
、 prod
和qa
中的 2 个值
This solution is satisfactory enough as it works & meets the requirements we had.这个解决方案足够令人满意,因为它可以工作并满足我们的要求。 For those curious, it looks something like this:对于那些好奇的人,它看起来像这样:
@Value("${env}")
private String environment;
...
// check environment against regex
boolean matchesProdRegex = Pattern.matches(".*prod.*", this.environment);
boolean matchesQaRegex = Pattern.matches(".*qa.*", this.environment);
if (matchesProdRegex ) {
ThreadContext.put("ENV", "prod");
} else if (matchesQaRegex ) {
ThreadContext.put("ENV", "qa");
} else {
ThreadContext.put("ENV", this.environment);
}
Consequently in log4j2.xml
, the ThreadContextMapFilter
looks something like:因此在log4j2.xml
中, ThreadContextMapFilter
看起来像这样:
<Console name="prodOutput" target="SYSTEM_OUT">
<PatternLayout pattern="..."/>
<Filters>
<ThreadContextMapFilter onMatch="DENY" onMismatch="NEUTRAL" operator="or">
<KeyValuePair key="ENV" value="qa"/>
<KeyValuePair key="ENV" value="prod"/>
</ThreadContextMapFilter>
</Filters>
</Console>
Thanks to anyone that took the time to read my post & hope this can be a useful reference for anyone in the future, cheers!感谢所有花时间阅读我的帖子的人,希望这对以后的任何人都有用,干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.