You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Awais Chishti (Jira)" <ji...@apache.org> on 2022/08/26 23:05:00 UTC

[jira] [Created] (LOG4J2-3582) Unexpected appender attribute after programmatically configuring two separate `Configuration` objects

Awais Chishti created LOG4J2-3582:
-------------------------------------

             Summary: Unexpected appender attribute after programmatically configuring two separate `Configuration` objects
                 Key: LOG4J2-3582
                 URL: https://issues.apache.org/jira/browse/LOG4J2-3582
             Project: Log4j 2
          Issue Type: Bug
          Components: Configurators
            Reporter: Awais Chishti


Expected output from the code below:
{quote}{{Got buffer size from second configuration: 7777, expected: 7777}}
{quote}
Actual output from the code below:
{quote}{{Got buffer size from second configuration: 1234, expected: 7777}}
{quote}
Code:
{code:java}
package com.chishtia.mre;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
import org.apache.logging.log4j.core.appender.rolling.RollingFileManager;
import org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;

/**
 * Class to test log4j2 configuration state persistence
 */
public class Log4j2PersistentConfigurationMRE {
    private static final String APPENDER_NAME = "someAppenderName";

    private static Configuration getConfigurationWithBufferSize(final int bufferSize) {
        // Initialize config
        final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
        builder.setConfigurationName("TestConfig");
        // Add appenders
        final AppenderComponentBuilder appenderBuilder = builder.newAppender(APPENDER_NAME, "RollingFile");
        appenderBuilder.addComponent(builder.newComponent("TimeBasedTriggeringPolicy"));
        appenderBuilder.addAttribute("filePattern", "example.log.%d{yyyy-MM-dd}");
        appenderBuilder.addAttribute("bufferedIO", true);
        appenderBuilder.addAttribute("bufferSize", bufferSize);
        builder.add(appenderBuilder);
        // Add root logger
        final RootLoggerComponentBuilder rootLoggerBuilder = builder.newRootLogger(Level.INFO);
        rootLoggerBuilder.add(builder.newAppenderRef(APPENDER_NAME));
        builder.add(rootLoggerBuilder);
        // Build
        return builder.build();
    }

    private static void log4j2MREDemo() {
        final Configuration firstConfiguration = getConfigurationWithBufferSize(1234);
        final Configuration secondConfiguration = getConfigurationWithBufferSize(7777);
        final Appender appender = secondConfiguration.getAppenders().get(APPENDER_NAME);
        final RollingFileAppender rollingFileAppender = (RollingFileAppender) appender;
        final RollingFileManager rollingFileManager = rollingFileAppender.getManager();
        System.out.println("Got buffer size from second configuration: " +
                           rollingFileManager.getBufferSize() +
                           ", expected: 7777");
    }

    public static void main(final String[] args) {
        log4j2MREDemo();
    }
}
{code}

TL;DR: It seems that the BufferSize appender attribute is not correctly set by ConfigurationBuilder::build() after its first invocation, and that the attribute set in the first invocation persists.

Q: Is this expected behavior? If yes then what workaround will let me rebuild configurations without the previous state persisting?



--
This message was sent by Atlassian Jira
(v8.20.10#820010)