/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: ApplLogger.java,v 1.2 2006/09/29 12:32:13 drmlipp Exp $
*
* $Log: ApplLogger.java,v $
* Revision 1.2 2006/09/29 12:32:13 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.1.1.4 2004/08/18 15:17:35 drmlipp
* Update to 1.2
*
* Revision 1.10 2004/06/22 11:52:04 lipp
* Some fixes to match new commons-logging.
*
* Revision 1.9 2003/09/05 09:40:26 lipp
* Fixed paths for IBM JVM.
*
* Revision 1.8 2003/06/27 08:51:46 lipp
* Fixed copyright/license information.
*
* Revision 1.7 2002/11/28 22:52:08 lipp
* Added search in /etc and result output.
*
* Revision 1.6 2002/10/11 14:59:38 lipp
* Better initialization.
*
* Revision 1.5 2002/09/04 06:57:37 lipp
* Now using JBoss-3.0
*
* Revision 1.4 2002/01/22 08:48:14 lipp
* Javadoc fixes.
*
* Revision 1.3 2002/01/08 16:57:05 lipp
* Removed debug message.
*
* Revision 1.2 2002/01/05 19:35:03 lipp
* Final log solution.
*
* Revision 1.1 2002/01/04 13:01:24 lipp
* Loading application specific log4j classes (1. try).
*
*/
package de.danet.an.util.log4j;
import java.io.InputStream;
import java.net.URL;
import org.apache.log4j.Hierarchy;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.RootCategory;
import org.apache.log4j.xml.DOMConfigurator;
/**
* This class provides an alternate access to the log4j logging
* library. The necessity arises from the usage of log4j as logging
* package in the JBoss application server. As log4j is already
* included in JBoss' classpath and configured by JBoss, we cannot
* have really independent application level logging.<P>
*
* The central problem is the static default hierarchy used by
* <code>org.apache.log4j.Logger</code>. For a distinct application
* level logging we need an alternate hierarchy. Of course, we want to
* access this hierarchy as simple as
* <code>Logger.getInstance(...)</code> and thus we need a new class
* that provides the static acces to the alternate (application
* level) hierarchy.<P>
*
* Note that no static methods of other log4j classes may be used in
* the context of this application level logging, as they usually rely
* on <code>Logger.getDefaultHierarchy()</code> and that is not the
* application level hierarchy.<P>
*/
public class ApplLogger {
/**
* The hierarchy used in <code>ApplLogger</code>.
*/
public static final RootCategory rc = new RootCategory(Level.DEBUG);
public static final Hierarchy defaultHierarchy = new Hierarchy(rc);
private static URL cu = null;
static {
try {
// First try /etc
cu = Thread.currentThread().getContextClassLoader()
.getResource ("/etc/log4j-appl.xml");
if (cu == null) {
// above doesn't work under unknown circumstances ...
cu = ApplLogger.class.getResource ("/etc/log4j-appl.xml");
}
if (cu == null) {
// above doesn't work under unknown circumstances ...
cu = ClassLoader.getSystemResource ("/etc/log4j-appl.xml");
}
// Now try /
if (cu == null) {
cu = Thread.currentThread().getContextClassLoader()
.getResource ("log4j-appl.xml");
}
if (cu == null) {
// above doesn't work under unknown circumstances ...
cu = ApplLogger.class.getResource ("log4j-appl.xml");
}
if (cu == null) {
// above doesn't work under unknown circumstances ...
cu = ClassLoader.getSystemResource ("log4j-appl.xml");
}
if (cu == null) {
System.err.println
("FATAL: No \"log4j-appl.xml\" in classpath!");
}
InputStream cf = cu.openStream();
new DOMConfigurator().doConfigure (cf, defaultHierarchy);
} catch (Exception ex) {
System.err.println
("FATAL: Problem initializing logging:" + ex.getMessage());
ex.printStackTrace();
}
}
private static final org.apache.log4j.Logger logger
= de.danet.an.util.log4j.ApplLogger.getLogger (ApplLogger.class);
static {
logger.info ("Application logger hierarchy initialized from " + cu);
}
/**
* Return the default Hierarchy instance.
* @return the default hierarchy instance.
*/
public static Hierarchy getDefaultHierarchy() {
return defaultHierarchy;
}
/**
* Retrieve a category with named as the <code>name</code>
* parameter. If the named category already exists, then the existing
* instance will be reutrned. Otherwise, a new instance is created.
*
* By default, categories do not have a set priority but inherit it
* from the hierarchy. This is one of the central features of log4j.
*
* @param name The name of the category to retrieve.
* @return the logger
*/
public static Logger getLogger(String name) {
return defaultHierarchy.getLogger(name);
}
/**
* Shorthand for <code>getInstance(clazz.getName())</code>.
*
* @param clazz The name of <code>clazz</code> will be used as the
* name of the category to retrieve. See {@link
* #getLogger(String)} for more detailed information.
* @return the logger
*/
public static Logger getLogger(Class clazz) {
return getLogger(clazz.getName());
}
}
|