/*******************************************************************************
 * Copyright (c) 2012, dhf.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those
 * of the authors and should not be interpreted as representing official policies, 
 * either expressed or implied, of the FreeBSD Project.
 ******************************************************************************/
package easyuse.rpc.util;

import easyuse.rpc.Logger;
import easyuse.rpc.LoggerFactory;
import easyuse.rpc.logging.JDKLoggerFactory;

/**
 * @author dhf
 */
public class LoggerHolder {
    private static LoggerFactory FACTORY = autodetect();

    private static LoggerFactory autodetect() {
        String packageName = "easyuse.rpc.logging";
        String[] simplifiedClassNames = {
            "SLF4JLoggerFactory", "JCLLoggerFactory", "Log4j12LoggerFactory"
        };

        LoggerFactory factory = null;
        for (String simplifiedClassName: simplifiedClassNames) {
            String className = packageName + "." + simplifiedClassName;
            try {
                factory = (LoggerFactory) Class.forName(className)
                        .newInstance();
                Logger logger = factory.getLogger(className);
                logger.info("autodetect logger type: {}", new Object[] {
                    logger.getClass()
                });
                return factory;
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        factory = new JDKLoggerFactory();
        Logger logger = factory.getLogger(JDKLoggerFactory.class);
        logger.info("fallback to logger type: {}", new Object[] {
            logger.getClass()
        });
        return factory;
    }

    public static Logger getLogger(Class<?> clazz) {
        return getFactory().getLogger(clazz);
    }

    public static Logger getLogger(String name) {
        return getFactory().getLogger(name);
    }

    public synchronized static void setFactory(LoggerFactory factory) {
        if (null == factory) {
            throw new NullPointerException("factory");
        }
        FACTORY = factory;
    }

    public synchronized static LoggerFactory getFactory() {
        return FACTORY;
    }
}

@@ -101,19 +101,16 @@
 12, dhf.
-%0A *
  All rig
@@ -122,23 +122,16 @@
 eserved.
-%0A * %0A *
  Redistr
@@ -148,16 +148,19 @@
 d use in
+%0A *
  source 
@@ -192,19 +192,16 @@
  without
-%0A *
  modific
@@ -229,16 +229,19 @@
 provided
+%0A *
  that th
@@ -272,24 +272,16 @@
 re met: 
-%0A * %0A * 
 1. Redis
@@ -310,16 +310,19 @@
 ode must
+%0A *
  retain 
@@ -353,22 +353,16 @@
 ce, this
-%0A *   
  list of
@@ -382,32 +382,35 @@
 nd the following
+%0A *
  disclaimer. %0A *
@@ -406,20 +406,16 @@
 laimer. 
-%0A * 
 2. Redis
@@ -456,32 +456,35 @@
 roduce the above
+%0A *
  copyright notic
@@ -485,22 +485,16 @@
  notice,
-%0A *   
  this li
@@ -545,16 +545,19 @@
 r in the
+%0A *
  documen
@@ -566,14 +566,8 @@
 tion
-%0A *   
  and
@@ -622,20 +622,15 @@
 on. 
-%0A * %0A * 
 THIS
+%0A *
  SOF
@@ -938,15 +938,15 @@
 ABLE
+%0A *
  FOR
-%0A *
  ANY
@@ -1016,16 +1016,19 @@
 TIAL
+%0A *
  DAMAGES
 %0A * 
@@ -1023,19 +1023,16 @@
  DAMAGES
-%0A *
  (INCLUD
@@ -1090,16 +1090,19 @@
 GOODS OR
+%0A *
  SERVICE
@@ -1103,19 +1103,16 @@
 ERVICES;
-%0A *
  LOSS OF
@@ -1168,16 +1168,19 @@
  HOWEVER
+%0A *
  CAUSED 
@@ -1182,19 +1182,16 @@
 USED AND
-%0A *
  ON ANY 
@@ -1253,16 +1253,19 @@
 ITY,
+%0A *
  OR TORT
 %0A * 
@@ -1260,19 +1260,16 @@
  OR TORT
-%0A *
  (INCLUD
@@ -1334,16 +1334,19 @@
  USE
+%0A *
  OF THIS
 %0A * 
@@ -1337,27 +1337,24 @@
 E%0A * OF THIS
-%0A *
  SOFTWARE, E
@@ -1406,19 +1406,15 @@
 AGE.
-%0A * %0A *
  The
+%0A *
  vie
@@ -1558,16 +1558,19 @@
 official
+%0A *
  policie
@@ -1572,20 +1572,16 @@
 licies, 
-%0A * 
 either e
@@ -2744,58 +2744,8 @@
 e) %7B
-%0A                e.printStackTrace();%0A            
 %7D%0A  

stop printing stack trace when autodect LoggerFactory in loggerHolder