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.core.joran.action;
15  
16  import org.xml.sax.Attributes;
17  
18  import ch.qos.logback.core.Appender;
19  import ch.qos.logback.core.CoreConstants;
20  import ch.qos.logback.core.joran.spi.InterpretationContext;
21  import ch.qos.logback.core.spi.AppenderAttachable;
22  import ch.qos.logback.core.util.OptionHelper;
23  
24  
25  import java.util.HashMap;
26  
27  public class AppenderRefAction extends Action {
28    boolean inError = false;
29  
30    @SuppressWarnings("unchecked")
31    public void begin(InterpretationContext ec, String tagName, Attributes attributes) {
32      // Let us forget about previous errors (in this object)
33      inError = false;
34  
35      // logger.debug("begin called");
36  
37      Object o = ec.peekObject();
38  
39      if (!(o instanceof AppenderAttachable)) {
40        String errMsg = "Could not find an AppenderAttachable at the top of execution stack. Near ["
41            + tagName + "] line " + getLineNumber(ec);
42        inError = true;
43        addError(errMsg);
44        return;
45      }
46  
47      AppenderAttachable appenderAttachable = (AppenderAttachable) o;
48  
49      String appenderName = ec.subst(attributes.getValue(ActionConst.REF_ATTRIBUTE));
50  
51      if (OptionHelper.isEmpty(appenderName)) {
52        // print a meaningful error message and return
53        String errMsg = "Missing appender ref attribute in <appender-ref> tag.";
54        inError = true;
55        addError(errMsg);
56  
57        return;
58      }
59  
60      HashMap appenderBag = (HashMap) ec.getObjectMap().get(
61          ActionConst.APPENDER_BAG);
62      Appender appender = (Appender) appenderBag.get(appenderName);
63  
64      if (appender == null) {
65        String msg = "Could not find an appender named [" + appenderName
66            + "]. Did you define it below instead of above in the configuration file?";
67        inError = true;
68        addError(msg);
69        addError("See " + CoreConstants.CODES_URL
70            + "#appender_order for more details.");
71        return;
72      }
73  
74      addInfo("Attaching appender named [" + appenderName + "] to "
75          + appenderAttachable);
76      appenderAttachable.addAppender(appender);
77    }
78  
79    public void end(InterpretationContext ec, String n) {
80    }
81  
82  }