Skip to content
Advertisement

Log4j2 how to overwrite path in configuration log4j2.xml?

I have to save the logs to the path taken from the database. I know the logs start earlier. For this, in xml I have a path configuration, but I want the path to be overwritten after downloading data from the database.

        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        Configuration config = ctx.getConfiguration();
        Layout layout = PatternLayout.createLayout("test[%t] %-5level %C %M: %msg%n", config, null, null, false, false, null, null );
        SizeBasedTriggeringPolicy policy = SizeBasedTriggeringPolicy.createPolicy("10M");
        DefaultRolloverStrategy strategy = DefaultRolloverStrategy.createStrategy(null, "0", null, null, config);
        RollingFileAppender appender = RollingFileAppender.createAppender(logDir, rollLogDir + "$${date:yyyy-MM}/test-%d{MM-dd-yyyy}-%i.log.gz", "true",
                "RollingFileAppender", "true", "128", "true", policy, strategy, layout, null, "true", "false",  "false", config);
        appender.start();
        config.addAppender(appender);
        AppenderRef ref = AppenderRef.createAppenderRef("File", Level.INFO, null);
        AppenderRef[] refs = new AppenderRef[] { ref };
        LoggerConfig loggerConfig = LoggerConfig.createLogger("true", Level.INFO, LogManager.ROOT_LOGGER_NAME, "true",
                refs, null, config, null);
        loggerConfig.addAppender(appender, Level.INFO, null);
        config.addLogger(LogManager.ROOT_LOGGER_NAME, loggerConfig);
        ctx.updateLoggers();

Log4j2.xml

<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="test[%t] %-5level %C %M: %msg%n"/>
        </Console>
        <RollingFile name="RollingFileAppender" fileName="logs/server1/testtowy.log"
              filePattern="logs/server1/$${date:yyyy-MM}/test-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d [%t] %p %c - %m%n"/>
            <Policies>
                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy max="1000" />
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="org.hibernate.SQL" level="debug" additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="RollingFileAppender"/>
        </Logger>
        <Logger name="com.test.test" level="info" additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="RollingFileAppender"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="RollingFileAppender"/>
        </Root>
    </Loggers>
</Configuration>

Advertisement

Answer

Take a look at Programmatic Configuration

My suggestion would have no Log4j2.xml logging, just add a new one for your app logs. Something like:

        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        final Configuration config = ctx.getConfiguration();
        Layout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, config, null,
            null,null, null);
        Appender appender = FileAppender.createAppender("target/test.log", "false", "false", "File", "true",
            "false", "false", "4000", layout, null, "false", null, config);
        appender.start();
        config.addAppender(appender);
        AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
        AppenderRef[] refs = new AppenderRef[] {ref};
        LoggerConfig loggerConfig = LoggerConfig.createLogger("false", "info", "org.apache.logging.log4j",
            "true", refs, null, config, null );
        loggerConfig.addAppender(appender, null, null);
        config.addLogger("org.apache.logging.log4j", loggerConfig);
        ctx.updateLoggers();
Advertisement