View Javadoc

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