Log4j2 如果用户已登录,则添加到布局中

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

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.

huangapple
  • 本文由 发表于 2020年8月17日 14:06:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63445394.html
匿名

发表评论

匿名网友

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

确定