View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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 v1.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  package ch.qos.logback.classic.util;
15  
16  import ch.qos.logback.classic.LoggerContext;
17  import ch.qos.logback.core.status.OnConsoleStatusListener;
18  import ch.qos.logback.core.status.StatusListener;
19  import ch.qos.logback.core.util.OptionHelper;
20  
21  public class StatusListenerConfigHelper {
22  
23    static void installIfAsked(LoggerContext loggerContext) {
24      String slClass = OptionHelper.getSystemProperty(
25          ContextInitializer.STATUS_LISTENER_CLASS);
26      if (!OptionHelper.isEmpty(slClass)) {
27        addStatusListener(loggerContext, slClass);
28      }
29    }
30  
31    static void addStatusListener(LoggerContext loggerContext,
32        String listenerClass) {
33      StatusListener listener = null;
34      if (ContextInitializer.SYSOUT.equalsIgnoreCase(listenerClass)) {
35        listener = new OnConsoleStatusListener();
36      } else {
37        try {
38          listener = (StatusListener) OptionHelper.instantiateByClassName(
39              listenerClass, StatusListener.class, loggerContext);
40        } catch (Exception e) {
41          // printing on the console is the best we can do
42          e.printStackTrace();
43        }
44      }
45      if (listener != null) {
46        loggerContext.getStatusManager().add(listener);
47      }
48    }
49  }