001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, 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 v1.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 */ 014package ch.qos.logback.core.util; 015 016import ch.qos.logback.core.Context; 017import ch.qos.logback.core.CoreConstants; 018import ch.qos.logback.core.spi.ContextAware; 019import ch.qos.logback.core.spi.LifeCycle; 020import ch.qos.logback.core.status.OnConsoleStatusListener; 021import ch.qos.logback.core.status.OnErrorConsoleStatusListener; 022import ch.qos.logback.core.status.StatusListener; 023 024import static ch.qos.logback.core.CoreConstants.STDOUT; 025import static ch.qos.logback.core.CoreConstants.SYSOUT; 026 027public class StatusListenerConfigHelper { 028 029 public static void installIfAsked(Context context) { 030 String slClass = OptionHelper.getSystemProperty(CoreConstants.STATUS_LISTENER_CLASS_KEY); 031 if (!OptionHelper.isNullOrEmptyOrAllSpaces(slClass)) { 032 addStatusListener(context, slClass); 033 } 034 } 035 036 private static void addStatusListener(Context context, String listenerClassName) { 037 StatusListener listener = null; 038 if (SYSOUT.equalsIgnoreCase(listenerClassName) || STDOUT.equalsIgnoreCase(listenerClassName)) { 039 listener = new OnConsoleStatusListener(); 040 } else { 041 listener = createListenerPerClassName(context, listenerClassName); 042 } 043 initAndAddListener(context, listener); 044 } 045 046 private static void initAndAddListener(Context context, StatusListener listener) { 047 if (listener != null) { 048 if (listener instanceof ContextAware) // LOGBACK-767 049 ((ContextAware) listener).setContext(context); 050 051 boolean effectivelyAdded = context.getStatusManager().add(listener); 052 if (effectivelyAdded && (listener instanceof LifeCycle)) { 053 ((LifeCycle) listener).start(); // LOGBACK-767 054 } 055 } 056 } 057 058 private static StatusListener createListenerPerClassName(Context context, String listenerClass) { 059 try { 060 return (StatusListener) OptionHelper.instantiateByClassName(listenerClass, StatusListener.class, context); 061 } catch (Exception e) { 062 // printing on the console is the best we can do 063 e.printStackTrace(); 064 return null; 065 } 066 } 067 068 /** 069 * This utility method adds a new OnConsoleStatusListener to the context passed 070 * as parameter. 071 * 072 * @param context 073 * @since 1.0.1 074 */ 075 static public void addOnConsoleListenerInstance(Context context, OnConsoleStatusListener onConsoleStatusListener) { 076 onConsoleStatusListener.setContext(context); 077 boolean effectivelyAdded = context.getStatusManager().add(onConsoleStatusListener); 078 if (effectivelyAdded) { 079 onConsoleStatusListener.start(); 080 } 081 } 082}