Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: de/danet/an/util/log4j/ApplLogger.java


1   /*
2    * This file is part of the WfMCore/WfMOpen project.
3    * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
4    * All rights reserved.
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   * 
20   * $Id: ApplLogger.java,v 1.9 2003/09/05 09:40:26 lipp Exp $
21   *
22   * $Log: ApplLogger.java,v $
23   * Revision 1.9  2003/09/05 09:40:26  lipp
24   * Fixed paths for IBM JVM.
25   *
26   * Revision 1.8  2003/06/27 08:51:46  lipp
27   * Fixed copyright/license information.
28   *
29   * Revision 1.7  2002/11/28 22:52:08  lipp
30   * Added search in /etc and result output.
31   *
32   * Revision 1.6  2002/10/11 14:59:38  lipp
33   * Better initialization.
34   *
35   * Revision 1.5  2002/09/04 06:57:37  lipp
36   * Now using JBoss-3.0
37   *
38   * Revision 1.4  2002/01/22 08:48:14  lipp
39   * Javadoc fixes.
40   *
41   * Revision 1.3  2002/01/08 16:57:05  lipp
42   * Removed debug message.
43   *
44   * Revision 1.2  2002/01/05 19:35:03  lipp
45   * Final log solution.
46   *
47   * Revision 1.1  2002/01/04 13:01:24  lipp
48   * Loading application specific log4j classes (1. try).
49   *
50   */
51  
52  package de.danet.an.util.log4j;
53  
54  import java.io.InputStream;
55  
56  import java.net.URL;
57  
58  import org.apache.log4j.Category;
59  import org.apache.log4j.Hierarchy;
60  import org.apache.log4j.Level;
61  import org.apache.log4j.spi.RootCategory;
62  import org.apache.log4j.xml.DOMConfigurator;
63  
64  /**
65   * This class provides an alternate access to the log4j logging
66   * library. The necessity arises from the usage of log4j as logging
67   * package in the JBoss application server. As log4j is already
68   * included in JBoss' classpath and configured by JBoss, we cannot
69   * have really independent application level logging.<P>
70   *
71   * The central problem is the static default hierarchy used by
72   * <code>org.apache.log4j.Category</code>. For a distinct application
73   * level logging we need an alternate hierarchy. Of course, we want to
74   * access this hierarchy as simple as
75   * <code>Category.getInstance(...)</code> and thus we need a new class
76   * that provides the static acces to the alternate (application
77   * level) hierarchy.<P>
78   *
79   * Note that no static methods of other log4j classes may be used in
80   * the context of this application level logging, as they usually rely
81   * on <code>Category.getDefaultHierarchy()</code> and that is not the
82   * application level hierarchy.<P>
83   *
84   * The naming of this class and its methods has already been aligned
85   * with the upcoming renaming in log4j 1.2.
86   */
87  public class ApplLogger {
88  
89      /**
90       * The hierarchy used in <code>ApplLogger</code>.
91       */
92      public static final RootCategory rc = new RootCategory(Level.DEBUG);
93      public static final Hierarchy defaultHierarchy = new Hierarchy(rc);
94  
95      private static URL cu = null;
96      static {
97    try {
98        // First try /etc
99        cu = Thread.currentThread().getContextClassLoader()
100     .getResource ("/etc/log4j-appl.xml");
101       if (cu == null) {
102     // above doesn't work under unknown circumstances ...
103     cu = ApplLogger.class.getResource ("/etc/log4j-appl.xml");
104       }
105       if (cu == null) {
106     // above doesn't work under unknown circumstances ...
107     cu = ClassLoader.getSystemResource ("/etc/log4j-appl.xml");
108       }
109 
110       // Now try /
111       if (cu == null) {
112     cu = Thread.currentThread().getContextClassLoader()
113         .getResource ("log4j-appl.xml");
114       }
115       if (cu == null) {
116     // above doesn't work under unknown circumstances ...
117     cu = ApplLogger.class.getResource ("log4j-appl.xml");
118       }
119       if (cu == null) {
120     // above doesn't work under unknown circumstances ...
121     cu = ClassLoader.getSystemResource ("log4j-appl.xml");
122       }
123       if (cu == null) {
124     System.err.println 
125         ("FATAL: No \"log4j-appl.xml\" in classpath!");
126       }
127       InputStream cf = cu.openStream();
128       new DOMConfigurator().doConfigure (cf, defaultHierarchy);
129   } catch (Exception ex) {
130       System.err.println 
131     ("FATAL: Problem initializing logging:" + ex.getMessage());
132       ex.printStackTrace();
133   }
134     }
135     private static final org.apache.log4j.Category logger
136   = de.danet.an.util.log4j.ApplLogger.getLogger (ApplLogger.class);
137     static {
138   logger.info ("Application logger hierarchy initialized from " + cu);
139     }
140 
141 
142     /**
143      * Return the default Hierarchy instance.
144      * @return the default hierarchy instance.
145      */
146     public static Hierarchy getDefaultHierarchy() {
147   return defaultHierarchy;
148     }
149 
150     /**
151      * Retrieve a category with named as the <code>name</code>
152      * parameter. If the named category already exists, then the existing
153      * instance will be reutrned. Otherwise, a new instance is created.
154      *
155      * By default, categories do not have a set priority but inherit it
156      * from the hierarchy. This is one of the central features of log4j.
157      *
158      * @param name The name of the category to retrieve.  
159      * @return the logger (<code>Category</code>).
160      */
161     public static Category getLogger(String name) {
162   return defaultHierarchy.getLogger(name);
163     }
164 
165     /**
166      * Shorthand for <code>getInstance(clazz.getName())</code>.
167      * 
168      * @param clazz The name of <code>clazz</code> will be used as the
169      * name of the category to retrieve.  See {@link
170      * #getLogger(String)} for more detailed information.
171      * @return the logger (<code>Category</code>).
172      */
173     public static Category getLogger(Class clazz) {
174   return getLogger(clazz.getName());
175     } 
176 }