View Javadoc
1   package ch.qos.logback.classic.util;
2   
3   import org.slf4j.ILoggerFactory;
4   import org.slf4j.LoggerFactory;
5   
6   import ch.qos.logback.classic.LoggerContext;
7   import ch.qos.logback.core.spi.ContextAwareBase;
8   import ch.qos.logback.core.status.ErrorStatus;
9   import ch.qos.logback.core.status.InfoStatus;
10  import ch.qos.logback.core.status.Status;
11  
12  /**
13   * Add a status message to the {@link LoggerContext} returned by {@link LoggerFactory#getILoggerFactory}.
14   * @author ceki
15   * @since 1.1.10
16   */
17  public class StatusViaSLF4JLoggerFactory {
18  
19      public static void addInfo(String msg, Object o) {
20          addStatus(new InfoStatus(msg, o));
21      }
22  
23      public static void addError(String msg, Object o) {
24          addStatus(new ErrorStatus(msg, o));
25      }
26  
27      public static void addError(String msg, Object o, Throwable t) {
28          addStatus(new ErrorStatus(msg, o, t));
29      }
30  
31      public static void addStatus(Status status) {
32          ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
33          if (iLoggerFactory instanceof LoggerContext) {
34              ContextAwareBase contextAwareBase = new ContextAwareBase();
35              LoggerContext loggerContext = (LoggerContext) iLoggerFactory;
36              contextAwareBase.setContext(loggerContext);
37              contextAwareBase.addStatus(status);
38          }
39      }
40  }