You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "ASF GitHub Bot (Jira)" <ji...@apache.org> on 2020/02/16 07:46:00 UTC

[jira] [Work logged] (LOG4J2-2211) MainMapLookup ${main:--file} placeholder doesn't work

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

ASF GitHub Bot logged work on LOG4J2-2211:
------------------------------------------

                Author: ASF GitHub Bot
            Created on: 16/Feb/20 07:45
            Start Date: 16/Feb/20 07:45
    Worklog Time Spent: 10m 
      Work Description: rgoers commented on pull request #145: fix bug LOG4J2-2211 
URL: https://github.com/apache/logging-log4j2/pull/145
 
 
   
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 388042)
    Time Spent: 40m  (was: 0.5h)

> 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
>             Fix For: 2.13.1
>
>          Time Spent: 40m
>  Remaining Estimate: 0h
>
> 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
(v8.3.4#803005)