1 18 package org.apache.beehive.netui.util.logging; 19 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 23 import org.apache.log4j.Category; 24 import org.apache.log4j.Level; 25 import org.apache.commons.logging.Log; 26 27 public class Logger 28 implements Log 29 { 30 private static final String STRUTS_APPENDER = "commons-logging"; 31 32 static 33 { 34 Category root = Category.getRoot(); 37 38 if (root.getAppender(STRUTS_APPENDER) != null) 39 root.removeAppender(STRUTS_APPENDER); 40 } 41 42 private org.apache.log4j.Logger _logInstance; 43 44 50 public static Logger getInstance(Class clazz) 51 { 52 return new Logger(clazz); 53 } 54 55 public Logger(Class clazz) 56 { 57 this(clazz.getName()); 58 } 59 60 public Logger(String className) 61 { 62 _logInstance = org.apache.log4j.Logger.getLogger(className); 63 } 64 65 public boolean isDebugEnabled() 66 { 67 return _logInstance.isEnabledFor(Level.DEBUG); 68 } 69 70 public boolean isErrorEnabled() 71 { 72 return _logInstance.isEnabledFor(Level.ERROR); 73 } 74 75 public boolean isFatalEnabled() 76 { 77 return _logInstance.isEnabledFor(Level.FATAL); 78 } 79 80 public boolean isInfoEnabled() 81 { 82 return _logInstance.isEnabledFor(Level.INFO); 83 } 84 85 public boolean isTraceEnabled() 86 { 87 return _logInstance.isEnabledFor(Level.DEBUG); 88 } 89 90 public boolean isWarnEnabled() 91 { 92 return _logInstance.isEnabledFor(Level.WARN); 93 } 94 95 public void debug(Object message) 96 { 97 if (_logInstance.isEnabledFor(Level.DEBUG)) 98 _logInstance.debug(message); 99 } 100 101 public void debug(Object message, Throwable t) 102 { 103 if (_logInstance.isEnabledFor(Level.DEBUG)) 104 _logInstance.debug(format(message, t)); 105 } 106 107 public void trace(Object message) 108 { 109 if (_logInstance.isEnabledFor(Level.DEBUG)) 110 _logInstance.debug(message); 111 } 112 113 public void trace(Object message, Throwable t) 114 { 115 if (_logInstance.isEnabledFor(Level.DEBUG)) 116 _logInstance.debug(format(message, t)); 117 } 118 119 public void info(Object message) 120 { 121 if (_logInstance.isEnabledFor(Level.INFO)) 122 _logInstance.info(message); 123 } 124 125 public void info(Object message, Throwable t) 126 { 127 if (_logInstance.isEnabledFor(Level.INFO)) 128 _logInstance.info(format(message, t)); 129 } 130 131 public void warn(Object message) 132 { 133 if (_logInstance.isEnabledFor(Level.WARN)) 134 _logInstance.warn(message); 135 } 136 137 public void warn(Object message, Throwable t) 138 { 139 if (_logInstance.isEnabledFor(Level.WARN)) 140 _logInstance.warn(format(message, t)); 141 } 142 143 public void error(Object message) 144 { 145 if (_logInstance.isEnabledFor(Level.ERROR)) 146 _logInstance.error(message); 147 } 148 149 public void error(Object message, Throwable t) 150 { 151 if (_logInstance.isEnabledFor(Level.ERROR)) 152 _logInstance.error(format(message, t)); 153 } 154 155 public void fatal(Object message) 156 { 157 if (_logInstance.isEnabledFor(Level.FATAL)) 158 _logInstance.fatal(message); 159 } 160 161 public void fatal(Object message, Throwable t) 162 { 163 if (_logInstance.isEnabledFor(Level.FATAL)) 164 _logInstance.fatal(format(message, t)); 165 } 166 167 private String format(Object m, Throwable t) 168 { 169 if(t == null) 170 return m.toString(); 171 172 StringWriter sw = new StringWriter (); 173 t.printStackTrace(new PrintWriter (sw)); 174 175 return m + "\n\n" + "Throwable: " + t.toString() + "\nStack Trace:\n" + sw.toString(); 176 } 177 } 178 | Popular Tags |