Open vy opened 5 months ago
To be more precise, the StrLookup
parses the string ${env:TZ:-%d{yyyy-MM-dd HH:mm:ss}{GMT+00}}
as:
${env:TZ:-%d{yyyy-MM-dd HH:mm:ss}
), where env:TZ
is the key used for the lookup and %d{yyyy-MM-dd HH:mm:ss}
is the default value.{GMT+00}}
.The main problem here is to have a coherent set of escape rules for lookup expressions and pattern expressions. Right now we have the following escaping rules:
${
can be escaped as $${
. This only works for the first level of lookups. If ${foo}
expands to bar
, then $${foo}
is not expanded, but ${$${foo}}
returns the expansion of ${$bar}
.:-
can be escaped as :\-
, but if \-
appears anywhere else, it will give two characters.%d
can be escaped as %%d
.}
neither in a lookup nor in the options of a pattern expression.This resembles more the way cmd.exe
parses a command line than the simpler bash
expansion.
Hi @vy , @ppkarwasz
I have a PR but I think this has more edge case. Please take a look and tell me if I miss anything
Looking at bash parameter expansion, I would propose to just introduce a simple escape mechanism that allows users to escape the closing }
as \}
.
The initial example would then need to be written as:
<PatternLayout pattern="${env:TZ:-%d{yyyy-MM-dd HH:mm:ss\}{GMT+00\}}"/>
This does not make the escaping rules coherent, but at least they are simpler:
${
is escaped as $${
,:-
is escaped as :\-
,}
is escaped as \}
.Using \
as a generic escaping mechanism is not possible for backward compatibility, since the default replacement could be a Windows path.
Note: Currently StrLookup
allows nested lookups anywhere between ${
and the closing }
. I would prefer to limit them to the default value part.
So ${foo:-${bar}}
would be allowed, but ${${foo}${bar}}
wouldn't. I really don't see a reason why the names to lookup should be compose by other lookups.
After discussing in with @vy on Slack, I believe we should coherently use $
as escape character, with the following rules:
$
only applies to the $
, :
, -
and }
characters.$${...}
is not expanded,${sys$:-foo}
and ${sys:$-foo}
are replaced with the value of the system property -foo
,${foo:-$}}
is expanded as }
if foo
is not defined.I am looking forward to log4j2 new version, so many things have to done :D
As reported in #2666,
leaves behind a trailing
}
while formatting date. That is,StrSubstitutor
doesn't correctly parse nested braces.