You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Volkan Yazici (Jira)" <ji...@apache.org> on 2022/11/08 20:56:00 UTC

[jira] [Commented] (LOG4J2-3629) No properties lookup from JsonTemplateLayout pattern resolver?

    [ https://issues.apache.org/jira/browse/LOG4J2-3629?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17630618#comment-17630618 ] 

Volkan Yazici commented on LOG4J2-3629:
---------------------------------------

Thanks for the report and reaching out to us, [~tgr]!

h3. What is wrong with the lookup usage in the reported template?

As you also indicated, JTL indeed passes the JSON object intact to the {{ExceptionResolver}}, which doesn't perform any lookups. This behaviour exhibits itself in all existing resolvers. A naive approach would be, after parsing the template JSON and before compiling resolvers, visiting all nodes to perform necessary lookup substitutions. Though this wouldn't fix the issue for dynamic (that is, yielding a different output depending on the instant or {{LogEvent}}) lookups, consider the following template:

{code:json}
{
  "$resolver": "pattern",
  "pattern": "LogEvent-dependent lookup: ${mdc:userId}, dynamic-lookup: ${date:yyyyMMdd}"
}
{code}

If -Log4Shell- lookups have taught us one thing, they are more sophisticated than meets the eye, hence it is wise to keep their integration simple, which is the way it is right now in JTL.

h3. Is there any other way to pass the pattern property to JTL?

I cannot think of a solution from the top of my head right now. You can either implement your own event resolver and use it, or intercept the template resolver compiler to perform the lookup substitution yourself, both of which are [documented|https://logging.apache.org/log4j/2.x/manual/json-template-layout.html].

h3. _"%xEx pattern seems more customizable"_...

In your filtering case, I need to disagree with you. Have you checked the truncation support in stack traces? Doesn't it address your case?

{code:json}
{
  "$resolver": "exception",
  "field": "stackTrace",
  "stackTrace": {
    "stringified": {
      "truncation": {
        "suffix": "... [truncated]",
        "pointMatcherStrings": [
          "at org.junit",
          "at org.apache.maven",
          "at sun.reflect",
          "at java.lang.reflect"
        ]
      }
    }
  }
}
{code}

> No properties lookup from JsonTemplateLayout pattern resolver?
> --------------------------------------------------------------
>
>                 Key: LOG4J2-3629
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-3629
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: JsonTemplateLayout
>    Affects Versions: 2.19.0
>            Reporter: Tobias Gruetzmacher
>            Priority: Minor
>              Labels: pattern
>
> This is a slightly similar issue to LOG4J2-3367:
> I want to be able to "share" some pattern settings between a line-based logger and a JSON logger, so I created a naive config like this (both logging to console just for demo purposes):
>  
> *log4j2.xml*
> {code:java}
> <?xml version="1.0" encoding="UTF-8"?>
> <Configuration name="Demo" status="debug" monitorInterval="60">
>   <Properties>
>     <Property name="exFilters">org.junit,org.apache.maven,sun.reflect,java.lang.reflect</Property>
>     <Property name="exPattern">%xEx{filters(${exFilters})}</Property>
>     <Property name="logPattern">%d{ISO8601} %-5p [%c{1}] %m%n${exPattern}</Property>
>   </Properties>
>   <Appenders>
>     <Console name="Json">
>       <JsonTemplateLayout eventTemplateUri="classpath:MyLayout.json" />
>     </Console>
>     <Console name="Plain">
>       <PatternLayout pattern="${logPattern}" />
>     </Console>
>   </Appenders>
>   <Loggers>
>     <Root level="info">
>       <AppenderRef ref="Json" />
>       <AppenderRef ref="Plain" />
>     </Root>
>   </Loggers>
> </Configuration>{code}
> *MyLayout.json*
> {code:java}
> {
>   "msg": {
>     "$resolver": "message"
>   },
>   "thrown": {
>     "stackTrace": {
>       "$resolver": "pattern",
>       "pattern": "${exPattern}"
>     }
>   }
> } {code}
> This doesn't work (probably because the lookup is only done at logging time, not at configuration time), but I'm wondering if this (or something similar) could be implemented somehow...
> Yes, I'm aware that {{ExceptionResolver}} provides something similar, but it's not quite the same as the {{%xEx}} pattern, which seems to be a lot more customizable...



--
This message was sent by Atlassian Jira
(v8.20.10#820010)