View Javadoc

1   /*
2    * Copyright  2004-2005 Stefan Reuter
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package net.sf.asterisk.util;
18  
19  import net.sf.asterisk.util.impl.JavaLoggingLog;
20  import net.sf.asterisk.util.impl.Log4JLogger;
21  import net.sf.asterisk.util.impl.NullLog;
22  
23  /***
24   * Facade to hide details of the underlying logging system.
25   * 
26   * @author srt
27   * @version $Id: LogFactory.java,v 1.2 2005/04/20 18:22:13 srt Exp $
28   */
29  public final class LogFactory
30  {
31      /***
32       * Indicates if log4j is available on the classpath or not. If the
33       * check has not yet performed this is <code>null</code>.
34       */
35      private static Boolean log4jLoggingAvailable = null;
36  
37      /***
38       * Indicates if java.util.logging is available on the classpath or not. If the
39       * check has not yet performed this is <code>null</code>.
40       */
41      private static Boolean javaLoggingAvailable = null;
42      
43      /***
44       * Returns an instance of Log suitable for logging from the given class.
45       * 
46       * @param clazz the class to create the logger for.
47       * @return the created logger.
48       */
49      public static Log getLog(Class clazz)
50      {
51          if (log4jLoggingAvailable == null)
52          {
53              try
54              {
55                  Class.forName("org.apache.log4j.Logger");
56                  log4jLoggingAvailable = Boolean.TRUE;
57              }
58              catch (Exception e)
59              {
60                  log4jLoggingAvailable = Boolean.FALSE;
61              }
62          }
63          if (log4jLoggingAvailable.booleanValue())
64          {
65              return new Log4JLogger(clazz);
66          }
67          else
68          {
69              if (javaLoggingAvailable == null)
70              {
71                  try
72                  {
73                      Class.forName("java.util.logging.Logger");
74                      javaLoggingAvailable = Boolean.TRUE;
75                  }
76                  catch (Exception e)
77                  {
78                      javaLoggingAvailable = Boolean.FALSE;
79                  }
80              }
81              if (javaLoggingAvailable.booleanValue())
82              {
83                  return new JavaLoggingLog(clazz);
84              }
85              else
86              {
87                  return new NullLog();
88              }
89          }
90      }
91  }