001/* 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v2.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014 015package ch.qos.logback.classic.util; 016 017import org.slf4j.ILoggerFactory; 018import org.slf4j.LoggerFactory; 019 020import ch.qos.logback.classic.LoggerContext; 021import ch.qos.logback.core.spi.ContextAwareBase; 022import ch.qos.logback.core.status.ErrorStatus; 023import ch.qos.logback.core.status.InfoStatus; 024import ch.qos.logback.core.status.Status; 025 026/** 027 * Add a status message to the {@link LoggerContext} returned by 028 * {@link LoggerFactory#getILoggerFactory}. 029 * 030 * @author ceki 031 * @since 1.1.10 032 */ 033public class StatusViaSLF4JLoggerFactory { 034 035 public static void addInfo(String msg, Object o) { 036 addStatus(new InfoStatus(msg, o)); 037 } 038 039 public static void addError(String msg, Object o) { 040 addStatus(new ErrorStatus(msg, o)); 041 } 042 043 public static void addError(String msg, Object o, Throwable t) { 044 addStatus(new ErrorStatus(msg, o, t)); 045 } 046 047 public static void addStatus(Status status) { 048 ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory(); 049 if (iLoggerFactory instanceof LoggerContext) { 050 ContextAwareBase contextAwareBase = new ContextAwareBase(); 051 LoggerContext loggerContext = (LoggerContext) iLoggerFactory; 052 contextAwareBase.setContext(loggerContext); 053 contextAwareBase.addStatus(status); 054 } 055 } 056}