Hi All:

I've boiled down a problem I am having to this short test (instead of my
whole server's test suite).

package com.test;

import java.io.File;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configurator;
import org.junit.Assert;
import org.junit.Test;

/**
 * Eventually, the logger "com.test.Thing1" logs to a file.
 */
public class Log4jTest {

    @Test
    public void test() {
        File file = new File("target/logs/special.log");
        file.delete();
        new Thing1().info();
        LogManager.shutdown(); // Comment this line out and the file size
test passes
        try (final LoggerContext lc =
Configurator.initialize(getClass().getName(),
"src/test/resources/log4j2-console-file.xml")) {
            new Thing1().info();
        }
        Assert.assertTrue("FIle is missing: " + file.getAbsolutePath(),
file.exists());
        Assert.assertTrue("File is empty: " + file.getAbsolutePath(),
file.length() > 0); // FAIL
    }

}

Where Thing1.java is:

package com.test;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Thing1 {

    private static final Logger LOGGER = LogManager.getLogger(Thing1.class);

    public void info() {
        LOGGER.info("Hello from {}", this);
    }
}

Where src/test/resources/log4j2-console-file.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ALL">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <!-- For a color log in the Eclipse console: -->
      <!-- - Install http://www.mihai-nita.net/eclipse -->
      <!-- - Use noConsoleNoAnsi="false" -->
      <PatternLayout noConsoleNoAnsi="false"
        pattern="%style{%d}{white} %highlight{%-5level}{TRACE=white}
%style{[%t][%logger]%notEmpty{[%markerSimpleName]}}{white}
%highlight{%msg%n%xThrowable}{TRACE=white}" />
    </Console>
    <RandomAccessFile name="MonitorTestFile" fileName="target/logs/special.log"
append="false">
      <PatternLayout charset="UTF-8">
        <Pattern>%m%n</Pattern>
      </PatternLayout>
    </RandomAccessFile>
  </Appenders>
  <Loggers>
    <Root level="INFO">
      <AppenderRef ref="Console" />
    </Root>
    <Logger name="com.test.Thing1" level="INFO" additivity="false">
      <AppenderRef ref="MonitorTestFile" />
    </Logger>
  </Loggers>
</Configuration>

The file is created but it is empty.

This short tests simulates log4j going up and down in tests through the
same LoggerContextRule we use in Log4j itself. My server will also bring
Log4j up and down IRL.

Thoughts?

-- 
E-Mail: [email protected] | [email protected]
Java Persistence with Hibernate, Second Edition
<https://www.amazon.com/gp/product/1617290459/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1617290459&linkCode=as2&tag=garygregory-20&linkId=cadb800f39946ec62ea2b1af9fe6a2b8>

<http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1617290459>
JUnit in Action, Second Edition
<https://www.amazon.com/gp/product/1935182021/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182021&linkCode=as2&tag=garygregory-20&linkId=31ecd1f6b6d1eaf8886ac902a24de418%22>

<http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182021>
Spring Batch in Action
<https://www.amazon.com/gp/product/1935182951/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182951&linkCode=%7B%7BlinkCode%7D%7D&tag=garygregory-20&linkId=%7B%7Blink_id%7D%7D%22%3ESpring+Batch+in+Action>
<http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182951>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory

Reply via email to