View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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    public void begin(InterpretationContext ec, String name, Attributes attributes) {
37      // Let us forget about previous errors (in this object)
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  }