ceki 2003/09/02 11:36:14
Added: src/java/org/apache/joran/action RootLoggerAction.java
LoggerAction.java LevelAction.java Action.java
Log:
Joran can now configure loggers and set their levels. (It still cannot
configure appenders or attach them to loggers.)
Revision Changes Path
1.1
jakarta-log4j/src/java/org/apache/joran/action/RootLoggerAction.java
Index: RootLoggerAction.java
===================================================================
package org.apache.joran.action;
import org.apache.joran.ExecutionContext;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerRepository;
import org.w3c.dom.Element;
public class RootLoggerAction extends Action {
static final String NAME_ATTR = "name";
static final String CLASS_ATTR = "class";
static final String ADDITIVITY_ATTR = "additivity";
static final String EMPTY_STR = "";
static final Class[] ONE_STRING_PARAM = new Class[] { String.class };
Logger logger = Logger.getLogger(RootLoggerAction.class);
public void begin(ExecutionContext ec, Element loggerElement) {
logger.debug("In begin method");
LoggerRepository repository = (LoggerRepository) ec.getObject(0);
Logger root = repository.getRootLogger();
}
public void end(ExecutionContext ec, Element e) {
}
public void finish(ExecutionContext ec) {
}
}
1.1 jakarta-log4j/src/java/org/apache/joran/action/LoggerAction.java
Index: LoggerAction.java
===================================================================
package org.apache.joran.action;
import org.apache.joran.ExecutionContext;
import org.apache.log4j.Logger;
import org.apache.log4j.helpers.Loader;
import org.apache.log4j.helpers.OptionConverter;
import org.apache.log4j.spi.LoggerRepository;
import org.w3c.dom.Element;
import java.lang.reflect.Method;
public class LoggerAction extends Action {
static final String NAME_ATTR = "name";
static final String CLASS_ATTR = "class";
static final String ADDITIVITY_ATTR = "additivity";
static final String EMPTY_STR = "";
static final Class[] ONE_STRING_PARAM = new Class[] { String.class };
Logger logger = Logger.getLogger(LoggerAction.class);
public void begin(ExecutionContext ec, Element loggerElement) {
LoggerRepository repository = (LoggerRepository) ec.getObject(0);
// Create a new org.apache.log4j.Category object from the <category> element.
String loggerName = loggerElement.getAttribute(NAME_ATTR);
if(loggerName == null || EMPTY_STR.equals(loggerName)) {
inError = true;
String errorMsg = "No 'name' attribute in element "
+loggerElement.getTagName();
logger.warn(errorMsg);
ec.addError(errorMsg);
return;
}
logger.debug("Logger name is ["+loggerName+"].");
Logger l;
String className = loggerElement.getAttribute(CLASS_ATTR);
if (EMPTY_STR.equals(className)) {
logger.debug("Retreiving an instance of org.apache.log4j.Logger.");
l = repository.getLogger(loggerName);
} else {
logger.debug("Desired logger sub-class: [" + className + ']');
try {
Class clazz = Loader.loadClass(className);
Method getInstanceMethod =
clazz.getMethod("getLogger", ONE_STRING_PARAM);
l = (Logger) getInstanceMethod.invoke(null, new Object[] { loggerName });
} catch (Exception oops) {
logger.error(
"Could not retrieve category ["
+ loggerName
+ "]. Reported error follows.",
oops);
return;
}
}
boolean additivity =
OptionConverter.toBoolean(
loggerElement.getAttribute(ADDITIVITY_ATTR),
true);
logger.debug(
"Setting [" + l.getName() + "] additivity to [" + additivity + "].");
l.setAdditivity(additivity);
logger.debug("Pushing logger named ["+loggerName+"].");
ec.pushObject(l);
}
public void end(ExecutionContext ec, Element e) {
logger.debug("end() called.");
if(!inError) {
logger.debug("Removing logger from stack.");
ec.popObject();
}
}
public void finish(ExecutionContext ec) {
}
}
1.1 jakarta-log4j/src/java/org/apache/joran/action/LevelAction.java
Index: LevelAction.java
===================================================================
package org.apache.joran.action;
import org.apache.joran.ExecutionContext;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.helpers.Loader;
import org.apache.log4j.helpers.OptionConverter;
import org.w3c.dom.Element;
import java.lang.reflect.Method;
public class LevelAction extends Action {
final static Logger logger = Logger.getLogger(LevelAction.class);
static final String VALUE_ATTR = "value";
static final String CLASS_ATTR = "class";
static final String INHERITED = "INHERITED";
static final String NULL = "NULL";
static final String EMPTY_STR = "";
static final Class[] ONE_STRING_PARAM = new Class[] { String.class };
public void begin(ExecutionContext ec, Element element) {
Object o = ec.peekObject();
if(!(o instanceof Logger)) {
logger.warn("Could not find a logger at the top of execution
stack.");
inError = true;
ec.addError("For element <level>, could not find a logger at
the top of execution stack.");
return;
}
Logger l = (Logger) o;
String loggerName = l.getName();
String levelStr = element.getAttribute(VALUE_ATTR);
logger.debug(
"Level value for logger [" + loggerName + "] is [" + levelStr + "].");
if (INHERITED.equalsIgnoreCase(levelStr)
|| NULL.equalsIgnoreCase(levelStr)) {
l.setLevel(null);
} else {
String className = element.getAttribute(CLASS_ATTR);
if (EMPTY_STR.equals(className)) {
l.setLevel(OptionConverter.toLevel(levelStr, Level.DEBUG));
} else {
logger.debug("Desired Level sub-class: [" + className + ']');
try {
Class clazz = Loader.loadClass(className);
Method toLevelMethod = clazz.getMethod("toLevel", ONE_STRING_PARAM);
Level pri =
(Level) toLevelMethod.invoke(null, new Object[] { levelStr });
l.setLevel(pri);
} catch (Exception oops) {
logger.error(
"Could not create level [" + levelStr + "]. Reported error follows.",
oops);
return;
}
}
}
logger.debug(loggerName + " level set to " + l.getLevel());
}
public void end(ExecutionContext ec, Element e) {
}
public void finish(ExecutionContext ec) {
}
}
1.1 jakarta-log4j/src/java/org/apache/joran/action/Action.java
Index: Action.java
===================================================================
/*
* ============================================================================
* The Apache Software License, Version 1.1
* ============================================================================
*
* Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment: "This product includes software
* developed by the Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The names "log4j" and "Apache Software Foundation" must not be used to
* endorse or promote products derived from this software without prior
* written permission. For written permission, please contact
* [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache", nor may
* "Apache" appear in their name, without prior written permission of the
* Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* on behalf of the Apache Software Foundation. For more information on the
* Apache Software Foundation, please see <http://www.apache.org/>.
*
*/
package org.apache.joran.action;
import org.apache.joran.ExecutionContext;
import org.w3c.dom.Element;
/**
*
* Most of the work for configuring log4j is done by Actions.
*
* Methods of an Action are invoked while an XML file is parsed through.
*
* This class is largely copied from the relevant class in the commons-digester
* project of the Apache Software Foundation.
*
* @author Craig McClanahan
* @authro Christopher Lenz
* @author Ceki Gülcü
*
*/
public abstract class Action {
/**
* When actions encounter an error condition they set this variable to true.
*/
protected boolean inError = false;
/**
* Called when the parser first encounters an element.
*
* The return value indicates whether child elements should be processed. If
* the returned value is 'false', then child elements are ignored.
*/
public abstract void begin(ExecutionContext ec, Element e);
public abstract void end(ExecutionContext ec, Element e);
public abstract void finish(ExecutionContext ec);
public String toString() {
return this.getClass().getName();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]