Gaurav February 2016

Garbage in Log Files when using Log4j 2 file appender programmatically

I have a use case where I have to use a different file name for all my loggers and I am trying to do it using a file appender programmatically in Log4j 2

I am using the following XML file for startup configuration of Log4j

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
    <Appenders>
        <File name="MyFile" fileName="C:\myLogs.log">
            <PatternLayout>
                <Pattern>"%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
            </PatternLayout>
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="MyFile"/>
        </Root>
    </Loggers>
</Configuration>

I am then using the following code to add the a file appender to my loggers:

public static Logger getLogger(String loggerName)
    {

        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        final org.apache.logging.log4j.core.config.Configuration config = ctx.getConfiguration();
        PatternLayout layout = PatternLayout.newBuilder().withPattern(PatternLayout.SIMPLE_CONVERSION_PATTERN).build();

        Appender appender = FileAppender.createAppender("C:/"+loggerName+".log", "false", "false", "File", "true",
                "false", "false", "4000", layout, null, "false", null, config);
        appender.start();
        config.addAppender(appender);
        AppenderRef ref = AppenderRef.createAppenderRef(loggerName+"ref", Level.ALL, null);
        AppenderRef[] refs = new AppenderRef[] {ref};
        LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.ALL, loggerName,
                "true", refs, null, config, null );
        loggerConfig.addAppender(appender, null, null);
        config.addLogger(loggerName, loggerConfig);
        ctx.updateLoggers();

        return LogManager        

Answers


Luke Woodward February 2016

Sorry, I couldn't reproduce this.

I used the following class, which contains your getLogger method, after adjusting the file name:

import org.apache.logging.log4j.*; 
import org.apache.logging.log4j.core.config.*;
import org.apache.logging.log4j.core.Appender; 
import org.apache.logging.log4j.core.appender.FileAppender; 
import org.apache.logging.log4j.core.layout.PatternLayout; 
import org.apache.logging.log4j.core.LoggerContext;

public class Log4j2Test {

    public static void main(String[] args) throws Exception {
        getLogger("Test").info("This is a test line");
    }

    public static Logger getLogger(String loggerName)
    {
        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        final org.apache.logging.log4j.core.config.Configuration config = ctx.getConfiguration();
        PatternLayout layout = PatternLayout.newBuilder().withPattern(PatternLayout.SIMPLE_CONVERSION_PATTERN).build();

        Appender appender = FileAppender.createAppender("/home/luke/Stack Overflow/"+loggerName+"_created.log", "false", "false", "File", "true",
                "false", "false", "4000", layout, null, "false", null, config);
        appender.start();
        config.addAppender(appender);
        AppenderRef ref = AppenderRef.createAppenderRef(loggerName+"ref", Level.ALL, null);
        AppenderRef[] refs = new AppenderRef[] {ref};
        LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.ALL, loggerName,
                "true", refs, null, config, null );
        loggerConfig.addAppender(appender, null, null);
        config.addLogger(loggerName, loggerConfig);
        ctx.updateLoggers();

        return LogManager.getLogger(loggerName);
    }
}

The CLASSPATH environment variable contains only the current directory . and the log4j2 JARs log4j-api-2.5.jar and log4j-core-2.5.jar. A copy of your log4j2.xml

Post Status

Asked in February 2016
Viewed 3,391 times
Voted 4
Answered 1 times

Search




Leave an answer