1
2
3
4
5
6
7
8
9
10
11 package ch.qos.logback.classic.joran.action;
12
13 import org.xml.sax.Attributes;
14
15 import ch.qos.logback.classic.Level;
16 import ch.qos.logback.classic.Logger;
17 import ch.qos.logback.classic.LoggerContext;
18 import ch.qos.logback.core.joran.action.Action;
19 import ch.qos.logback.core.joran.action.ActionConst;
20 import ch.qos.logback.core.joran.spi.InterpretationContext;
21 import ch.qos.logback.core.util.OptionHelper;
22
23
24
25
26
27
28 public class LoggerAction extends Action {
29 public static final String LEVEL_ATTRIBUTE = "level";
30
31 boolean inError = false;
32 Logger logger;
33 public void begin(InterpretationContext ec, String name, Attributes attributes) {
34
35 inError = false;
36 logger = null;
37
38 LoggerContext loggerContext = (LoggerContext) this.context;
39
40 String loggerName = attributes.getValue(NAME_ATTRIBUTE);
41
42 if (OptionHelper.isEmpty(loggerName)) {
43 inError = true;
44 String aroundLine = getLineColStr(ec);
45 String errorMsg = "No 'name' attribute in element " + name + ", around " +aroundLine;
46 addError(errorMsg);
47 return;
48 }
49
50 logger = loggerContext.getLogger(loggerName);
51
52 String levelStr = ec.subst(attributes.getValue(LEVEL_ATTRIBUTE));
53
54 if (!OptionHelper.isEmpty(levelStr)) {
55 if (ActionConst.INHERITED.equalsIgnoreCase(levelStr)
56 || ActionConst.NULL.equalsIgnoreCase(levelStr)) {
57 addInfo("Setting level of logger [" + loggerName
58 + "] to null, i.e. INHERITED");
59 logger.setLevel(null);
60 } else {
61 Level level = Level.toLevel(levelStr);
62 addInfo("Setting level of logger [" + loggerName + "] to " + level);
63 logger.setLevel(level);
64 }
65 }
66
67 if (!OptionHelper.isEmpty(ActionConst.ADDITIVITY_ATTRIBUTE)) {
68 boolean additive = OptionHelper.toBoolean(attributes
69 .getValue(ActionConst.ADDITIVITY_ATTRIBUTE), true);
70 addInfo("Setting additivity of logger [" + loggerName + "] to "
71 + additive);
72 logger.setAdditive(additive);
73 }
74 ec.pushObject(logger);
75 }
76
77 public void end(InterpretationContext ec, String e) {
78 if (inError) {
79 return;
80 }
81 Object o = ec.peekObject();
82 if (o != logger) {
83 addWarn("The object on the top the of the stack is not "+logger+" pushed earlier");
84 addWarn("It is: " + o);
85 } else {
86 ec.popObject();
87 }
88 }
89
90 public void finish(InterpretationContext ec) {
91 }
92 }