001: /**********************************************************************************
002: *
003: * Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,
004: * Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
005: *
006: * Licensed under the Educational Community License Version 1.0 (the "License");
007: * By obtaining, using and/or copying this Original Work, you agree that you have read,
008: * understand, and will comply with the terms and conditions of the Educational Community License.
009: * You may obtain a copy of the License at:
010: *
011: * http://cvs.sakaiproject.org/licenses/license_1_0.html
012: *
013: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
014: * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
015: * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
016: * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
017: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
018: *
019: **********************************************************************************/package edu.indiana.lib.twinpeaks.util;
020:
021: import java.lang.*;
022: import java.util.*;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import javax.servlet.http.*;
028: import javax.xml.parsers.*;
029:
030: import org.w3c.dom.*;
031: import org.w3c.dom.html.*;
032: import org.xml.sax.*;
033:
034: public class LogUtils {
035: /**
036: * Get a Log instance
037: * @param logClass Java Class being logged
038: */
039: public static Log getLog(Class logClass) {
040: return LogFactory.getLog(logClass);
041: }
042:
043: /**
044: * Get a Log instance
045: * @param logName Name being logged
046: */
047: public static Log getLog(String logName) {
048: return LogFactory.getLog(logName);
049: }
050:
051: /**
052: * Serialize an XML object (Document or Element) to the log
053: * @param log Apache Log object
054: * @param recordElement The XML object to disolay (Document, Element)
055: */
056: public static void displayXml(org.apache.commons.logging.Log log,
057: Object xmlObject) {
058: displayXml(log, null, xmlObject);
059: }
060:
061: /**
062: * Serialize an XML object (Document or Element) to the log (with an
063: * optional warning header)
064: * @param log Apache Log object
065: * @param errorText Error message (null for none)
066: * @param recordElement The XML object to disolay (Document, Element)
067: */
068: public static void displayXml(org.apache.commons.logging.Log log,
069: String errorText, Object xmlObject) {
070:
071: if (!(xmlObject instanceof Document)
072: && !(xmlObject instanceof Element)) {
073: throw new IllegalArgumentException(
074: "Unexpected object for serialzation: "
075: + xmlObject.toString());
076: }
077:
078: if (!StringUtils.isNull(errorText)) {
079: log.error(errorText);
080: }
081:
082: log
083: .info("Record Start ----------------------------------------");
084:
085: try {
086: log.info(DomUtils.serialize(xmlObject));
087: } catch (DomException exception) {
088: log.error("Failed to serialize element "
089: + xmlObject.toString(), exception);
090: }
091: log
092: .info("Record End ------------------------------------------");
093: }
094:
095: /*
096: * Test
097: */
098: public static void main(String[] args) {
099:
100: Log log = LogUtils.getLog(LogUtils.class);
101:
102: log.debug("Debug");
103: log.error("Error");
104: log.warn("Warn");
105: log.info("Info");
106: }
107: }
|