1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2009 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.util.socketstatistics.genericlogger;
18
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.util.Map;
22 import java.util.WeakHashMap;
23
24 /**
25 * LogFactory that returns a GenericLogger.
26 *
27 * This Factory searches in all loaded classes of the calling thread
28 * for a logging facility and returns the corresponding GenericLogger.
29 *
30 * The search order is as follows:
31 * - SLF4J (org.slf4j.LoggerFactory)
32 * - Apache Commons Logging (org.apache.commons.logging)
33 * - Log4J (org.apache.log4j)
34 *
35 * If none of these logging facilities is found, it uses the JDK
36 * logging (java.util.logging).
37 *
38 * @svnLink $Revision: 4093 $;$Date: 2010-01-15 14:26:34 +0100 (Fr, 15. Jan 2010) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/socketstatistics/src/main/java/ch/elca/el4j/util/socketstatistics/genericlogger/GenericLogFactory.java $
39 *
40 * @author Jonas Hauenstein (JHN)
41 */
42 public class GenericLogFactory {
43
44 /**
45 * Map for storage of already initiated loggers.
46 * Map key is the calling threads classloader
47 */
48 private static Map<ClassLoader, GenericLogger> s_loggerMap = new WeakHashMap<ClassLoader, GenericLogger>();
49
50 /**
51 * Return a GenericLogger named corresponding to the class passed as parameter.
52 *
53 * @param clazz The returned logger will be named after clazz
54 * @return The GenericLogger
55 */
56 public static GenericLogger getLogger(Class clazz) {
57 return getLogger(clazz.getName());
58 }
59
60 /**
61 * Return a GenericLogger named corresponding to the name parameter.
62 *
63 * @param name The name of the logger
64 * @return The GenericLogger
65 */
66 public static GenericLogger getLogger(String name) {
67 Object ret;
68 GenericLogger logger = null;
69 //check if there is already a logger for this thread
70 if (s_loggerMap.containsKey(Thread.currentThread().getContextClassLoader())) {
71 return s_loggerMap.get(Thread.currentThread().getContextClassLoader());
72 }
73
74 if ((ret = fetchlogger("org.slf4j.LoggerFactory", "getLogger", name)) != null) {
75 logger = new SLF4JLogger(ret);
76 s_loggerMap.put(Thread.currentThread().getContextClassLoader(), logger);
77 } else if ((ret = fetchlogger("org.apache.commons.logging.LogFactory", "getLog", name)) != null) {
78 logger = new CommonsLoggingLogger(ret);
79 s_loggerMap.put(Thread.currentThread().getContextClassLoader(), logger);
80 //Checkstyle: UseLogger off
81 } else if ((ret = fetchlogger("org.apache.log4j.Logger", "getLoggger", name)) != null) {
82 logger = new Log4JLogger(ret);
83 s_loggerMap.put(Thread.currentThread().getContextClassLoader(), logger);
84 //Checkstyle: UseLogger on
85 } else {
86 logger = new JDKLogger(name);
87 s_loggerMap.put(Thread.currentThread().getContextClassLoader(), logger);
88 }
89
90 return logger;
91 }
92
93 /**
94 * Search for a logging factory and a corresponding logger in the calling threads ClassLoader.
95 *
96 * @param factoryclass The full name of the logger factory
97 * @param factorymethod The name of the method to create a now logger from the factory
98 * @param logname The desired name of the created logger
99 * @return an Object representing the logger class or null if no matching loggerfactory is found
100 */
101 private static Object fetchlogger(String factoryclass, String factorymethod, String logname) {
102
103 ClassLoader ccl = Thread.currentThread().getContextClassLoader();
104 try {
105 Class factory = Class.forName(factoryclass, true, ccl);
106 Method fm = factory.getMethod(factorymethod, String.class);
107 Object o = fm.invoke(factory, logname);
108 return o;
109 } catch (ClassNotFoundException e) {
110 return null;
111 } catch (SecurityException e) {
112 return null;
113 } catch (NoSuchMethodException e) {
114 return null;
115 } catch (IllegalArgumentException e) {
116 return null;
117 } catch (IllegalAccessException e) {
118 return null;
119 } catch (InvocationTargetException e) {
120 return null;
121 }
122
123 }
124
125
126
127 }