View Javadoc

1   package com.aragost.javahg.log;
2   
3   import com.aragost.javahg.internals.ServiceLoader;
4   import com.google.common.base.Strings;
5   
6   /**
7    * Factory class to retrieve a {@link Logger} instance. The LoggerFactory 
8    * searches a implementation with the use of {@link ServiceLoader}, if no
9    * implementation could be found it uses the default implementation 
10   * {@link JULLoggerFactory}.
11   */
12  public abstract class LoggerFactory {
13  
14      private static LoggerFactory instance;
15    
16      static {
17          String loggerFactoryName = System.getProperty( LoggerFactory.class.getName() );
18          try 
19          {
20              if ( ! Strings.isNullOrEmpty(loggerFactoryName) ){
21                  instance = (LoggerFactory) Class.forName(loggerFactoryName).newInstance();
22              } else {
23                  instance = ServiceLoader.loadService(LoggerFactory.class);
24              }
25          } 
26          catch (Exception ex)
27          {
28            // do nothing
29          }
30          if ( instance == null ){
31              instance = new JULLoggerFactory();
32          }
33      }
34    
35      /**
36       * Return a logger for the class
37       * 
38       * @param cls
39       * @return
40       */
41      public static Logger getLogger(Class<?> cls) {
42          return instance.getLoggerInstance(cls);
43      }
44      
45      protected abstract Logger getLoggerInstance(Class<?> cls);
46  
47  }