View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2007 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.log;
22  
23  import java.util.WeakHashMap;
24  
25  /**
26   * Log4j wrapper
27   * 
28   * @author vlads
29   *
30   */
31  public class Logger {
32  	
33  	/** The fully qualified name of the Log class. */
34      private static final String FQCN = Logger.class.getName();
35      
36      private static int log4jAvalable = 0;
37      
38      private static WeakHashMap instances = new WeakHashMap();
39      
40      protected Logger() {
41      }
42      
43      private static int detectLog4j() {
44      	try {
45  			Class.forName("org.apache.log4j.Logger");
46  			return 1;
47  		} catch (ClassNotFoundException e) {
48  			return -1;
49  		}
50      }
51      
52      /**
53       * We are in java 1.4 lets save some typing.
54       */
55      public static Logger getLogger() {
56          String useName = null;
57          StackTraceElement[] ste = new Throwable().getStackTrace();
58          for (int i = 0; i < ste.length; i++ ) {
59              if (ste[i].getClassName().equals(FQCN)) {
60                  i ++;
61                  useName = ste[i].getClassName();
62                  break;
63              }
64          }
65          if (useName == null) {
66              throw new Error("Can't find call origin");
67          }
68          return getLogger(useName);    
69      }
70      
71      /**
72       * Return the native Logger instance we are using.
73       */
74      public static Logger getLogger(String name) {
75          return createLogger(name);
76      }
77      
78      private static Logger createLogger(String name) {
79      	if (log4jAvalable == 0) {
80      		log4jAvalable = detectLog4j();
81      	} 
82      	if (log4jAvalable > 0) {
83      		return createLoggerWrapper(name);
84      	} else {
85      		return new Logger();
86      	}
87      }
88      
89      private static Logger createLoggerWrapper(String name) {
90      	org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(name);
91          if (instances.containsKey(logger)) {
92              return (Logger)instances.get(logger);
93          } else {
94              Logger log = new LoggerLog4j(logger);
95              instances.put(logger, log);
96              return log;
97          }
98      }
99      
100     public static Logger getLogger(Class clazz) {
101         return getLogger(clazz.getName());
102     }
103     
104     public static Logger getLog(Class clazz) {
105         return getLogger(clazz.getName());
106     }
107     
108     private static class LoggerLog4j extends Logger {
109 
110 		private org.apache.log4j.Logger logger;
111 
112 		protected LoggerLog4j(org.apache.log4j.Logger log4jLogger) {
113 			this.logger = log4jLogger;
114 		}
115 
116 		public void error(Object message) {
117 			this.logger.log(FQCN, org.apache.log4j.Level.ERROR, message, null);
118 		}
119 
120 		public void error(Object message, Throwable t) {
121 			this.logger.log(FQCN, org.apache.log4j.Level.ERROR, message, t);
122 		}
123 
124 		public void warn(Object message) {
125 			this.logger.log(FQCN, org.apache.log4j.Level.WARN, message, null);
126 		}
127 
128 		public void info(Object message) {
129 			this.logger.log(FQCN, org.apache.log4j.Level.INFO, message, null);
130 		}
131 
132 		public boolean isDebugEnabled() {
133 			return this.logger.isDebugEnabled();
134 		}
135 
136 		public void debug(Object message) {
137 			this.logger.log(FQCN, org.apache.log4j.Level.DEBUG, message, null);
138 		}
139 	}
140 
141 	public void error(Object message) {
142 		System.err.println(message);
143 	}
144 
145 	public void error(Object message, Throwable t) {
146 		System.err.println(message);
147 		if (t != null) {
148 			t.printStackTrace(System.err);
149 		}
150 	}
151     
152     public void warn(Object message) {
153     }
154     
155     public void info(Object message) {
156     }
157     
158     public boolean isDebugEnabled() {
159    		return false;
160     }
161 
162     public void debug(Object message) {
163     }
164 }