Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-2211

MainMapLookup ${main:--file} placeholder doesn't work

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Blocker
    • Resolution: Fixed
    • 2.10.0
    • 2.13.1
    • Lookups
    • java: 1.7
      log4j: 2.10.0

      os : windows

    • Important

    Description

      I use the MainMapLookup like the documentation(http://logging.apache.org/log4j/2.x/manual/lookups.html#AppMainArgsLookup),but the "${main:–file}" doesn't work, it replace the placeholder by default value "-file".

      log4j2.xml(log4j2 2.10.0)

       

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

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

      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;
      }
      }
      }
      

      then org.apache.logging.log4j.core.lookup.Interpolator.class

       

      @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 red mark, var="main" and prefixPos=-1. so it return the default value "-file";

       

       

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              wangzhongkuo wangzhongkuo
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 1h
                  1h