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.joran.action;
15  
16  import org.xml.sax.Attributes;
17  
18  import ch.qos.logback.classic.LoggerContext;
19  import ch.qos.logback.classic.turbo.ReconfigureOnChangeFilter;
20  import ch.qos.logback.core.joran.action.Action;
21  import ch.qos.logback.core.joran.spi.InterpretationContext;
22  import ch.qos.logback.core.util.ContextUtil;
23  import ch.qos.logback.core.util.Duration;
24  import ch.qos.logback.core.util.OptionHelper;
25  import ch.qos.logback.core.util.StatusPrinter;
26  
27  public class ConfigurationAction extends Action {
28    static final String INTERNAL_DEBUG_ATTR = "debug";
29    static final String SCAN_ATTR = "scan";
30    static final String SCAN_PERIOD_ATTR = "scanPeriod";
31  
32    boolean debugMode = false;
33    long threshold = 0;
34  
35    public void begin(InterpretationContext ec, String name, Attributes attributes) {
36      String debugAttrib = ec.subst(attributes.getValue(INTERNAL_DEBUG_ATTR));
37      threshold = System.currentTimeMillis();
38      if (OptionHelper.isEmpty(debugAttrib)
39          || debugAttrib.equalsIgnoreCase("false")
40          || debugAttrib.equalsIgnoreCase("null")) {
41        addInfo(INTERNAL_DEBUG_ATTR + " attribute not set");
42      } else {
43        debugMode = true;
44      }
45  
46      processScanAttrib(attributes);
47  
48      new ContextUtil(context).addHostNameAsProperty();
49  
50      // the context is turbo filter attachable, so it is pushed on top of the
51      // stack
52      ec.pushObject(getContext());
53    }
54  
55    void processScanAttrib(Attributes attributes) {
56      String scanAttrib = attributes.getValue(SCAN_ATTR);
57      if (!OptionHelper.isEmpty(scanAttrib)
58          && !"false".equalsIgnoreCase(scanAttrib)) {
59        ReconfigureOnChangeFilter rocf = new ReconfigureOnChangeFilter();
60        rocf.setContext(context);
61        String scanPeriodAttrib = attributes.getValue(SCAN_PERIOD_ATTR);
62        if (!OptionHelper.isEmpty(scanPeriodAttrib)) {
63          try {
64            Duration duration = Duration.valueOf(scanPeriodAttrib);
65            rocf.setRefreshPeriod(duration.getMilliseconds());
66            addInfo("Setting ReconfigureOnChangeFilter scanning period to "
67                + duration);
68          } catch (NumberFormatException nfe) {
69            addError("Error while converting [" + scanAttrib + "] to long", nfe);
70          }
71        }
72        rocf.start();
73        LoggerContext lc = (LoggerContext) context;
74        addInfo("Adding ReconfigureOnChangeFilter as a turbo filter");
75        lc.addTurboFilter(rocf);
76      }
77    }
78  
79    public void end(InterpretationContext ec, String name) {
80      if (debugMode) {
81        addInfo("End of configuration.");
82        LoggerContext loggerContext = (LoggerContext) context;
83        StatusPrinter.print(loggerContext, threshold);
84      }
85      ec.popObject();
86    }
87  }