55

I am using SLF4J with LOG4J, and the configurations are usually in the log4j.properties, and it sets the log level to INFO.

However during the tests I would like to set the logs to DEBUG.

I can't see a way to automate this, neither to have something like log4j.tests.properties that would be loaded only during tests.

So I've tried doing this programmatically in the test setup (the @BeforeClass):

LogManager.getRootLogger().setLevel(Level.ALL);

With no success...

I am using these versions:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>

How can I achieve this result?

EDIT: I think I wasn't clear enough. This question is not about setting the correct log level... It is about setting the DEBUG log level when running Junit tests, and setting INFO log level in any other situation. I want to automate this.

2
  • 1
    There's a configuration file that Log4J uses called log4j.configuration inside of that, you'll find a property which sets the logging level, and the only thing you'll need to do is to change it from whatever is there to DEBUG
    – Julian
    Aug 4, 2016 at 21:56
  • 1
    that does not solve my problem. I want to set debug only when running the tests.
    – PedroD
    Aug 4, 2016 at 21:57

5 Answers 5

47

You do not need to give the JVM a different log implementation.

The logging code searches for the log4j.properties file using the classpath. So all you need to do is ensure that your test log4j.properties file is in a location that it will find before the release file.

I use Maven, which lays out files in directories to make that easy. My release log4j.properties goes in the directory src/main/resources. My test version goes in src/test/resources. The Eclipse build path (classpath) is set up to search src/test/resources before src/main/resources, so your unit tests use the test file. The JAR (or WAR) build instructions use the files from src/main/resources.

1
  • 7
    Logback takes a similar approach -- it looks for logback-test.xml.
    – Noumenon
    Mar 15, 2020 at 17:55
16

Below ROOT log level change will work for junit to set logging to desired level.

import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

@Before
public void setUp() {
    final Logger logger = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    logger.setLevel(Level.ALL);
}
2
  • The accepted answer and comment about using logback-test.xml is easier, but this works in a pinch too. Jun 15, 2021 at 14:34
  • The question is about log4j, not logback...
    – Redlab
    Dec 3, 2021 at 10:24
13

You can use the org.apache.logging.log4j.core.config.Configurator which has a setAllLevels method.

In your test you can use it in a @Before method:

  @Before
  public void changeLogLevel() {
    Configurator.setAllLevels("", Level.ALL);
  }

NOTE: tested with binding org.slf4j:slf4j-log4j12:1.7.26

3

maybe not entirely related to the question in the first place, however a google query will lead most developers to this topic when they search for a way on

how to change the log level for certain junit test methods.

The way to go is to use a custom junit MethodRule that accesses the loggers and re-configures the log level per package.

With below classes, you can achieve this. It sets the log level for packages and classes as defines in the annotation of a test method and when the test has finished, sets the loggers back to their initial state. We assume the default log level is set to INFO currently.

@Test
@LogLevel(packageToLevel = { "my.java.package=DEBUG", "my.other.java.package.ClassNeedsTracing=TRACE" })
public void allLogsOfThatPackageAreInDebugNow() {
   ...
}

@Test
@LogLevel(packageToLevel = { "my.java.package=TRACE", "my.java.package.ClassNoTracing=TRACE" })
public void allLogsOfThatPackageAreInTraceNowExceptOneClass() {
   ...
}

To achieve this, you need so specify the test rule in your test class:

@Rule
LogLevelRule logLevelRule = new LogLevelRule();

Classes needed found below:

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

/**
 * a Junit Rule that check for LogLevel annotation on methods and activates the configured log level per package. After
 * the test was executed, restores the previous log level.
 */
public class LogLevelRule implements MethodRule {

    @Override
    public Statement apply(Statement base, FrameworkMethod method, Object target) {

        return new Statement() {
            @Override
            public void evaluate() throws Throwable {

                // activate log level desired, remember what they were
                Map<String, Level> existingPackageLogLevel = new HashMap<>();
                LogLevel logLevelAnnotation = method.getAnnotation(LogLevel.class);
                if (logLevelAnnotation != null) {
                    activate(logLevelAnnotation.packageToLevel(), existingPackageLogLevel);
                }

                // run the test
                Throwable testFailure = evaluateSafely(base);

                // revert the log level back to what it was
                if (!existingPackageLogLevel.isEmpty()) {
                    deactivate(existingPackageLogLevel);
                }

                if (testFailure != null) {
                    throw testFailure;
                }
            }

            /**
             * execute the test safely so that if it fails, we can still revert the log level
             */
            private Throwable evaluateSafely(Statement base) {
                try {
                    base.evaluate();
                    return null;
                } catch (Throwable throwable) {
                    return throwable;
                }
            }
        };
    }

    /**
     * activates the log level per package and remember the current setup
     *
     * @param packageToLevel
     *            the configuration of the annotation
     * @param existingPackageLogLevel
     *            where to store the current information
     */
    protected void activate(String[] packageToLevel, Map<String, Level> existingPackageLogLevel) {
        for (String pkgToLevel : packageToLevel) {
            String[] split = pkgToLevel.split("=");
            String pkg = split[0];
            String levelString = split[1];
            Logger logger = LogManager.getLogger(pkg);
            Level level = logger.getLevel();
            existingPackageLogLevel.put(pkg, level);
            logger.setLevel(Level.toLevel(levelString));
        }
    }

    /**
     * resets the log level of the changes packages back to what it was before
     *
     * @param existingPackageLogLevel
     */
    protected void deactivate(Map<String, Level> existingPackageLogLevel) {
        for (Map.Entry<String, Level> e : existingPackageLogLevel.entrySet()) {
            LogManager.getLogger(e.getKey()).setLevel(e.getValue());
        }
    }

}




import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * marks a method to use a different log level for the execution phase
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface LogLevel {
    String[] packageToLevel();
}
1

Usually LEVEL.FINEST should do it... but take a look to http://saltnlight5.blogspot.mx/2013/08/how-to-configure-slf4j-with-different.html to see logging frameworks implementations considereation.

4
  • That log level doesn't exist here. However this is not about the implementation, it is about using a different configuration whether I am running tests or not.
    – PedroD
    Aug 4, 2016 at 22:02
  • 1
    at the time you run your test they will use a logger implementation, Log4J, standar java.util.Log or any other... may be this is what are you looking for... the way to configure those levels stackoverflow.com/questions/878348/… Aug 4, 2016 at 22:35
  • I just needed to put the log4j.properties in the src/test/resources and it will override the one in src/main/resources during the tests. :)
    – PedroD
    Aug 4, 2016 at 23:01
  • 1
    Sorry if i missunderstand the question at the beggining... you can do it on the file as you mention (using the proper file, in this case the log4j one) or going programmatically, and programatically will depend on if you used log4J or something else... glad you find your answer. Aug 4, 2016 at 23:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.