[
https://issues.apache.org/jira/browse/LOG4J2-2211?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16337587#comment-16337587
]
wangzhongkuo commented on LOG4J2-2211:
--------------------------------------
```java
/**
* Resolves the specified variable. This implementation will try to extract
* a variable prefix from the given variable name (the first colon (':') is
* used as prefix separator). It then passes the name of the variable with
* the prefix stripped to the lookup object registered for this prefix. If
* no prefix can be found or if the associated lookup object cannot resolve
* this variable, the default lookup object will be used.
*
* @param event The current LogEvent or null.
* @param var the name of the variable whose value is to be looked up
* @return the value of this variable or <b>null</b> if it cannot be
* resolved
*/
@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;
}
```
in the method above, only if the var="main:–file", it can find the
MainMapLookup and it't map contians all of the program arguments. e.g.
\{0=--file, foo.txt=--verbose, 1=foo.txt, 2=--verbose, bar=null, 3=-x, 4=bar,
--file=foo.txt, --verbose=-x, -x=bar}
but the code "varName = varNameExpr.substring(0, i);" in
org.apache.logging.log4j.core.lookup.StrSubstitutor.substitute(LogEvent,
StringBuilder, int, int, List<String>) reassign it "main", so can't find the
matched lookup but defaultLookup, but defaultLookup's map does not cotains the
params "–file=foo.txt,", just return the default value "-file".
> MainMapLookup ${main:--file} placeholder doesn't work
> -----------------------------------------------------
>
> 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
> Priority: Blocker
> Labels: newbie
>
> 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)