001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.logging;
019:
020: import org.apache.log4j.Hierarchy;
021: import org.apache.log4j.Level;
022: import org.apache.log4j.Logger;
023: import org.apache.log4j.helpers.LogLog;
024: import org.apache.log4j.spi.RootCategory;
025: import org.apache.log4j.xml.DOMConfigurator;
026:
027: import de.finix.contelligent.exception.ContelligentException;
028:
029: /**
030: * This class is used to configure the logging mechanism and to obtain our own
031: * <code>Hierarchy</code> from Log4J, the real work is done by the Log4j
032: * package. <BR>
033: * The LoggingService defines a Log4j <code>Hierarchy</code> where all used
034: * <code>Logger</code> instances reside in so we don't interact with other
035: * classes using Log4j in the same VM. For that reason all categories must be
036: * created using the static {@link #getLogger} method of this class, for
037: * example: <BR>
038: *
039: * <PRE>
040: *
041: * org.apache.log4j.Logger myLog = LoggingService.getLogger(MyClass.class); ...
042: * myLog.info("hello world!");
043: *
044: * </PRE>
045: *
046: * <BR>
047: * Look at the <A
048: * href="http://jakarta.apache.org/log4j/docs/index.html">Log4j-Homepage</A>
049: * for further information about Log4j.
050: * <P>
051: * To configure Log4j an XML file is used. The name of this file must be passed
052: * when {@link #init initializing} this class.
053: */
054: final public class LoggingService {
055: // we use our own hierarchy so we don't have to bother with other classes
056: // using log4j in the same VM.
057: static final Hierarchy ourHierarchy = new Hierarchy(
058: new RootCategory(Level.INFO));
059:
060: static Logger log = null; // the logger for this class
061:
062: /** enable or disable log4j's internal logging (optional) */
063: final static private boolean log4InternalDebugging = false;
064:
065: /** enable or disable the whole logging (optional) */
066: // final static private boolean loggingOn = true;
067: /**
068: * avoid instantiation.
069: */
070: private LoggingService() {
071: }
072:
073: static public Hierarchy getHierarchy() {
074: return ourHierarchy;
075: }
076:
077: static public Logger getLogger(String loggerName) {
078: return ourHierarchy.getLogger(loggerName);
079: }
080:
081: static public Logger getLogger(Class aClass) {
082: return ourHierarchy.getLogger(aClass.getName());
083: }
084:
085: /**
086: * Initializes the logging service and log4j using class
087: * <code>org.apache.log4j.xml.DOMConfigurator</code> which takes an XML
088: * file to configure log4j.
089: *
090: * @param configFileName
091: * the name of the XML file to use as configuration for Log4J
092: */
093: public static synchronized void init(String configFileName)
094: throws ContelligentException {
095: try {
096: LogLog.setInternalDebugging(log4InternalDebugging); // disable/enable
097: // log4j
098: // internal
099: // logging.
100: DOMConfigurator domConfigurator = new DOMConfigurator();
101:
102: domConfigurator.doConfigure(configFileName, ourHierarchy);
103:
104: // if (!loggingOn) {
105: // ourHierarchy.disableAll();
106: // System.out.println("[LoggingService] ... .. logging is turned OFF
107: // !!");
108: // System.out.println("[LoggingService] ... .. started.");
109: // } else {
110: log = getLogger(LoggingService.class);
111: log.info("[LoggingService] started.");
112: // }
113: } catch (Exception e) {
114: throw new ContelligentException(
115: "Could not initialize log4j: " + e);
116: }
117: }
118:
119: }
|