1 16 package org.apache.cocoon.ojb.components; 17 18 import org.apache.avalon.framework.activity.Initializable; 19 import org.apache.avalon.framework.component.Component; 20 import org.apache.avalon.framework.logger.AbstractLogEnabled; 21 import org.apache.avalon.framework.thread.ThreadSafe; 22 23 import org.apache.ojb.broker.util.configuration.Configuration; 24 import org.apache.ojb.broker.util.configuration.ConfigurationException; 25 import org.apache.ojb.broker.util.logging.Logger; 26 import org.apache.ojb.broker.util.logging.LoggingConfiguration; 27 28 50 public class LoggerImpl extends AbstractLogEnabled 51 implements Component, ThreadSafe, Initializable, 52 Logger { 53 54 57 private static org.apache.avalon.framework.logger.Logger LOGGER; 58 59 private final String name; 60 private transient int level; 61 private transient org.apache.avalon.framework.logger.Logger logger; 62 63 66 public LoggerImpl() { 67 this.name = null; 68 } 69 70 73 public LoggerImpl(String name) { 74 this.name = name.startsWith("org.apache.ojb.")? name.substring(15): name; 75 } 76 77 80 public LoggerImpl(String name, LoggingConfiguration config) { 81 this(name); 82 } 83 84 87 public void initialize() { 88 LOGGER = getLogger(); 89 } 90 91 protected int getLevel() { 92 if (logger == null) { 93 this.logger = LOGGER.getChildLogger(this.name); 94 if (this.logger.isDebugEnabled()) this.level = DEBUG; 95 else if (this.logger.isInfoEnabled()) this.level = INFO; 96 else if (this.logger.isWarnEnabled()) this.level = WARN; 97 else if (this.logger.isErrorEnabled()) this.level = ERROR; 98 else this.level = FATAL; 99 } 100 return level; 101 } 102 103 public String getName() { 104 return name; 105 } 106 107 public void debug(Object message) { 108 if (DEBUG >= getLevel()) { 109 logger.debug(toString(message)); 110 } 111 } 112 113 public void debug(Object message, Throwable t) { 114 if (DEBUG >= getLevel()) { 115 logger.debug(toString(message), t); 116 } 117 } 118 119 public void safeDebug(String message, Object obj) { 120 if (DEBUG >= getLevel()) { 121 logger.debug(message + " : " + toString(obj)); 122 } 123 } 124 125 public void safeDebug(String message, Object obj, Throwable t) { 126 if (DEBUG >= getLevel()) { 127 logger.debug(message + " : " + toString(obj), t); 128 } 129 } 130 131 public void info(Object message) { 132 if (INFO >= getLevel()) { 133 logger.info(toString(message)); 134 } 135 } 136 137 public void info(Object message, Throwable t) { 138 if (INFO >= getLevel()) { 139 logger.info(toString(message), t); 140 } 141 } 142 143 public void safeInfo(String message, Object obj) { 144 if (INFO >= getLevel()) { 145 logger.info(message + " : " + toString(obj)); 146 } 147 } 148 149 public void safeInfo(String message, Object obj, Throwable t) { 150 if (INFO >= getLevel()) { 151 logger.info(message + " : " + toString(obj), t); 152 } 153 } 154 155 public void warn(Object message) { 156 if (WARN >= getLevel()) { 157 logger.warn(toString(message)); 158 } 159 } 160 161 public void warn(Object message, Throwable t) { 162 if (WARN >= getLevel()) { 163 logger.warn(toString(message), t); 164 } 165 } 166 167 public void safeWarn(String message, Object obj) { 168 if (WARN >= getLevel()) { 169 logger.warn(message + " : " + toString(obj)); 170 } 171 } 172 173 public void safeWarn(String message, Object obj, Throwable t) { 174 if (WARN >= getLevel()) { 175 logger.warn(message + " : " + toString(obj), t); 176 } 177 } 178 179 public void error(Object message) { 180 if (ERROR >= getLevel()) { 181 logger.error(toString(message)); 182 } 183 } 184 185 public void error(Object message, Throwable t) { 186 if (ERROR >= getLevel()) { 187 logger.error(toString(message), t); 188 } 189 } 190 191 public void safeError(String message, Object obj) { 192 if (ERROR >= getLevel()) { 193 logger.error(message + " : " + toString(obj)); 194 } 195 } 196 197 public void safeError(String message, Object obj, Throwable t) { 198 if (ERROR >= getLevel()) { 199 logger.error(message + " : " + toString(obj), t); 200 } 201 } 202 203 public void fatal(Object message) { 204 if (FATAL >= getLevel()) { 205 logger.fatalError(toString(message)); 206 } 207 } 208 209 public void fatal(Object message, Throwable t) { 210 if (FATAL >= getLevel()) { 211 logger.fatalError(toString(message), t); 212 } 213 } 214 215 public void safeFatal(String message, Object obj) { 216 if (FATAL >= getLevel()) { 217 logger.fatalError(message + " : " + toString(obj)); 218 } 219 } 220 221 public void safeFatal(String message, Object obj, Throwable t) { 222 if (FATAL >= getLevel()) { 223 logger.fatalError(message + " : " + toString(obj), t); 224 } 225 } 226 227 public boolean isDebugEnabled() { 228 return isEnabledFor(DEBUG); 229 } 230 231 public boolean isEnabledFor(int priority) { 232 return priority >= getLevel(); 233 } 234 235 238 public void configure(Configuration config) throws ConfigurationException { 239 } 240 241 private String toString(Object obj) { 242 if (obj != null) { 243 try { 244 return obj.toString(); 245 } catch (Throwable throwable) { 246 return "BAD toString() impl for " + obj.getClass().getName(); 247 } 248 } 249 250 return "null"; 251 } 252 } 253 | Popular Tags |