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