All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.neuland.assertj.logging.ExpectedLogging Maven / Gradle / Ivy

There is a newer version: 0.4.0
Show newest version
package de.neuland.assertj.logging;

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


/**
 * {@inheritDoc}
 */
public class ExpectedLogging extends GenericExpectedLogging {
    private final Logger logger;
    private ExpectedLogging(String loggingSource) {
        super(loggingSource);
        this.logger = getLogger();
    }

    public static ExpectedLogging forSource(Class loggingSource) {
        return new ExpectedLogging(loggingSource.getCanonicalName());
    }

    @Override
    Log4j12LogEventCaptureAppender addCaptureAppender() {
        Log4j12LogEventCaptureAppender captureAppender = new Log4j12LogEventCaptureAppender();
        logger.addAppender(captureAppender);

        return captureAppender;
    }

    @Override
    void assertLoggerLevelIsAtLeastInfo() {
        final Level loggerLevel = logger.getLevel();
        if (loggerLevel == null || loggerLevel.isGreaterOrEqual(Level.INFO)) {
            logger.setLevel(Level.INFO);
        }
    }

    @Override
    void removeCaptureAppender(final Log4j12LogEventCaptureAppender appender) {
        logger.removeAppender(appender);
    }

    private Logger getLogger() {
        return LogManager.getLogger(loggingSource);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy