You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "wangzhongkuo (JIRA)" <ji...@apache.org> on 2018/01/23 11:45:00 UTC
[jira] [Created] (LOG4J2-2211) MainMapLookup and StrSubstitutor
wangzhongkuo created LOG4J2-2211:
------------------------------------
Summary: MainMapLookup and StrSubstitutor
Key: LOG4J2-2211
URL: https://issues.apache.org/jira/browse/LOG4J2-2211
Project: Log4j 2
Issue Type: Bug
Components: Lookups
Affects Versions: 2.10.0
Environment: java: 1.7
log4j: 2.10.0
os : windows
Reporter: wangzhongkuo
I use the MainMapLookup like the documentation([http://logging.apache.org/log4j/2.x/manual/lookups.html#AppMainArgsLookup),b|http://logging.apache.org/log4j/2.x/manual/lookups.html#AppMainArgsLookup),i]ut the "${main:–file}" doesn't work, it replace the placeholder by default value "-file".
log4j2.xml(log4j2 2.10.0)
{code:java}
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d [%t] [${main:--file}] %-5level: %msg%n%throwable" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.foo" level="DEBUG" />
<Root level="TRACE">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
{code}
im sure i have written the code: MainMapLookup.setMainArguments(args);
my program arguments are : --file foo.txt --verbose -x bar
so i traced the source code, in StrSubstitutor.class line 958, it changes my varName "main:–file" to "main". i can't understand the code : "varName = varNameExpr.substring(0, i);"
{code:java}
if (valueDelimiterMatcher != null) {
final char [] varNameExprChars = varNameExpr.toCharArray();
int valueDelimiterMatchLen = 0;
for (int i = 0; i < varNameExprChars.length; i++) {
// if there's any nested variable when nested variable substitution disabled, then stop resolving name and default value.
if (!substitutionInVariablesEnabled
&& prefixMatcher.isMatch(varNameExprChars, i, i, varNameExprChars.length) != 0) {
break;
}
if ((valueDelimiterMatchLen = valueDelimiterMatcher.isMatch(varNameExprChars, i)) != 0) {
varName = varNameExpr.substring(0, i);
varDefaultValue = varNameExpr.substring(i + valueDelimiterMatchLen);
break;
}
}
}
{code}
then org.apache.logging.log4j.core.lookup.Interpolator.class
{code:java}
@Override
public String lookup(final LogEvent event, String var) {
if (var == null) {
return null;
}
final int prefixPos = var.indexOf(PREFIX_SEPARATOR);
if (prefixPos >= 0) {
final String prefix = var.substring(0, prefixPos).toLowerCase(Locale.US);
final String name = var.substring(prefixPos + 1);
final StrLookup lookup = lookups.get(prefix);
if (lookup instanceof ConfigurationAware) {
((ConfigurationAware) lookup).setConfiguration(configuration);
}
String value = null;
if (lookup != null) {
value = event == null ? lookup.lookup(name) : lookup.lookup(event, name);
}
if (value != null) {
return value;
}
var = var.substring(prefixPos + 1);
}
if (defaultLookup != null) {
return event == null ? defaultLookup.lookup(var) : defaultLookup.lookup(event, var);
}
return null;
}
{code}
in the red mark, var="main" and prefixPos=-1. so it return the default value "-file";
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)