0

Problem: Logs doesn't echo anything in my unit tests. Logging works well in my app, but when I move the same code to test package they stop working. (see code below)

Question: How to get logs in my unit tests?

Code:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LogTest {

  private static final Log _LOG = LogFactory.getLog(TransformerServiceTest.class);

  @Test
  public void testLogs() {
    _LOG.info("!!!!!!!!!!!!!!!!!!"); // NEVER ECHOES TO STDOUT
  }
}

Update: Now only _LOG.error() works

After changing to DEBUG level, only ERROR level works

log4j.properties

# Root logger option
log4j.rootLogger=DEBUG, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

LogTest.class

public class LogTest {

    private static Log _LOG = LogFactory.getLog(LogTest.class);

    @Test
    public void test() {
        _LOG.error("Error works!"); //works
        _LOG.warn("Warn works!"); //doesn't work
        _LOG.info("Info works!"); // doesn't work
    }
}
VB_
  • 45,112
  • 42
  • 145
  • 293
  • 2
    add log configuration to your test. If you are using a maven build, put log configuration in the src/test/resources directory. – DwB Sep 04 '14 at 14:53
  • Maybe show your configuration/build process as well? This isn't really enough information for us to do anything other than guesswork. – sheltem Sep 04 '14 at 14:53
  • @DwB thanks! I included log4j.properties and it works. But I don't know how to include all levels of logging. Can you suggest how to log INFO, WARN, ERROR? – VB_ Sep 04 '14 at 18:32
  • @sheltem Do you know how to include all levels of logging. Can you suggest how to log INFO, WARN, ERROR? – VB_ Sep 04 '14 at 18:33
  • Set the log level to the lowest value (DEBUG or TRACE) and everything at and above it will show. – DwB Sep 04 '14 at 18:34
  • @DwB look at updated section of my question pls – VB_ Sep 04 '14 at 18:40

1 Answers1

0

You just need to add the log4j dependency into your classpath and all will work fine.

If you are using Maven then just add this to the dependencies element in the pom.xml:

...
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
...

If you have a simpler working env, just add it to your classpath

shlomi33
  • 1,458
  • 8
  • 9