View Javadoc
1 /* ==================================================================== 2 * License: 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * 3. The end-user documentation included with the redistribution, 17 * if any, must include the following acknowledgment: 18 * "This product includes software developed by 19 * Robert Half International (http://www.rhi.com/)." 20 * Alternately, this acknowledgment may appear in the software itself, 21 * if and wherever such third-party acknowledgments normally appear. 22 * 23 * 4. The names "Parc", "RHI", and "Robert Half International" must 24 * not be used to endorse or promote products derived from this 25 * software without prior written permission. For written 26 * permission, please contact pete.mckinstry@rhi.com. 27 * 28 * 5. Products derived from this software may not be called "PARC", 29 * nor may "PARC" appear in their name, without prior written 30 * permission of Robert Half International. 31 * 32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 43 * SUCH DAMAGE. 44 * ==================================================================== 45 * 46 */ 47 package com.rhi.architecture.logging; 48 49 import com.rhi.architecture.config.ConfigurationException; 50 import com.rhi.architecture.resource.InitializationException; 51 import com.rhi.architecture.resource.ResourceContext; 52 53 import java.util.Properties; 54 55 import org.apache.log4j.Logger; 56 import org.apache.log4j.Level; 57 58 /*** 59 * Log4JLogger 60 * 61 * @author Pete McKinstry 62 * @copyright 2002, Robert Half Int'l., Inc. All rights reserved. 63 * 64 * @since 1.2 65 */ 66 public final class Log4JLogger extends com.rhi.architecture.logging.Logger { 67 68 // logging constants - here for backward compatibility, but moved 69 // to Log4JFactory where they belong in new model. 70 /*** 71 * file appender to use 72 */ 73 public static final String FILE_APPENDER = Log4JFactory.FILE_APPENDER; 74 /*** 75 * log4j config filename. 76 */ 77 public static final String LOG4J_PROPERTYFILE = 78 Log4JFactory.LOG4J_PROPERTYFILE; 79 /*** 80 * path to write logfile to 81 */ 82 public static final String LOGFILE_PATH = Log4JFactory.LOGFILE_PATH; 83 84 /*** 85 * space for msg templates. 86 */ 87 public static final String SPACER = " "; 88 89 private Logger logger; 90 91 // make logging details work. 92 private static final String FQCN = Log4JLogger.class.getName(); 93 94 /*** 95 * Default constructor. - for backwkard compatibility. 96 */ 97 public Log4JLogger() { 98 logger = null; 99 } 100 101 /*** 102 * constructor used for new model, where multiple Loggers are 103 * supported. 104 * @param categoryName 105 */ 106 public Log4JLogger(String categoryName) { 107 logger = Logger.getLogger(categoryName); 108 } 109 110 /*** 111 * Perform whatever initialization is required of the resource. 112 * This method should only be called once per application instance. 113 * @param p 114 * @throws InitializationException 115 * @since 1.2 116 */ 117 public void init(Properties p) throws InitializationException { 118 /* 119 * Apply a pseudo copy constructor to initialize this Log4JLogger 120 * usually the old Logger model. New users should use the LogFactory 121 * instead.. 122 */ 123 try { 124 LogFactory factory = getFactory(p); 125 Log4JLogger defaultInstance = (Log4JLogger) factory.getDefaultLogger(); 126 127 // pseudo copy constructor to reset this objects state w/ the 128 // configured Logger state. 129 this.logger = defaultInstance.logger; 130 } 131 catch (ConfigurationException e) { 132 e.printStackTrace(); 133 throw new InitializationException(e.toString()); 134 } 135 } 136 137 /*** 138 * Load factory - used only for backward compatibility to support users 139 * who don't know about the new LogFactory interface & are initializing 140 * the Logger class directly. 141 * 142 * Register the LogFactory so that it can be cleaned up by the 143 * architecture upon application shutdown. 144 * 145 * @param p 146 * @return LogFactory 147 * @throws ConfigurationException 148 * @throws InitializationException 149 * @since 1.5 150 */ 151 public LogFactory getFactory(Properties p) 152 throws ConfigurationException, InitializationException { 153 LogFactory factory = 154 LogFactory.getFactory( 155 "com.rhi.architecture.logging.Log4JFactory"); 156 factory.init(p); 157 // register w/ ResourceContext for later. 158 { 159 ResourceContext ctx = new ResourceContext(); 160 ctx.register(LogFactory.KEY, factory); 161 } 162 163 return factory; 164 } 165 166 /*** 167 * Perform whatever cleanup is required of the underlying object.. 168 * 169 * @since 1.2 170 */ 171 public void close() { 172 // no op 173 } 174 175 /*** 176 * Log a message w/ a fatal priority. Fatal messages are 177 * usually not recoverable & will precede an application 178 * shutdown. Actual support for this priority is log 179 * implementation specific, but should be available in 180 * most packages. (e.g. Log4J, JDK1.4) 181 * 182 * @param message - log message 183 * 184 * @since 1.0 185 */ 186 public void fatal(String message) { 187 logger.log(FQCN, Level.FATAL, message, null); 188 } 189 /*** 190 * So that InterfaceLogger works correctly (details such as 191 * filename, line number, etc... so up correctly.) 192 * @param className 193 * @param message 194 * 195 * @since 1.01 196 */ 197 public void fatal(String className, String message) { 198 logger.log(className, Level.FATAL, message, null); 199 } 200 /*** 201 * Log a message w/ a fatal priority. Fatal messages are 202 * usually not recoverable & will precede an application 203 * shutdown. Actual support for this priority is log 204 * implementation specific, but should be available in 205 * most packages. (e.g. Log4J, JDK1.4) 206 * 207 * @param message - log message 208 * @param t - the exception causing the fatal error. 209 * 210 * @since 1.0 211 */ 212 public void fatal(String message, Throwable t) { 213 logger.log(FQCN, Level.FATAL, message, t); 214 } 215 /*** 216 * So that InterfaceLogger works correctly (details such as 217 * filename, line number, etc... so up correctly.) 218 * @param className 219 * @param message - log message 220 * @param t - the exception causing the fatal error. 221 * 222 * @since 1.01 223 */ 224 public void fatal(String className, String message, Throwable t) { 225 logger.log(className, Level.FATAL, message, t); 226 } 227 228 /*** 229 * Log the provided error message. 230 * 231 * Note: The Priority of messages logged w/ the error() method is 232 * <link>org.apache.log4j.Priority.ERROR</link>. 233 * 234 * @param message - error message 235 * 236 * @since 1.2 237 */ 238 public void error(String message) { 239 logger.log(FQCN, Level.ERROR, message, null); 240 } 241 /*** 242 * So that InterfaceLogger works correctly (details such as 243 * filename, line number, etc... so up correctly.) 244 * @param className 245 * @param message 246 * 247 * @since 1.01 248 */ 249 public void error(String className, String message) { 250 logger.log(className, Level.ERROR, message, null); 251 } 252 253 /*** 254 * Log the provided error message. 255 * 256 * Note: The Priority of messages logged w/ the error() method is 257 * <link>org.apache.log4j.Priority.ERROR</link>. 258 * 259 * @param message - error message 260 * @param t - the exception causing the error. 261 * 262 * @since 1.2 263 */ 264 public void error(String message, Throwable t) { 265 logger.log(FQCN, Level.ERROR, message, t); 266 } 267 /*** 268 * So that InterfaceLogger works correctly (details such as 269 * filename, line number, etc... so up correctly.) 270 * @param className 271 * @param message - log message 272 * @param t - the exception causing the fatal error. 273 * 274 * @since 1.01 275 */ 276 public void error(String className, String message, Throwable t) { 277 logger.log(className, Level.ERROR, message, t); 278 } 279 280 /*** 281 * Log the provided warning message. This method allows for developers 282 * to easily log free form messages about the program status. It is 283 * designed to be easy to use & simple to encourage debug messages 284 * in the code. 285 * 286 * Note: The Priority of messages logged w/ the info() method is 287 * <link>org.apache.log4j.Priority.WARNING</link>. These message can 288 * be disabled in the log4j configuration file when deploying to 289 * a production environment. (or for performance testing.) * 290 * @param message - log message 291 * 292 * @since 1.01 293 */ 294 public final void warning(String message) { 295 logger.log(FQCN, Level.WARN, message, null); 296 } 297 298 /*** 299 * So that InterfaceLogger works correctly (details such as 300 * filename, line number, etc... so up correctly.) 301 * @param className 302 * @param message - log message 303 * 304 * @since 1.01 305 */ 306 public final void warning(String className, String message) { 307 logger.log(className, Level.WARN, message, null); 308 } 309 310 /*** 311 * Check whether this category is enabled for the WARN Level. 312 * 313 * @return boolean 314 * 315 * @since 1.0 316 */ 317 public boolean isWarningEnabled() { 318 return logger.isEnabledFor(Level.WARN); 319 } 320 321 /*** 322 * log the provided informational message. This method allows for 323 * developers to easily log free form messages about the program 324 * status. It is designed to be easy to use & simple to encourage 325 * debug messages in the code. 326 * 327 * Note: The Priority of messages logged w/ the info() method is 328 * <link>org.apache.log4j.Priority.INFO</link>. These message can 329 * be disabled in the log4j configuration file when deploying to 330 * a production environment. (or for performance testing.) 331 * @param message - log message 332 * 333 * @since 1.01 334 */ 335 public final void info(String message) { 336 logger.log(FQCN, Level.INFO, message, null); 337 } 338 339 /*** 340 * So that InterfaceLogger works correctly (details such as 341 * filename, line number, etc... so up correctly.) 342 * @param className 343 * @param message - log message 344 * 345 * @since 1.01 346 */ 347 public final void info(String className, String message) { 348 logger.log(className, Level.INFO, message, null); 349 } 350 351 /*** 352 * Check whether this category is enabled for the INFO Level. 353 * 354 * @return boolean 355 * 356 * @since 1.0 357 */ 358 public boolean isInfoEnabled() { 359 return logger.isInfoEnabled(); 360 } 361 362 /*** 363 * Log the provided debug message. This method allows for developers 364 * to easily log free form messages about the program status. It is 365 * designed to be easy to use & simple to encourage debug messages 366 * in the code. 367 * 368 * Note: The Priority of messages logged w/ the info() method is 369 * <link>org.apache.log4j.Priority.INFO</link>. These message can 370 * be disabled in the log4j configuration file when deploying to 371 * a production environment. (or for performance testing.) 372 * @param message - log message 373 * 374 * @since 1.01 375 */ 376 public final void debug(String message) { 377 logger.log(FQCN, Level.DEBUG, message, null); 378 } 379 380 /*** 381 * So that InterfaceLogger works correctly (details such as 382 * filename, line number, etc... so up correctly.) 383 * @param className 384 * @param message - log message 385 * 386 * @since 1.01 387 */ 388 public final void debug(String className, String message) { 389 logger.log(className, Level.DEBUG, message, null); 390 } 391 392 /*** 393 * Check whether this category is enabled for the DEBUG Level. 394 * 395 * @return boolean 396 * 397 * @since 1.0 398 */ 399 public boolean isDebugEnabled() { 400 return logger.isDebugEnabled(); 401 } 402 403 /*** 404 * @see com.rhi.architecture.logging.Logger#trace(String) 405 * @param message - log message 406 */ 407 public void trace(String message) { 408 logger.log(FQCN, TraceLevel.TRACE, message, null); 409 } 410 411 /*** 412 * @see com.rhi.architecture.logging.Logger#trace(String) 413 * @param className 414 * @param message - log message 415 */ 416 public void trace(String className, String message) { 417 logger.log(className, TraceLevel.TRACE, message, null); 418 } 419 420 /*** 421 * @see com.rhi.architecture.logging.Logger#isTraceEnabled() 422 * @return boolean 423 */ 424 public boolean isTraceEnabled() { 425 return logger.isEnabledFor(TraceLevel.TRACE); 426 } 427 428 }

This page was automatically generated by Maven