[ 
https://issues.apache.org/jira/browse/LOG4J2-2211?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16345306#comment-16345306
 ] 

ASF GitHub Bot commented on LOG4J2-2211:
----------------------------------------

Github user garydgregory commented on the issue:

    https://github.com/apache/logging-log4j2/pull/144
  
    @wangzhongkuo: We are a volunteer-based organization, and as such, we look 
for as much help as we can as all of our schedules are busy, as is yours I 
imagine. This is why we ask for contributions and for contributors to ease the 
addition of new features and the inclusion of bug fixes by follow a fairly 
light process to minimize the impact of changes and of time in reviewing 
patches. 
    
    With this in mind, this PR contains no unit tests, and as such will likely 
not get accepted (by me at least.) Do consider:
    
    1. Write a unit test that FAILS, therefore demonstrating that there is a 
problem.
    2. Apply your bug fix
    3. Run the test again and it now PASSES, demonstrating that your fix works.
    4. Run all the unit tests to make sure your fix didn't break something else.
    
    Thank you,
    Gary


> 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)

Reply via email to