user3544765 user3544765 - 5 months ago 43
Java Question

log4j2 double dollar $$ sign meaning in configuration

I am reading the configuration part of Log4j2.

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

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

xav xav

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: