Home » xwork-2.1.5 » com.opensymphony » xwork2 » util » logging » [javadoc | source]

    1   /*
    2    * Copyright (c) 2002-2006 by OpenSymphony
    3    * All rights reserved.
    4    */
    5   package com.opensymphony.xwork2.util.logging;
    6   
    7   import com.opensymphony.xwork2.util.logging.jdk.JdkLoggerFactory;
    8   
    9   import java.util.concurrent.locks.ReadWriteLock;
   10   import java.util.concurrent.locks.ReentrantReadWriteLock;
   11   
   12   /**
   13    * Creates loggers.  Static accessor will lazily try to decide on the best factory if none specified.
   14    */
   15   public abstract class LoggerFactory {
   16       
   17       private static final ReadWriteLock lock = new ReentrantReadWriteLock();
   18       private static LoggerFactory factory;
   19       
   20       public static void setLoggerFactory(LoggerFactory factory) {
   21           lock.writeLock().lock();
   22           try {
   23               LoggerFactory.factory = factory;
   24           } finally {
   25               lock.writeLock().unlock();
   26           }
   27               
   28       }
   29       
   30       public static Logger getLogger(Class<?> cls) {
   31           return getLoggerFactory().getLoggerImpl(cls);
   32       }
   33       
   34       public static Logger getLogger(String name) {
   35           return getLoggerFactory().getLoggerImpl(name);
   36       }
   37       
   38       protected static LoggerFactory getLoggerFactory() {
   39           lock.readLock().lock();
   40           try {
   41               if (factory != null) {
   42                   return factory;
   43               }
   44           } finally {
   45               lock.readLock().unlock();
   46           }
   47           lock.writeLock().lock();
   48           try {
   49               if (factory == null) {
   50                   try {
   51                       Class.forName("org.apache.commons.logging.LogFactory");
   52                       factory = new com.opensymphony.xwork2.util.logging.commons.CommonsLoggerFactory();
   53                   } catch (ClassNotFoundException ex) {
   54                       // commons logging not found, falling back to jdk logging
   55                       factory = new JdkLoggerFactory();
   56                   }
   57               }
   58               return factory;
   59           }
   60           finally {
   61               lock.writeLock().unlock();
   62           }
   63       }
   64       
   65       protected abstract Logger getLoggerImpl(Class<?> cls);
   66       
   67       protected abstract Logger getLoggerImpl(String name); 
   68   
   69   }

Home » xwork-2.1.5 » com.opensymphony » xwork2 » util » logging » [javadoc | source]