英文:
Log4j2 add to layout if user logged in
问题
如果用户未经过身份验证,日志将如下所示:
{
"value1": "这是我们的值",
"map": {
"key1": "value1",
"key2": "value2"
},
"something": "其他内容"
}
如果用户已经身份验证,日志将如下所示:
{
"value1": "这是我们的值",
"map": {
"key1": "value1",
"key2": "value2"
},
"user": {
"name": "名称",
"client": "某个值"
},
"something": "其他内容"
}
有没有一种方法可以实现这个需求?
英文:
I wish to add or append to a PatternLayout in log4j2 depending if the user is authenticated or not.
If the user is not authenticated the log would look like this:
{
"value1": "Here is our values",
"map": {
"key1": "value1",
"key2": "value2"
},
"something": "something else"
}
And if the user is authenticated I would get something like this:
{
"value1": "Here is our values",
"map": {
"key1": "value1",
"key2": "value2"
},
"user": {
"name": "name",
"client": "some value"
},
"something": "something else"
}
Is there some way of achieving this?
答案1
得分: 1
一个实现这一目标的方法是使用 Log4j2 的 PatternSelector。目前,Log4j 2 支持 LevelPatternSelector、MarkerPatternSelector 和 ScriptPatternSelector。很可能您真正想要的是类似 ThreadContextPatternSelector 的东西,它可以基于 ThreadContext 中的键的存在选择模式。但是,在有人提出这个请求之前,您可以使用 ScriptPatternSelector 来实现相同的功能,做法如下:
<PatternLayout>
<ScriptPatternSelector defaultPattern="[%-5level] %c{1.} %C{1.}.%M.%L %msg%n">
<Script name="BeanShellSelector" language="bsh"><![CDATA[
if (logEvent.getContextMap().containsKey("client")) {
return "Client";
}
return null;
]]>
</Script>
<PatternMatch key="Client" pattern="{ ... \"user\": { \"name\": \"${mdc:name}\", \"client\": \"${mdc:client}\"}, ..."/>
</ScriptPatternSelector>
</PatternLayout>
如果您从 OAuth 令牌获取客户端信息,您可以创建一个自定义的 PatternSelector,从 Spring SecurityContext 获取该信息,并返回要应用的模式的键,或返回 null 以使用默认模式。要创建自定义插件,您可以查看 LevelPatternSelector 或 MarkerPatternSelector 作为示例。
英文:
One way to achieve this is to use a PatternSelector with Log4j2. Currently Log4j 2 supports a LevelPatternSelector, MarkerPatternSelector, and ScriptPatternSelector. More than likely you would really like something like a ThreadContextPatternSelector that could select a pattern based on the presence of a key in the ThreadContext. However, until someone requests that you could implement the same thing using the ScriptPatternSelector doing something like:
<PatternLayout>
<ScriptPatternSelector defaultPattern="[%-5level] %c{1.} %C{1.}.%M.%L %msg%n">
<Script name="BeanShellSelector" language="bsh"><![CDATA[
if (logEvent.getContextMap().containsKey("client")) {
return "Client";
}
return null;
]]>
</Script>
<PatternMatch key="Client" pattern="{ ... \"user\": { \"name\": \"${mdc:name}\", \"client\": \"${mdc:client}\"}, ..."/>
</ScriptPatternSelector>
</PatternLayout>
If you are obtaining the client information from an OAuth token you could create a custom PatternSelector that gets it from the Spring SecurityContext and returns the key of the pattern to apply or null to use the default pattern. To create a custom plugin you can look at either the LevelPatternSelector or the MarkerPatternSelector as examples.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论