001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: ApplLogger.java,v 1.2 2006/09/29 12:32:13 drmlipp Exp $
021: *
022: * $Log: ApplLogger.java,v $
023: * Revision 1.2 2006/09/29 12:32:13 drmlipp
024: * Consistently using WfMOpen as projct name now.
025: *
026: * Revision 1.1.1.4 2004/08/18 15:17:35 drmlipp
027: * Update to 1.2
028: *
029: * Revision 1.10 2004/06/22 11:52:04 lipp
030: * Some fixes to match new commons-logging.
031: *
032: * Revision 1.9 2003/09/05 09:40:26 lipp
033: * Fixed paths for IBM JVM.
034: *
035: * Revision 1.8 2003/06/27 08:51:46 lipp
036: * Fixed copyright/license information.
037: *
038: * Revision 1.7 2002/11/28 22:52:08 lipp
039: * Added search in /etc and result output.
040: *
041: * Revision 1.6 2002/10/11 14:59:38 lipp
042: * Better initialization.
043: *
044: * Revision 1.5 2002/09/04 06:57:37 lipp
045: * Now using JBoss-3.0
046: *
047: * Revision 1.4 2002/01/22 08:48:14 lipp
048: * Javadoc fixes.
049: *
050: * Revision 1.3 2002/01/08 16:57:05 lipp
051: * Removed debug message.
052: *
053: * Revision 1.2 2002/01/05 19:35:03 lipp
054: * Final log solution.
055: *
056: * Revision 1.1 2002/01/04 13:01:24 lipp
057: * Loading application specific log4j classes (1. try).
058: *
059: */
060:
061: package de.danet.an.util.log4j;
062:
063: import java.io.InputStream;
064:
065: import java.net.URL;
066:
067: import org.apache.log4j.Hierarchy;
068: import org.apache.log4j.Level;
069: import org.apache.log4j.Logger;
070: import org.apache.log4j.spi.RootCategory;
071: import org.apache.log4j.xml.DOMConfigurator;
072:
073: /**
074: * This class provides an alternate access to the log4j logging
075: * library. The necessity arises from the usage of log4j as logging
076: * package in the JBoss application server. As log4j is already
077: * included in JBoss' classpath and configured by JBoss, we cannot
078: * have really independent application level logging.<P>
079: *
080: * The central problem is the static default hierarchy used by
081: * <code>org.apache.log4j.Logger</code>. For a distinct application
082: * level logging we need an alternate hierarchy. Of course, we want to
083: * access this hierarchy as simple as
084: * <code>Logger.getInstance(...)</code> and thus we need a new class
085: * that provides the static acces to the alternate (application
086: * level) hierarchy.<P>
087: *
088: * Note that no static methods of other log4j classes may be used in
089: * the context of this application level logging, as they usually rely
090: * on <code>Logger.getDefaultHierarchy()</code> and that is not the
091: * application level hierarchy.<P>
092: */
093: public class ApplLogger {
094:
095: /**
096: * The hierarchy used in <code>ApplLogger</code>.
097: */
098: public static final RootCategory rc = new RootCategory(Level.DEBUG);
099: public static final Hierarchy defaultHierarchy = new Hierarchy(rc);
100:
101: private static URL cu = null;
102: static {
103: try {
104: // First try /etc
105: cu = Thread.currentThread().getContextClassLoader()
106: .getResource("/etc/log4j-appl.xml");
107: if (cu == null) {
108: // above doesn't work under unknown circumstances ...
109: cu = ApplLogger.class
110: .getResource("/etc/log4j-appl.xml");
111: }
112: if (cu == null) {
113: // above doesn't work under unknown circumstances ...
114: cu = ClassLoader
115: .getSystemResource("/etc/log4j-appl.xml");
116: }
117:
118: // Now try /
119: if (cu == null) {
120: cu = Thread.currentThread().getContextClassLoader()
121: .getResource("log4j-appl.xml");
122: }
123: if (cu == null) {
124: // above doesn't work under unknown circumstances ...
125: cu = ApplLogger.class.getResource("log4j-appl.xml");
126: }
127: if (cu == null) {
128: // above doesn't work under unknown circumstances ...
129: cu = ClassLoader.getSystemResource("log4j-appl.xml");
130: }
131: if (cu == null) {
132: System.err
133: .println("FATAL: No \"log4j-appl.xml\" in classpath!");
134: }
135: InputStream cf = cu.openStream();
136: new DOMConfigurator().doConfigure(cf, defaultHierarchy);
137: } catch (Exception ex) {
138: System.err.println("FATAL: Problem initializing logging:"
139: + ex.getMessage());
140: ex.printStackTrace();
141: }
142: }
143: private static final org.apache.log4j.Logger logger = de.danet.an.util.log4j.ApplLogger
144: .getLogger(ApplLogger.class);
145: static {
146: logger.info("Application logger hierarchy initialized from "
147: + cu);
148: }
149:
150: /**
151: * Return the default Hierarchy instance.
152: * @return the default hierarchy instance.
153: */
154: public static Hierarchy getDefaultHierarchy() {
155: return defaultHierarchy;
156: }
157:
158: /**
159: * Retrieve a category with named as the <code>name</code>
160: * parameter. If the named category already exists, then the existing
161: * instance will be reutrned. Otherwise, a new instance is created.
162: *
163: * By default, categories do not have a set priority but inherit it
164: * from the hierarchy. This is one of the central features of log4j.
165: *
166: * @param name The name of the category to retrieve.
167: * @return the logger
168: */
169: public static Logger getLogger(String name) {
170: return defaultHierarchy.getLogger(name);
171: }
172:
173: /**
174: * Shorthand for <code>getInstance(clazz.getName())</code>.
175: *
176: * @param clazz The name of <code>clazz</code> will be used as the
177: * name of the category to retrieve. See {@link
178: * #getLogger(String)} for more detailed information.
179: * @return the logger
180: */
181: public static Logger getLogger(Class clazz) {
182: return getLogger(clazz.getName());
183: }
184: }
|