001package ch.qos.logback.classic.util;
002
003import org.slf4j.ILoggerFactory;
004import org.slf4j.LoggerFactory;
005
006import ch.qos.logback.classic.LoggerContext;
007import ch.qos.logback.core.spi.ContextAwareBase;
008import ch.qos.logback.core.status.ErrorStatus;
009import ch.qos.logback.core.status.InfoStatus;
010import ch.qos.logback.core.status.Status;
011
012/**
013 * Add a status message to the {@link LoggerContext} returned by
014 * {@link LoggerFactory#getILoggerFactory}.
015 * 
016 * @author ceki
017 * @since 1.1.10
018 */
019public class StatusViaSLF4JLoggerFactory {
020
021    public static void addInfo(String msg, Object o) {
022        addStatus(new InfoStatus(msg, o));
023    }
024
025    public static void addError(String msg, Object o) {
026        addStatus(new ErrorStatus(msg, o));
027    }
028
029    public static void addError(String msg, Object o, Throwable t) {
030        addStatus(new ErrorStatus(msg, o, t));
031    }
032
033    public static void addStatus(Status status) {
034        ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
035        if (iLoggerFactory instanceof LoggerContext) {
036            ContextAwareBase contextAwareBase = new ContextAwareBase();
037            LoggerContext loggerContext = (LoggerContext) iLoggerFactory;
038            contextAwareBase.setContext(loggerContext);
039            contextAwareBase.addStatus(status);
040        }
041    }
042}