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