11

I am reading the configuration part of Log4j2. http://logging.apache.org/log4j/2.x/manual/configuration.html

<Appenders>
    <Console name="STDOUT">
      <PatternLayout pattern="%m%n"/>
    </Console>
    <List name="List">
      <ThresholdFilter level="debug"/>
    </List>
    <Routing name="Routing">
      <Routes pattern="$${sd:type}">
        <Route>
          <RollingFile name="Rolling-${sd:type}" fileName="${filename}"
                       filePattern="target/rolling1/test1-${sd:type}.%i.log.gz">
            <PatternLayout>
              <pattern>%d %p %c{1.} [%t] %m%n</pattern>
            </PatternLayout>
            <SizeBasedTriggeringPolicy size="500" />
          </RollingFile>
        </Route>
        <Route ref="STDOUT" key="Audit"/>
        <Route ref="List" key="Service"/>
      </Routes>
    </Routing>
  </Appenders>

What is the meaning of double $$ sign? e.g. $${sd:type}?

user3544765
  • 456
  • 5
  • 13

2 Answers2

9

It seems that $ is used as an escape character. As stated in Log4J documentation, Log4j configuration file parser uses Apache Commons Lang's StrSubstitutor, and this documentation for StrSubstitutor says:

The other possibility is to use the escape character, by default '$'. If this character is placed before a variable reference, this reference is ignored and won't be replaced. For example:

The variable $${${name}} must be used.

I guess they want to set the value to "${sd:type}" so that this variable can be evaluated later at run-time. There is a good example/explanation here: http://logging.apache.org/log4j/2.x/manual/lookups.html#ContextMapLookup

xav
  • 5,452
  • 7
  • 48
  • 57
  • Yes, you are correct. According to Lookup menu, http://logging.apache.org/log4j/2.x/manual/lookups.html, it mentions that "During initial configuration processing the first '$' will be removed" – user3544765 Sep 09 '16 at 06:44
0

You can set Configuration element's attribute status="DEBUG" like this:

<Configuration status="DEBUG">  
<properties>
        <property name="LOG_HOME">logs</property>
</properties>

Then record log follow the two step:

step 1:

Set filePattern="${LOG_HOME}/${date:yyyy-MM}/all-%d{yyyy-MM-dd}.log.gz"

log recored:
...
filePattern="logs/2021-06/all-%d{yyyy-MM-dd}.log.gz"
...

step 2:

Set filePattern="${LOG_HOME}/$${date:yyyy-MM}/all-%d{yyyy-MM-dd}.log.gz"

log recored:
...
filePattern="logs/${date:yyyy-MM}/all-%d{yyyy-MM-dd}.log.gz"
...

Summary:

Now you know their difference.Double $ means the result will be computed dynamically according the latter context.It's a placeholder for dynamically compute.