1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.classic.util;
16  
17  import org.slf4j.ILoggerFactory;
18  import org.slf4j.LoggerFactory;
19  
20  import ch.qos.logback.classic.LoggerContext;
21  import ch.qos.logback.core.spi.ContextAwareBase;
22  import ch.qos.logback.core.status.ErrorStatus;
23  import ch.qos.logback.core.status.InfoStatus;
24  import ch.qos.logback.core.status.Status;
25  
26  /**
27   * Add a status message to the {@link LoggerContext} returned by
28   * {@link LoggerFactory#getILoggerFactory}.
29   * 
30   * @author ceki
31   * @since 1.1.10
32   */
33  public class StatusViaSLF4JLoggerFactory {
34  
35      public static void addInfo(String msg, Object o) {
36          addStatus(new InfoStatus(msg, o));
37      }
38  
39      public static void addError(String msg, Object o) {
40          addStatus(new ErrorStatus(msg, o));
41      }
42  
43      public static void addError(String msg, Object o, Throwable t) {
44          addStatus(new ErrorStatus(msg, o, t));
45      }
46  
47      public static void addStatus(Status status) {
48          ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
49          if (iLoggerFactory instanceof LoggerContext) {
50              ContextAwareBase contextAwareBase = new ContextAwareBase();
51              LoggerContext loggerContext = (LoggerContext) iLoggerFactory;
52              contextAwareBase.setContext(loggerContext);
53              contextAwareBase.addStatus(status);
54          }
55      }
56  }