1
2
3
4
5
6
7
8
9
10
11
12
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
33 inError = false;
34
35
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
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 }