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

hm.binkley.util.logging.LoggerUtil Maven / Gradle / Ivy

There is a newer version: 6
Show newest version
/*
 * This is free and unencumbered software released into the public domain.
 *
 * Please see https://github.com/binkley/binkley/blob/master/LICENSE.md.
 */

package hm.binkley.util.logging;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.util.ContextInitializer;
import ch.qos.logback.core.joran.spi.JoranException;
import org.slf4j.Logger;

import javax.annotation.Nonnull;

import static org.slf4j.LoggerFactory.getILoggerFactory;

/**
 * {@code LoggerUtil} has utility methods for SLF4J loggers.
 *
 * @author B. K. Oxley (binkley)
 */
public final class LoggerUtil {
    private LoggerUtil() {
    }

    /**
     * Programatically changes the log level if logger is a logback logger.
     *
     * @param logger the logback logger, never missing
     * @param level the new level, never missing
     *
     * @throws ClassCastException if logger is not a logback logger
     */
    public static void setLevel(@Nonnull final Logger logger, @Nonnull final Level level) {
        ((ch.qos.logback.classic.Logger) logger).setLevel(level);
    }

    /** Resets the global logack, forcing reread of configuration. */
    public static void refreshLogback() {
        try {
            final LoggerContext context = (LoggerContext) getILoggerFactory();
            context.reset();
            context.getStatusManager().clear();
            new ContextInitializer(context).autoConfig();
        } catch (final JoranException e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy