You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Piotr Karwasz (Jira)" <ji...@apache.org> on 2022/10/27 08:46:00 UTC
[jira] [Comment Edited] (LOG4J2-3562) Lookups are not substituted when pattern is created programmatically
[ https://issues.apache.org/jira/browse/LOG4J2-3562?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17624959#comment-17624959 ]
Piotr Karwasz edited comment on LOG4J2-3562 at 10/27/22 8:45 AM:
-----------------------------------------------------------------
Hi [~Calixte],
Sorry for the late comment. This is *not* a bug: if you are configuring Log4j2 components directly lookups are *not* evaluated.
If you want lookup evaluation use the [ConfigurationBuilder API|https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/config/builder/api/ConfigurationBuilder.html].
was (Author: pkarwasz):
Hi [~Calixte],
Sorry for the late comment. This is *not* a bug: if you are configuring Log4j2 components directly lookups are *not* evaluated.
If you want lookup evaluation use the [ConfigurationBuilder API](https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/config/builder/api/ConfigurationBuilder.html).
> 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)