View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2009, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.classic.util;
11  
12  import ch.qos.logback.classic.LoggerContext;
13  import ch.qos.logback.core.status.OnConsoleStatusListener;
14  import ch.qos.logback.core.status.StatusListener;
15  import ch.qos.logback.core.util.OptionHelper;
16  
17  public class StatusListenerConfigHelper {
18  
19    static void installIfAsked(LoggerContext loggerContext) {
20      String slClass = OptionHelper.getSystemProperty(
21          ContextInitializer.STATUS_LISTENER_CLASS);
22      if (!OptionHelper.isEmpty(slClass)) {
23        addStatusListener(loggerContext, slClass);
24      }
25    }
26  
27    static void addStatusListener(LoggerContext loggerContext,
28        String listenerClass) {
29      StatusListener listener = null;
30      if (ContextInitializer.SYSOUT.equalsIgnoreCase(listenerClass)) {
31        listener = new OnConsoleStatusListener();
32      } else {
33        try {
34          listener = (StatusListener) OptionHelper.instantiateByClassName(
35              listenerClass, StatusListener.class, loggerContext);
36        } catch (Exception e) {
37          // printing on the console is the best we can do
38          e.printStackTrace();
39        }
40      }
41      if (listener != null) {
42        loggerContext.getStatusManager().add(listener);
43      }
44    }
45  }