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 subversion and git services (Jira)" <ji...@apache.org> on 2020/02/16 07:39:00 UTC

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

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

ASF subversion and git services commented on LOG4J2-2211:
---------------------------------------------------------

Commit 19aa7fd40606d728471953890f86c9ae4d88ea5b in logging-log4j2's branch refs/heads/master from Ralph Goers
[ https://gitbox.apache.org/repos/asf?p=logging-log4j2.git;h=19aa7fd ]

LOG4J2-2211 - Allow Lookup keys with leading dashes by using a slash as an escape character.


> 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
>          Time Spent: 20m
>  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)