Search code examples
javaxmllog4jwebspherelog4j2

log4j2 creates empty logs file


I'm very new with log4j2 but, I have this application that used log4j and I had to change it to the log4j2 version.

The log file is being created but it's always empty, this project doesn't use maven so I don't have any pom.xml, I just have my log4j2.xml which look like this:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
    <Appenders>
        <RollingFile name="archive" fileName="/${sys:ENTORNO}/online/es/web/logs/WebSeal_EAI${sys:cloneId}.log"
                filePattern="/${sys:ENTORNO}/online/es/web/logs/WebSeal_EAI${sys:cloneId}.log.%d{yyyy-MM-dd}.gz">
            <PatternLayout>
                <Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %log{36} - %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" />
            </Policies>
        </RollingFile>
        <Console name="screen" target="SYSTEM_OUT">
            <PatternLayout>
                <Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %log{36} - %m%n</Pattern>
            </PatternLayout>
        </Console>
    </Appenders>

    <Loggers>
        <Root level="debug">
            <AppenderRef ref="archive"/>
        </Root>
    </Loggers>
</Configuration>

And in java, I load the configuration like this:

            File file = new File(log4Jcfg);
            ConfigurationFactory factory =  XmlConfigurationFactory.getInstance();
            ConfigurationSource configurationSource = new ConfigurationSource(new FileInputStream(new File(log4Jcfg)));
            Configuration configuration = factory.getConfiguration(context, configurationSource);
            context = new LoggerContext("JournalDevLoggerContext");
            context.start(configuration);

log4Jcfg has the xml path. The file it's created but it's always empty and I truly don't know why.

I have been reading a lot of questions like this, and tried almost everything (I think) as I put the .xml in the classpath, I changed a few things about the xml but none of that worked for me.

Anyone knows what is happening, I mean, if the file is created, why it doesn't print anything? Is it something wrong with the .xml or the java code where I load the configuration?

Also, this project is with the server Websphere v9.0.

Thank you so much.


Solution

  • I case someone has this same problem, I've just solved it. The problem was the way I was loading the xml config; I've changed it like this:

    LoggerContext ctx = getLoggerContext();
    ConfigurationSource configurationSource = new ConfigurationSource(new FileInputStream(new File(log4Jcfg)));
    ctx.start(ConfigurationFactory.getInstance().getConfiguration(ctx, configurationSource));
    

    and now it works perfect!