You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by sd...@apache.org on 2003/05/05 03:03:47 UTC
cvs commit: jakarta-log4j-sandbox/src/java/org/apache/log4j Decoder.java UtilLoggingLevel.java
sdeboy 2003/05/04 18:03:46
Modified: src/java/org/apache/log4j/xml XMLDecoder.java
Added: src/java/org/apache/log4j/xml UtilLoggingEntityResolver.java
UtilLoggingXMLDecoder.java logger.dtd
src/java/org/apache/log4j Decoder.java UtilLoggingLevel.java
Log:
Added support for reception of java.util.logging (JDK1.4) events via their sockethandler and xmlformatter.
Revision Changes Path
1.5 +7 -3 jakarta-log4j-sandbox/src/java/org/apache/log4j/xml/XMLDecoder.java
Index: XMLDecoder.java
===================================================================
RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/xml/XMLDecoder.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- XMLDecoder.java 1 May 2003 04:50:26 -0000 1.4
+++ XMLDecoder.java 5 May 2003 01:03:46 -0000 1.5
@@ -63,6 +63,7 @@
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import org.apache.log4j.Decoder;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;
@@ -83,7 +84,7 @@
* @author Paul Smith <ps...@apache.org>
*
*/
-public class XMLDecoder {
+public class XMLDecoder implements Decoder {
private static final String beginPart =
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?><!DOCTYPE log4j:eventSet SYSTEM \"log4j.dtd\"><log4j:eventSet version=\"1.2\" xmlns:log4j=\"http://jakarta.apache.org/log4j/\">";
private static final String endPart = "</log4j:eventSet>";
@@ -204,6 +205,11 @@
return decodeEvents(doc);
}
+ public Vector decodeEvents(String document) {
+ Document doc=parse(document);
+ return decodeEvents(doc);
+ }
+
/**
* Converts the string data into an XML Document, and then soaks out the
* relevant bits to form a new LoggingEvent instance which can be used
@@ -300,8 +306,6 @@
}
}
- //NOTE: this process unfortunately adds the stack trace of the caller because
- //we don't have an ability to modify the stack trace pre jdk1.4
if (tagName.equalsIgnoreCase("log4j:throwable")) {
exception = new String[]{getCData(list.item(y))};
}
1.1 jakarta-log4j-sandbox/src/java/org/apache/log4j/xml/UtilLoggingEntityResolver.java
Index: UtilLoggingEntityResolver.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
* apache@apache.org.
*
* 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.log4j.xml;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.apache.log4j.helpers.LogLog;
/**
* An {@link EntityResolver} specifically designed to return
* <code>java 1.4's logging dtd, logger.dtd</code> which is embedded within the log4j jar
* file.
*
* @author Paul Austin
* */
public class UtilLoggingEntityResolver implements EntityResolver {
public InputSource resolveEntity (String publicId, String systemId) {
System.err.println("piblicID: ["+publicId+"]");
System.err.println("systemId: ["+systemId+"]");
if (systemId.endsWith("logger.dtd")) {
Class clazz = getClass();
InputStream in = clazz.getResourceAsStream("/org/apache/log4j/xml/logger.dtd");
if (in == null) {
LogLog.error("Could not find [logger.dtd]. Used [" + clazz.getClassLoader()
+ "] class loader in the search.");
return null;
} else {
return new InputSource(in);
}
} else {
return null;
}
}
}
1.1 jakarta-log4j-sandbox/src/java/org/apache/log4j/xml/UtilLoggingXMLDecoder.java
Index: UtilLoggingXMLDecoder.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
* apache@apache.org.
*
* 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.log4j.xml;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.Decoder;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.UtilLoggingLevel;
import org.apache.log4j.spi.LoggingEvent;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/**
* Decodes JDK 1.4's java.util.logging package events delivered via XML (using the logger.dtd).
*
* @author Scott Deboy <sd...@apache.org>
* @author Paul Smith <ps...@apache.org>
*
*/
public class UtilLoggingXMLDecoder implements Decoder {
//NOTE: xml section is only handed on first delivery of events
//on this first delivery of events, there is no end tag for the log element
private static final String beginPart =
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?><!DOCTYPE log SYSTEM \"logger.dtd\"><log>";
private static final String endPart = "</log>";
private StringBuffer buf = new StringBuffer();
private DocumentBuilderFactory dbf;
private DocumentBuilder docBuilder;
private Map additionalProperties = Collections.EMPTY_MAP;
public UtilLoggingXMLDecoder() {
dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
try {
docBuilder = dbf.newDocumentBuilder();
docBuilder.setErrorHandler(new SAXErrorHandler());
docBuilder.setEntityResolver(new UtilLoggingEntityResolver());
} catch (ParserConfigurationException pce) {
System.err.println("Unable to get document builder");
}
}
/**
* Sets an additionalProperty map, where each Key/Value pair is
* automatically added to each LoggingEvent as it is decoded.
*
* This is useful, say, to include the source file name of the Logging events
* @param additionalProperties
*/
public void setAdditionalProperties(Map additionalProperties) {
this.additionalProperties = additionalProperties;
}
/**
* Converts the LoggingEvent data in XML string format into an actual
* XML Document class instance.
* @param data
* @return
*/
private Document parse(String data) {
if (docBuilder == null) {
return null;
}
Document document = null;
try {
// we change the system ID to a valid URI so that Crimson won't
// complain. Indeed, "log4j.dtd" alone is not a valid URI which
// causes Crimson to barf. The Log4jEntityResolver only cares
// about the "log4j.dtd" ending.
/**
* resetting the length of the StringBuffer is dangerous, particularly
* on some JDK 1.4 impls, there's a known Bug that causes a memory leak
*/
if (data != null) {
data = data.trim();
}
buf = new StringBuffer(1024);
if (!data.startsWith("<?xml")) {
buf.append(beginPart);
}
buf.append(data);
if (!data.endsWith(endPart)) {
buf.append(endPart);
}
InputSource inputSource =
new InputSource(new StringReader(buf.toString()));
inputSource.setSystemId("dummy://logger.dtd");
document = docBuilder.parse(inputSource);
} catch (Exception e) {
e.printStackTrace();
}
return document;
}
/**
* Reads the contents of the file into a String
* @param file the file to load
* @return The contents of the file as a String
* @throws IOException if an error occurred during the loading process
*/
private String loadFileSource(File file) throws IOException {
LineNumberReader reader = null;
StringBuffer buf = new StringBuffer(1024);
try {
reader = new LineNumberReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
buf.append(line);
}
} catch (IOException e) {
throw e;
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
}
}
return buf.toString();
}
/**
* Decodes a File into a Vector of LoggingEvents
* @param file the file to decode events from
* @return Vector of LoggingEvents
* @throws IOException
*/
public Vector decode(File file) throws IOException {
String fileContents = loadFileSource(file);
Document doc = parse(fileContents);
return decodeEvents(doc);
}
public Vector decodeEvents(String document) {
if (document != null) {
document = document.trim();
if (document.equals("")) {
return null;
} else {
Document doc = parse(document);
if (doc != null) {
return decodeEvents(doc);
}
}
}
return null;
}
/**
* Converts the string data into an XML Document, and then soaks out the
* relevant bits to form a new LoggingEvent instance which can be used
* by any Log4j element locally.
* @param data
* @return a single LoggingEvent
*/
public LoggingEvent decode(String data) {
Document document = parse(data);
if (document == null) {
return null;
}
Vector events = decodeEvents(document);
if (events.size() > 0) {
return (LoggingEvent) events.firstElement();
}
return null;
}
/**
* Given a Document, converts the XML into a Vector of LoggingEvents
* @param document
* @return
*/
private Vector decodeEvents(Document document) {
Vector events = new Vector();
NodeList eventList = document.getElementsByTagName("record");
for (int eventIndex = 0;
eventIndex < eventList.getLength();
eventIndex++) {
Node eventNode = eventList.item(eventIndex);
Logger logger = null;
long timeStamp = 0L;
Level level = null;
String threadName = null;
Object message = null;
String ndc = null;
Hashtable mdc = null;
String[] exception = null;
String className = null;
String methodName = null;
String fileName = null;
String lineNumber = null;
Hashtable properties = new Hashtable();
//format of date: 2003-05-04T11:04:52
//ignore date or set as a property? using millis in constructor instead
NodeList list = eventNode.getChildNodes();
int listLength = list.getLength();
if (listLength == 0) {
continue;
}
for (int y = 0; y < listLength; y++) {
String tagName = list.item(y).getNodeName();
if (tagName.equalsIgnoreCase("logger")) {
logger = Logger.getLogger(getCData(list.item(y)));
}
if (tagName.equalsIgnoreCase("millis")) {
timeStamp = Long.parseLong(getCData(list.item(y)));
}
if (tagName.equalsIgnoreCase("level")) {
level = UtilLoggingLevel.toLevel(getCData(list.item(y)));
}
if (tagName.equalsIgnoreCase("thread")) {
threadName = getCData(list.item(y));
}
if (tagName.equalsIgnoreCase("sequence")) {
properties.put("log4jid", getCData(list.item(y)));
}
if (tagName.equalsIgnoreCase("message")) {
message = getCData(list.item(y));
}
if (tagName.equalsIgnoreCase("class")) {
className = getCData(list.item(y));
}
if (tagName.equalsIgnoreCase("method")) {
methodName = getCData(list.item(y));
}
if (tagName.equalsIgnoreCase("exception")) {
ArrayList exceptionList=new ArrayList();
NodeList exList = list.item(y).getChildNodes();
int exlistLength = exList.getLength();
for (int i2=0;i2<exlistLength;i2++) {
Node exNode = exList.item(i2);
String exName = exList.item(i2).getNodeName();
if (exName.equalsIgnoreCase("message")) {
exceptionList.add(getCData(exList.item(i2)));
}
if (exName.equalsIgnoreCase("frame")) {
NodeList exList2 = exNode.getChildNodes();
int exlist2Length=exList2.getLength();
for (int i3=0;i3<exlist2Length;i3++) {
exceptionList.add(getCData(exList2.item(i3))+"\n");
}
}
}
exception=(String[]) exceptionList.toArray(new String[exceptionList.size()]);
}
}
/**
* We add all the additional properties to the properties
* hashtable
*/
if (additionalProperties.size() > 0) {
if (properties == null) {
properties = new Hashtable(additionalProperties);
} else {
properties.putAll(additionalProperties);
}
}
events.add(
new LoggingEvent(
logger.getName(),
logger,
timeStamp,
level,
threadName,
message,
ndc,
mdc,
exception,
className,
methodName,
fileName,
lineNumber,
properties));
}
return events;
}
private String getCData(Node n) {
StringBuffer buf = new StringBuffer();
NodeList nl = n.getChildNodes();
for (int x = 0; x < nl.getLength(); x++) {
Node innerNode = nl.item(x);
if ((innerNode.getNodeType() == Node.TEXT_NODE)
|| (innerNode.getNodeType() == Node.CDATA_SECTION_NODE)) {
buf.append(innerNode.getNodeValue());
}
}
return buf.toString();
}
}
1.1 jakarta-log4j-sandbox/src/java/org/apache/log4j/xml/logger.dtd
Index: logger.dtd
===================================================================
<!-- DTD used by the java.util.logging.XMLFormatter -->
<!-- This provides an XML formatted log message. -->
<!-- The document type is "log" which consists of a sequence
of record elements -->
<!ELEMENT log (record*)>
<!-- Each logging call is described by a record element. -->
<!ELEMENT record (date, millis, sequence, logger?, level,
class?, method?, thread?, message, key?, catalog?, param*, exception?)>
<!-- Date and time when LogRecord was created in ISO 8601 format -->
<!ELEMENT date (#PCDATA)>
<!-- Time when LogRecord was created in milliseconds since
midnight January 1st, 1970, UTC. -->
<!ELEMENT millis (#PCDATA)>
<!-- Unique sequence number within source VM. -->
<!ELEMENT sequence (#PCDATA)>
<!-- Name of source Logger object. -->
<!ELEMENT logger (#PCDATA)>
<!-- Logging level, may be either one of the constant
names from java.util.logging.Constants (such as "SEVERE"
or "WARNING") or an integer value such as "20". -->
<!ELEMENT level (#PCDATA)>
<!-- Fully qualified name of class that issued
logging call, e.g. "javax.marsupial.Wombat". -->
<!ELEMENT class (#PCDATA)>
<!-- Name of method that issued logging call.
It may be either an unqualified method name such as
"fred" or it may include argument type information
in parenthesis, for example "fred(int,String)". -->
<!ELEMENT method (#PCDATA)>
<!-- Integer thread ID. -->
<!ELEMENT thread (#PCDATA)>
<!-- The message element contains the text string of a log message. -->
<!ELEMENT message (#PCDATA)>
<!-- If the message string was localized, the key element provides
the original localization message key. -->
<!ELEMENT key (#PCDATA)>
<!-- If the message string was localized, the catalog element provides
the logger's localization resource bundle name. -->
<!ELEMENT catalog (#PCDATA)>
<!-- If the message string was localized, each of the param elements
provides the String value (obtained using Object.toString())
of the corresponding LogRecord parameter. -->
<!ELEMENT param (#PCDATA)>
<!-- An exception consists of an optional message string followed
by a series of StackFrames. Exception elements are used
for Java exceptions and other java Throwables. -->
<!ELEMENT exception (message?, frame+)>
<!-- A frame describes one line in a Throwable backtrace. -->
<!ELEMENT frame (class, method, line?)>
<!-- an integer line number within a class's source file. -->
<!ELEMENT line (#PCDATA)>
1.1 jakarta-log4j-sandbox/src/java/org/apache/log4j/Decoder.java
Index: Decoder.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
* apache@apache.org.
*
* 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.log4j;
import java.util.Vector;
import org.apache.log4j.spi.LoggingEvent;
/**
* Allow LoggingEvents to be reconstructed from a different format (usually xml).
*
* @author Scott Deboy <sd...@apache.org>
*
*/
public interface Decoder {
Vector decodeEvents(String document);
LoggingEvent decode(String event);
}
1.1 jakarta-log4j-sandbox/src/java/org/apache/log4j/UtilLoggingLevel.java
Index: UtilLoggingLevel.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
* apache@apache.org.
*
* 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.log4j;
import java.util.ArrayList;
import java.util.List;
/**
* An extension of the Level class that provides support for java.util.logging
* Levels.
*
* @author Scott Deboy <sd...@apache.org>
*
*/
public class UtilLoggingLevel extends Level {
public static final int SEVERE_INT = 1000;
public static final int WARNING_INT = 900;
public static final int INFO_INT = 800;
public static final int CONFIG_INT = 700;
public static final int FINE_INT = 500;
public static final int FINER_INT = 400;
public static final int FINEST_INT = 300;
public static final int UNKNOWN_INT = 200;
public static final UtilLoggingLevel SEVERE = new UtilLoggingLevel(SEVERE_INT, "SEVERE", 0);
public static final UtilLoggingLevel WARNING = new UtilLoggingLevel(WARNING_INT, "WARNING", 4);
public static final UtilLoggingLevel INFO = new UtilLoggingLevel(INFO_INT, "INFO", 5);
public static final UtilLoggingLevel CONFIG = new UtilLoggingLevel(CONFIG_INT, "CONFIG", 6);
public static final UtilLoggingLevel FINE = new UtilLoggingLevel(FINE_INT, "FINE", 7);
public static final UtilLoggingLevel FINER = new UtilLoggingLevel(FINER_INT, "FINER", 8);
public static final UtilLoggingLevel FINEST = new UtilLoggingLevel(FINEST_INT, "FINEST", 9);
protected UtilLoggingLevel(int level, String levelStr, int syslogEquivalent) {
super(level, levelStr, syslogEquivalent);
}
/**
Convert an integer passed as argument to a level. If the
conversion fails, then this method returns the specified default.
*/
public static UtilLoggingLevel toLevel(int val, UtilLoggingLevel defaultLevel) {
switch (val) {
case SEVERE_INT:
return SEVERE;
case WARNING_INT:
return WARNING;
case INFO_INT:
return INFO;
case CONFIG_INT:
return CONFIG;
case FINE_INT:
return FINE;
case FINER_INT:
return FINER;
case FINEST_INT:
return FINEST;
default:
return FINEST;
}
}
public static Level toLevel(int val) {
return toLevel(val, FINEST);
}
public static List getAllPossibleLevels() {
ArrayList list=new ArrayList();
list.add(Level.DEBUG);
list.add(INFO);
list.add(Level.WARN);
list.add(Level.ERROR);
list.add(Level.FATAL);
list.add(FINE);
list.add(FINER);
list.add(FINEST);
list.add(CONFIG);
list.add(WARNING);
list.add(SEVERE);
return list;
}
public static Level toLevel(String s) {
return toLevel(s, Level.DEBUG);
}
public static Level toLevel(String sArg, Level defaultLevel) {
if (sArg == null) {
return defaultLevel;
}
String s = sArg.toUpperCase();
if (s.equals("SEVERE")) {
return SEVERE;
}
//if(s.equals("FINE")) return Level.FINE;
if (s.equals("WARNING")) {
return WARNING;
}
if (s.equals("INFO")) {
return INFO;
}
if (s.equals("CONFI")) {
return CONFIG;
}
if (s.equals("FINE")) {
return FINE;
}
if (s.equals("FINER")) {
return FINER;
}
if (s.equals("FINEST")) {
return FINEST;
}
return defaultLevel;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org