View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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.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   * Action which handles <logger> elements in configuration files.
28   * 
29   * @author Ceki Gulcu
30   */
31  public class LoggerAction extends Action {
32      public static final String LEVEL_ATTRIBUTE = "level";
33  
34      boolean inError = false;
35      Logger logger;
36  
37      public void begin(InterpretationContext ec, String name, Attributes attributes) {
38          // Let us forget about previous errors (in this object)
39          inError = false;
40          logger = null;
41  
42          LoggerContext loggerContext = (LoggerContext) this.context;
43  
44          String loggerName = ec.subst(attributes.getValue(NAME_ATTRIBUTE));
45  
46          if (OptionHelper.isEmpty(loggerName)) {
47              inError = true;
48              String aroundLine = getLineColStr(ec);
49              String errorMsg = "No 'name' attribute in element " + name + ", around " + aroundLine;
50              addError(errorMsg);
51              return;
52          }
53  
54          logger = loggerContext.getLogger(loggerName);
55  
56          String levelStr = ec.subst(attributes.getValue(LEVEL_ATTRIBUTE));
57  
58          if (!OptionHelper.isEmpty(levelStr)) {
59              if (ActionConst.INHERITED.equalsIgnoreCase(levelStr) || ActionConst.NULL.equalsIgnoreCase(levelStr)) {
60                  addInfo("Setting level of logger [" + loggerName + "] to null, i.e. INHERITED");
61                  logger.setLevel(null);
62              } else {
63                  Level level = Level.toLevel(levelStr);
64                  addInfo("Setting level of logger [" + loggerName + "] to " + level);
65                  logger.setLevel(level);
66              }
67          }
68  
69          String additivityStr = ec.subst(attributes.getValue(ActionConst.ADDITIVITY_ATTRIBUTE));
70          if (!OptionHelper.isEmpty(additivityStr)) {
71              boolean additive = OptionHelper.toBoolean(additivityStr, true);
72              addInfo("Setting additivity of logger [" + loggerName + "] to " + additive);
73              logger.setAdditive(additive);
74          }
75          ec.pushObject(logger);
76      }
77  
78      public void end(InterpretationContext ec, String e) {
79          if (inError) {
80              return;
81          }
82          Object o = ec.peekObject();
83          if (o != logger) {
84              addWarn("The object on the top the of the stack is not " + logger + " pushed earlier");
85              addWarn("It is: " + o);
86          } else {
87              ec.popObject();
88          }
89      }
90  
91      public void finish(InterpretationContext ec) {
92      }
93  }