View Javadoc

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.HashMap;
22  import java.util.Map;
23  
24  /**
25   * Base class for different facades to the actual logger interface.
26   *
27   * @svnLink $Revision: 3929 $;$Date: 2009-09-25 16:38:41 +0200 (Fr, 25. Sep 2009) $;$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/GenericLogger.java $
28   *
29   * @author Jonas Hauenstein (JHN)
30   */
31  public abstract class GenericLogger {
32  	
33  	/**
34  	 * Reference to the actually used logger for reflective calls.
35  	 */
36  	protected Object m_orgLogger;
37  	
38  	/**
39  	 * List of allowed log levels.
40  	 */
41  	protected Map<String, Boolean> m_logLevels = new HashMap<String, Boolean>();
42  	
43  	/**
44  	 * Constructor.
45  	 *
46  	 * @param originallogger Reference to the actually used logger for reflective calls
47  	 */
48  	public GenericLogger(Object originallogger) {
49  		this.m_orgLogger = originallogger;
50  	}
51  
52  	/**
53  	 * Check in underlying logging facility with the passed method
54  	 * name if the logger is enabled for a particular level - 
55  	 * or in other words, if the method in fact returns true.
56  	 *
57  	 * @param methodname Name of the method to call on logging facility
58  	 * @return True if method returned true, false otherwise
59  	 */
60  	protected boolean checkForLevel(String methodname) {
61  		if (m_orgLogger != null) {
62  			try {
63  				Method m = m_orgLogger.getClass().getMethod(methodname);
64  				Object r = m.invoke(m_orgLogger);
65  				if (r instanceof Boolean) {
66  					return (Boolean) r;
67  				} else {
68  					return false;
69  				}
70  			} catch (SecurityException e) {
71  				return false;
72  			} catch (NoSuchMethodException e) {
73  				return false;
74  			} catch (IllegalArgumentException e) {
75  				return false;
76  			} catch (IllegalAccessException e) {
77  				return false;
78  			} catch (InvocationTargetException e) {
79  				return false;
80  			}
81  		}
82  		return false;
83  	}
84  	
85  	/**
86  	 * Is the underlying logger instance enabled for the parameter level?
87  	 *
88  	 * @param level Log level to check
89  	 * @return True if this Logger is enabled for the parameter level, false otherwise
90  	 */
91  	public boolean isLogEnabled(String level) {
92  		if (m_logLevels.containsKey(level)) {
93  			return m_logLevels.get(level);
94  		}
95  		return false;
96  	}
97  	
98  	/**
99  	 * Log a message under the specified log level.
100 	 * The level can be:
101 	 *  - debug
102 	 *  - error
103 	 *  - info
104 	 *  - trace
105 	 *  - warn
106 	 *
107 	 * @param level The log level of the message
108 	 * @param msg The log message
109 	 */
110 	public abstract void log(String level, String msg);
111 	
112 }