For some reason my Eclipse console no longer displays Log4j INFO and DEBUG statements when I run JUnit tests. In terms of code there hasn't been any change, so it must something to do with the Eclipse configuration.
All I do in my Unit test is the following and for some reason ONLY the ERROR statement is displayed in the Eclipse console. Why? Where shall I look for clues?
public class SampleTest
{
private static final Logger LOGGER = Logger.getLogger(SampleTest.class);
@Before
public void init() throws Exception
{
// Log4J junit configuration.
BasicConfigurator.configure();
LOGGER.info("INFO TEST");
LOGGER.debug("DEBUG TEST");
LOGGER.error("ERROR TEST");
}
}
Details:
- log4j-1.2.6.jar
- junit-4.6.jar Eclipse
- IDE for Java Developers, Version: Helios Release, Build id: 20100617-1415
Configuring with
BasicConfigurator.configure();
sets up a basic console appender set at debug. A project with the setup above and no other code (except for a test) should produce three lines of logging in the console. I cannot say anything else than "it works for me".Have you tried creating an empty project with just log4j and junit, with only the code above and ran it?
Also, in order to get the
@Before
method running:EDIT:
If you run more than one test at one time, each of them will call init before running.
In this case, if you had two tests, the first would have one logger and the second test would call init again, making it log twice (try it) - you should get 9 lines of logging in console with two tests.
You might want to use a static init method annotated with
@BeforeClass
to avoid this. Though this also happens across files, you might want to have a look at documentation on TestSuites in JUnit 4. And/or callBasicConfigurator.resetConfiguration();
in an @AfterClass annotated class, to remove all loggers after each test class / test suite.Also, the root logger is reused, so that if you set the root logger's level in a test method that runs early, it will keep this setting for all other tests that are run later, even if they are in different files. (will not happen when resetting configuration).
Testcase - this will cause 9 lines of logging:
Changing the init method reduces to the excepted six lines:
Your problem is probably caused in some other test class or test suite where the logging level of the root logger is set to ERROR, and not reset.
You could also test this out by resetting in the @BeforeClass method, before setting logging up.
Be advised that these changes might break expected logging for other test cases until it is fixed at all places. I suggest trying out how this works in a separate workspace/project to get a feel for how it works.
Check that your
log4j.properties
orlog4j.xml
are copied to your IDE classpath and loads when callingBasicConfigurator.configure()
I had the same error.
I am using Jboss 7.1 AS. In the configuration file - standalone.xml edit the following tag. (stop your server and edit)
The ALL has the lowest possible rank and is intended to turn on all logging.
Add a test dependency to your pom to slf4j.
if the log4j.xml file is not in the project,and you are using tomcat, try going to tomcat instance and search for log4j. Try changing the consoleAppender level to debug and redeploy the application in tomcat. That might help.
One thing to note, if you have a log4j.properties file on your classpath you do not need to call BasicConfigurator. A description of how to configure the properties file is here.
You could pinpoint whether your IDE is causing the issue by trying to run this class from the command line with log4j.jar and log4j.properties on your classpath.