View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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.core.util;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.CoreConstants;
18  import ch.qos.logback.core.spi.ContextAware;
19  import ch.qos.logback.core.spi.LifeCycle;
20  import ch.qos.logback.core.status.OnConsoleStatusListener;
21  import ch.qos.logback.core.status.StatusListener;
22  
23  public class StatusListenerConfigHelper {
24  
25      public static void installIfAsked(Context context) {
26          String slClass = OptionHelper.getSystemProperty(CoreConstants.STATUS_LISTENER_CLASS_KEY);
27          if (!OptionHelper.isEmpty(slClass)) {
28              addStatusListener(context, slClass);
29          }
30      }
31  
32      private static void addStatusListener(Context context, String listenerClassName) {
33          StatusListener listener = null;
34          if (CoreConstants.SYSOUT.equalsIgnoreCase(listenerClassName)) {
35              listener = new OnConsoleStatusListener();
36          } else {
37              listener = createListenerPerClassName(context, listenerClassName);
38          }
39          initAndAddListener(context, listener);
40      }
41  
42      private static void initAndAddListener(Context context, StatusListener listener) {
43          if (listener != null) {
44              if (listener instanceof ContextAware) // LOGBACK-767
45                  ((ContextAware) listener).setContext(context);
46  
47              boolean effectivelyAdded = context.getStatusManager().add(listener);
48              if (effectivelyAdded && (listener instanceof LifeCycle)) {
49                  ((LifeCycle) listener).start(); // LOGBACK-767
50              }
51          }
52      }
53  
54      private static StatusListener createListenerPerClassName(Context context, String listenerClass) {
55          try {
56              return (StatusListener) OptionHelper.instantiateByClassName(listenerClass, StatusListener.class, context);
57          } catch (Exception e) {
58              // printing on the console is the best we can do
59              e.printStackTrace();
60              return null;
61          }
62      }
63  
64      /**
65         * This utility method adds a new OnConsoleStatusListener to the context
66         * passed as parameter.
67         *
68         * @param context
69         * @since 1.0.1
70         */
71      static public void addOnConsoleListenerInstance(Context context, OnConsoleStatusListener onConsoleStatusListener) {
72          onConsoleStatusListener.setContext(context);
73          boolean effectivelyAdded = context.getStatusManager().add(onConsoleStatusListener);
74          if (effectivelyAdded) {
75              onConsoleStatusListener.start();
76          }
77      }
78  }