[
https://issues.apache.org/jira/browse/LOG4J2-3562?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17625191#comment-17625191
]
Ralph Goers edited comment on LOG4J2-3562 at 10/27/22 4:17 PM:
---------------------------------------------------------------
I think more information needs to be added to this.
When PatternLayout is configured as shown in the XML in the description the
Lookup is actually invoked by the Plugin system when resolving the value for
the pattern parameter. When you create it programmatically you are bypassing
the Plugin system so this obviously doesn't happen unless you invoke the Lookup
yourself. In other words, it works something like:
{code:java}
var pattern = substitutor.replace("${java:version} - %msg%n");
var layout = PatternLayout.newBuilder()
.withPattern(pattern)
.build();
{code}
But notice that invoking a Lookup for this is very inefficient when you are
doing it yourself. Instead, just do:
{code:java}
var version = System.getProperty("java.version");
var layout = PatternLayout.newBuilder()
.withPattern(version + " - %msg%n")
.build();
{code}
Next, Lookups are performed against the pattern itself for every log event, but
my recollection is that the lookups processed during execution has been
severely restricted for security reasons. Unfortunately, I am having some
trouble locating where that change was documented.
was (Author: [email protected]):
I think more information needs to be added to this.
When PatternLayout is configured as shown in the XML in the description the
Lookup is actually invoked by the Plugin system when resolving the value for
the pattern parameter. When you create it programmatically you are bypassing
the Plugin system so this obviously doesn't happen unless you invoke the Lookup
yourself. In other words, it works something like:
{code:java}
var pattern = substitutor.replace("${java:version} - %msg%n");
var layout = PatternLayout.newBuilder()
.withPattern(pattern)
.build();
{code}
But notice that invoking a Lookup for this is very inefficient. Instead, just
do:
{code:java}
var version = System.getProperty("java.version");
var layout = PatternLayout.newBuilder()
.withPattern(version + " - %msg%n")
.build();
{code}
Next, Lookups are performed against the pattern itself for every log event, but
my recollection is that the lookups processed during execution has been
severely restricted for security reasons. Unfortunately, I am having some
trouble locating where that change was documented.
> Lookups are not substituted when pattern is created programmatically
> --------------------------------------------------------------------
>
> Key: LOG4J2-3562
> URL: https://issues.apache.org/jira/browse/LOG4J2-3562
> Project: Log4j 2
> Issue Type: Bug
> Affects Versions: 2.18.0, 2.17.2, 2.19.0
> Reporter: Calixte Bonsart
> Priority: Major
>
> Starting in version 2.17.2, when programmatically creating an appender with a
> pattern that contains a lookup, that lookup is not substituted anymore. The
> lookup works when it is defined in the XML configuration file. The same code
> works in version 2.17.1 and below.
> {code:java}
> % java -cp log4j-api-2.17.1.jar:log4j-core-2.17.1.jar
> -Dlog4j2.configurationFile=log4j2.xml Main.java
> Java version 17.0.3 - Hello World
> Java version 17.0.3 - Hello World
> % java -cp log4j-api-2.17.2.jar:log4j-core-2.17.2.jar
> -Dlog4j2.configurationFile=log4j2.xml Main.java
> Java version 17.0.3 - Hello World
> ${java:version} - Hello World
> % java -cp log4j-api-2.18.0.jar:log4j-core-2.18.0.jar
> -Dlog4j2.configurationFile=log4j2.xml Main.java
> Java version 17.0.3 - Hello World
> ${java:version} - Hello World
> {code}
> {code:java}
> public class Main{
> public static void main(String[] args){
> var logger = LogManager.getLogger("Main");
> logger.info("Hello World");
> var ctx = (LoggerContext)LogManager.getContext(false);
> var config = ctx.getConfiguration();
> var layout = PatternLayout.newBuilder()
> .withPattern("${java:version} - %msg%n")
> .build();
> var appender = ConsoleAppender.newBuilder()
> .setLayout(layout)
> .setTarget(Target.SYSTEM_OUT)
> .setName("Console")
> .build();
> config.addAppender(appender);
> appender.start();
> var loggerConfig = new LoggerConfig("Main", Level.INFO, false);
> loggerConfig.addAppender(appender, null, null);
> config.addLogger("Main", loggerConfig);
> ctx.updateLoggers();
> logger.info("Hello World");
> }
> }
> {code}
> {code:xml}
> <?xml version="1.0" encoding="UTF-8"?>
> <Configuration>
> <Appenders>
> <Console name="Console" target="SYSTEM_OUT">
> <PatternLayout pattern="${java:version} - %msg%n" />
> </Console>
> </Appenders>
> <Loggers>
> <Root level="info">
> <AppenderRef ref="Console" />
> </Root>
> </Loggers>
> </Configuration>
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)