DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG· RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT <http://issues.apache.org/bugzilla/show_bug.cgi?id=34185>. ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND· INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=34185 Summary: Requirement: Combine JCL and UGLI Product: Log4j Version: 1.3alpha Platform: All OS/Version: All Status: NEW Keywords: PatchAvailable Severity: normal Priority: P5 Component: Other AssignedTo: log4j-dev@logging.apache.org ReportedBy: [EMAIL PROTECTED] CC: commons-dev@jakarta.apache.org Due to beginning and ongoing discussions on both logging mailing lists I thougt it is good to provide some example how near a possible solution is. The code below does not solve any of the problems in JCL regarding ClassLoading and dynamic lookup. The idea is to ship an possible integration of Jakarta Commons Logging (JCL) and the new Log4j Logging Wrapper UGLI. I can offer to make this part "production ready" (Solid order of methods, JavaDoc, packaging...) if current project leaders give a "go". This is just demo code, it is placed in ASF Bugzilla to make it easy to find. Below: 1) modified interface ULogger 2) modified SimpleLogger 3) modified NOPLogger 4) modified JDK14Logger Missing 5) modified org.apache.log4j.Logger (just 4 methods, but the code below shows how easy integration is...) package org.apache.ugli; /** * * The main user inteface to logging. It is expected that logging takes places * through concerete implemetations of the ULogger interface. * * * @author Ceki Gülcü */ public interface ULogger extends org.apache.commons.logging.Log { public void trace(Object parameterizedMsg, Object param1); public void trace(Object parameterizedMsg, Object param1, Object param2); /** * Log a parameterized message object at the DEBUG level. * * <p> * This form is useful in avoiding the superflous object creation problem * when invoking this method while it is disabled. * </p> * * @param parameterizedMsg - * the parameterized message object * @param param1 - * the parameter */ public void debug(Object parameterizedMsg, Object param1); /** * Log a parameterized message object at the DEBUG level. * * <p> * This form is useful in avoiding the superflous object creation problem * when invoking this method while it is disabled. * </p> * * @param parameterizedMsg - * the parameterized message object * @param param1 - * the first parameter * @param param2 - * the second parameter */ public void debug(Object parameterizedMsg, Object param1, Object param2); public void info(Object parameterizedMsg, Object param1); public void info(Object parameterizedMsg, Object param1, Object param2); public void warn(Object parameterizedMsg, Object param1); public void warn(Object parameterizedMsg, Object param1, Object param2); public void error(Object parameterizedMsg, Object param1); public void error(Object parameterizedMsg, Object param1, Object param2); public void fatal(Object parameterizedMsg, Object param1); public void fatal(Object parameterizedMsg, Object param1, Object param2); } //End of class ULogger package org.apache.ugli.impl; import org.apache.ugli.ULogger; /** * A no operation (NOP) implementation of [EMAIL PROTECTED] ULogger}. * * @author Ceki Gülcü */ public class NOPLogger implements ULogger { /** * The unique instance of NOPLogger. */ public final static NOPLogger NOP_LOGGER = new NOPLogger(); /** * There is no point in people creating multiple instances of NullLogger. * Hence, the private access modifier. */ private NOPLogger() { } /* * Always returns false. * * @see org.apache.ugli.Logger#isDebugEnabled() */ public boolean isDebugEnabled() { return false; } /* * A NOP implementation. * * @see org.apache.ugli.Logger#debug(java.lang.Object) */ public void debug(Object msg) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.Logger#debug(java.lang.Object, java.lang.Object, * java.lang.Object) */ public void debug(Object parameterizedMsg, Object param1, Object param2) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.Logger#debug(java.lang.Object, java.lang.Throwable) */ public void debug(Object msg, Throwable t) { // NOP } /* * Always returns false. * * @see org.apache.ugli.Logger#isInfoEnabled() */ public boolean isInfoEnabled() { // NOP return false; } /* * A NOP implementation. * * @see org.apache.ugli.Logger#info(java.lang.Object) */ public void info(Object msg) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.Logger#info(java.lang.Object, java.lang.Object, * java.lang.Object) */ public void info(String parameterizedMsg, Object param1, Object param2) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.Logger#info(java.lang.Object, java.lang.Throwable) */ public void info(Object msg, Throwable t) { // NOP } /* * Always returns false. * * @see org.apache.ugli.Logger#isWarnEnabled() */ public boolean isWarnEnabled() { return false; } /* * A NOP implementation. * * @see org.apache.ugli.Logger#warn(java.lang.Object) */ public void warn(Object msg) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.Logger#warn(java.lang.Object, java.lang.Object, * java.lang.Object) */ public void warn(String parameterizedMsg, Object param1, Object param2) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.Logger#warn(java.lang.Object, java.lang.Throwable) */ public void warn(Object msg, Throwable t) { // NOP } /* * Always returns false. * * @see org.apache.ugli.Logger#isErrorEnabled() */ public boolean isErrorEnabled() { return false; } /* * A NOP implementation. * * @see org.apache.ugli.Logger#error(java.lang.Object) */ public void error(Object msg) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.Logger#error(java.lang.Object, java.lang.Object, * java.lang.Object) */ public void error(String parameterizedMsg, Object param1, Object param2) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.Logger#error(java.lang.Object, java.lang.Throwable) */ public void error(Object msg, Throwable t) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.ULogger#debug(java.lang.String, java.lang.Object) */ public void debug(Object parameterizedMsg, Object param1) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.ULogger#error(java.lang.String, java.lang.Object) */ public void error(Object parameterizedMsg, Object param1) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.ULogger#fatal(java.lang.String, java.lang.Object, * java.lang.Object) */ public void fatal(Object parameterizedMsg, Object param1, Object param2) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.ULogger#fatal(java.lang.String, java.lang.Object) */ public void fatal(Object parameterizedMsg, Object param1) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.ULogger#info(java.lang.String, java.lang.Object) */ public void info(Object parameterizedMsg, Object param1) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.ULogger#trace(java.lang.String, java.lang.Object, * java.lang.Object) */ public void trace(Object parameterizedMsg, Object param1, Object param2) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.ULogger#trace(java.lang.String, java.lang.Object) */ public void trace(Object parameterizedMsg, Object param1) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.ULogger#warn(java.lang.String, java.lang.Object) */ public void warn(Object parameterizedMsg, Object param1) { // NOP } /* * A NOP implementation. * * @see org.apache.commons.logging.Log#fatal(java.lang.Object, * java.lang.Throwable) */ public void fatal(Object message, Throwable t) { // NOP } /* * A NOP implementation. * * @see org.apache.commons.logging.Log#fatal(java.lang.Object) */ public void fatal(Object message) { // NOP } /* * Always returns false. * * @see org.apache.commons.logging.Log#isFatalEnabled() */ public boolean isFatalEnabled() { return false; } /* * Always returns false. * * @see org.apache.commons.logging.Log#isTraceEnabled() */ public boolean isTraceEnabled() { return false; } /* * A NOP implementation. * * @see org.apache.commons.logging.Log#trace(java.lang.Object, * java.lang.Throwable) */ public void trace(Object message, Throwable t) { // NOP } /* * A NOP implementation. * * @see org.apache.commons.logging.Log#trace(java.lang.Object) */ public void trace(Object message) { // NOP } /** * A NOP implementation. * * @see org.apache.ugli.ULogger#error(java.lang.Object, java.lang.Object, * java.lang.Object) */ public void error(Object parameterizedMsg, Object param1, Object param2) { // NOP } /** * A NOP implementation. * * @see org.apache.ugli.ULogger#info(java.lang.Object, java.lang.Object, * java.lang.Object) */ public void info(Object parameterizedMsg, Object param1, Object param2) { // NOP } /** * A NOP implementation. * * @see org.apache.ugli.ULogger#warn(java.lang.Object, java.lang.Object, * java.lang.Object) */ public void warn(Object parameterizedMsg, Object param1, Object param2) { // NOP } } //End of class NOPLogger import org.apache.ugli.ULogger; /** * A simple implementation that logs messages of level INFO or higher on the * console (<code>System.out<code>). * <p> * The output includes the relative time in milliseconds, thread name, the level, * logger name, and the message followed by the line separator for the host. * In log4j terms it amounts to the "%r [%t] %level %logger - %m%n" pattern. * <pre> * * * 176 [main] INFO examples.Sort - Populating an array of 2 elements in reverse order. * 225 [main] INFO examples.SortAlgo - Entered the sort method. * 304 [main] INFO SortAlgo.DUMP - Dump of interger array: * 317 [main] INFO SortAlgo.DUMP - Element [0] = 0 * 331 [main] INFO SortAlgo.DUMP - Element [1] = 1 * 343 [main] INFO examples.Sort - The next log statement should be an error message. * 346 [main] ERROR SortAlgo.DUMP - Tried to dump an uninitialized array. * at org.log4j.examples.SortAlgo.dump(SortAlgo.java:58) * at org.log4j.examples.Sort.main(Sort.java:64) * 467 [main] INFO examples.Sort - Exiting main method. * * * * </pre> * * @author Ceki Gülcü */ public class SimpleLogger implements ULogger { String loggerName; /** * Mark the time when this class gets loaded into memory. */ static private long startTime = System.currentTimeMillis(); public static final String LINE_SEPARATOR = System .getProperty("line.separator"); static private String INFO_STR = "INFO"; static private String WARN_STR = "WARN"; static private String ERROR_STR = "ERROR"; /** * Package access allows only [EMAIL PROTECTED] SimpleLoggerFA} to instantiate * SimpleLogger instances. */ SimpleLogger(String name) { this.loggerName = name; } /** * This is our internal implementation for logging regular * (non-parameterized) log messages. * * @param level * @param message * @param t */ private void log(String level, String message, Throwable t) { StringBuffer buf = new StringBuffer(); long millis = System.currentTimeMillis(); buf.append(millis - startTime); buf.append(" ["); buf.append(Thread.currentThread().getName()); buf.append("] "); buf.append(level); buf.append(" "); buf.append(loggerName); buf.append(" - "); buf.append(message); buf.append(LINE_SEPARATOR); System.out.print(buf.toString()); if (t != null) { t.printStackTrace(System.out); } System.out.flush(); } private String formatObject(Object parameterizedMsg, Object param1, Object param2) { String formatedString = null; if (parameterizedMsg instanceof String) { try { String msgStr = (String) parameterizedMsg; msgStr = MessageFormatter.format(msgStr, param1, param2); } catch (RuntimeException e) { formatedString = improveFormatObject(parameterizedMsg, param1, param2); } } else { // To be failsafe, we handle the case where 'messagePattern' is // not a valid String. Unless the user makes a mistake, this should // not happen. formatedString = improveFormatObject(parameterizedMsg, param1, param2); } return formatedString; } private String improveFormatObject(Object parameterizedMsg, Object param1, Object param2) { StringBuffer myBuf = new StringBuffer(String.valueOf(parameterizedMsg)); myBuf.append("["); myBuf.append(param1); myBuf.append("]"); if (param2 != null) { myBuf.append("["); myBuf.append(param2); myBuf.append("]"); } return myBuf.toString(); } /** * For parameterized messages, first substitute parameters and then log. * * @param level * @param parameterizedMsg * @param param1 * @param param2 */ private void parameterizedLog(String level, Object parameterizedMsg, Object param1, Object param2) { String msgString = formatObject(parameterizedMsg, param1, param2); log(level, msgString, null); } /* * A NOP implementation. * * @see org.apache.commons.logging.Log#debug(java.lang.Object, * java.lang.Throwable) */ public void debug(Object message, Throwable t) { // NOP } /* * A NOP implementation. * * @see org.apache.commons.logging.Log#debug(java.lang.Object) */ public void debug(Object message) { // NOP } /* * (non-Javadoc) * * @see org.apache.commons.logging.Log#error(java.lang.Object, * java.lang.Throwable) */ public void error(Object message, Throwable t) { log(ERROR_STR, String.valueOf(message), t); } /* * (non-Javadoc) * * @see org.apache.commons.logging.Log#error(java.lang.Object) */ public void error(Object message) { log(ERROR_STR, String.valueOf(message), null); } /* * (non-Javadoc) * * @see org.apache.commons.logging.Log#fatal(java.lang.Object, * java.lang.Throwable) */ public void fatal(Object message, Throwable t) { log(ERROR_STR, String.valueOf(message), t); } /* * (non-Javadoc) * * @see org.apache.commons.logging.Log#fatal(java.lang.Object) */ public void fatal(Object message) { log(ERROR_STR, String.valueOf(message), null); } /* * @see org.apache.commons.logging.Log#info(java.lang.Object, * java.lang.Throwable) */ public void info(Object message, Throwable t) { log(INFO_STR, String.valueOf(message), t); } /* * @see org.apache.commons.logging.Log#info(java.lang.Object) */ public void info(Object message) { log(INFO_STR, String.valueOf(message), null); } /* * Always returns false. * * @see org.apache.commons.logging.Log#isDebugEnabled() */ public boolean isDebugEnabled() { return false; } /* * Always returns true. * * @see org.apache.commons.logging.Log#isErrorEnabled() */ public boolean isErrorEnabled() { return true; } /* * Always returns true. * * @see org.apache.commons.logging.Log#isFatalEnabled() */ public boolean isFatalEnabled() { return true; } /* * Always returns true. * * @see org.apache.commons.logging.Log#isInfoEnabled() */ public boolean isInfoEnabled() { return true; } /* * Always returns false. * * @see org.apache.commons.logging.Log#isTraceEnabled() */ public boolean isTraceEnabled() { return false; } /* * Always returns true. * * @see org.apache.commons.logging.Log#isWarnEnabled() */ public boolean isWarnEnabled() { return true; } /* * A NOP implementation. * * @see org.apache.commons.logging.Log#trace(java.lang.Object, * java.lang.Throwable) */ public void trace(Object message, Throwable t) { // NOP } /* * A NOP implementation. * * @see org.apache.commons.logging.Log#trace(java.lang.Object) */ public void trace(Object message) { // NOP } /* * @see org.apache.commons.logging.Log#warn(java.lang.Object, * java.lang.Throwable) */ public void warn(Object message, Throwable t) { log(ERROR_STR, String.valueOf(message), t); } /* * @see org.apache.commons.logging.Log#warn(java.lang.Object) */ public void warn(Object message) { log(WARN_STR, String.valueOf(message), null); } /* * @see org.apache.ugli.ULogger#debug(java.lang.String, java.lang.Object, * java.lang.Object) */ public void debug(Object parameterizedMsg, Object param1, Object param2) { // NOP } /* * @see org.apache.ugli.ULogger#debug(java.lang.String, java.lang.Object) */ public void debug(Object parameterizedMsg, Object param1) { // NOP } /* * @see org.apache.ugli.ULogger#error(java.lang.String, java.lang.Object, * java.lang.Object) */ public void error(Object parameterizedMsg, Object param1, Object param2) { parameterizedLog(ERROR_STR, parameterizedMsg, param1, param2); } /* * @see org.apache.ugli.ULogger#error(java.lang.String, java.lang.Object) */ public void error(Object parameterizedMsg, Object param1) { parameterizedLog(ERROR_STR, parameterizedMsg, param1, null); } /* * @see org.apache.ugli.ULogger#fatal(java.lang.String, java.lang.Object, * java.lang.Object) */ public void fatal(Object parameterizedMsg, Object param1, Object param2) { parameterizedLog(ERROR_STR, parameterizedMsg, param1, param2); } /* * @see org.apache.ugli.ULogger#fatal(java.lang.String, java.lang.Object) */ public void fatal(Object parameterizedMsg, Object param1) { parameterizedLog(ERROR_STR, parameterizedMsg, param1, null); } /* * @see org.apache.ugli.ULogger#info(java.lang.String, java.lang.Object, * java.lang.Object) */ public void info(Object parameterizedMsg, Object param1, Object param2) { parameterizedLog(INFO_STR, parameterizedMsg, param1, param2); } /* * @see org.apache.ugli.ULogger#info(java.lang.String, java.lang.Object) */ public void info(Object parameterizedMsg, Object param1) { parameterizedLog(INFO_STR, parameterizedMsg, param1, null); } /* * A NOP implementation. * * @see org.apache.ugli.ULogger#trace(java.lang.String, java.lang.Object, * java.lang.Object) */ public void trace(Object parameterizedMsg, Object param1, Object param2) { // NOP } /* * A NOP implementation. * * @see org.apache.ugli.ULogger#trace(java.lang.String, java.lang.Object) */ public void trace(Object parameterizedMsg, Object param1) { // NOP } /* * @see org.apache.ugli.ULogger#warn(java.lang.String, java.lang.Object, * java.lang.Object) */ public void warn(Object parameterizedMsg, Object param1, Object param2) { parameterizedLog(WARN_STR, parameterizedMsg, param1, param2); } /* * @see org.apache.ugli.ULogger#warn(java.lang.String, java.lang.Object) */ public void warn(Object parameterizedMsg, Object param1) { parameterizedLog(WARN_STR, parameterizedMsg, param1, null); } } //End of class SimpleLogger import org.apache.ugli.ULogger; import java.util.logging.Level; import java.util.logging.Logger; /** * A wrapper over * * @{link java.utill.Logger} which conforms to the [EMAIL PROTECTED] ULogger} interface. * * @author Ceki Gülcü */ public class JDK14Logger implements ULogger { final Logger logger; // WARN: JDK14Logger constructor should have only package access so that // only JDK14LoggerFA be able to create one. JDK14Logger(Logger logger) { this.logger = logger; } private String formatObject(Object parameterizedMsg, Object param1, Object param2) { String formatedString = null; if (parameterizedMsg instanceof String) { try { String msgStr = (String) parameterizedMsg; msgStr = MessageFormatter.format(msgStr, param1, param2); } catch (RuntimeException e) { formatedString = improveFormatObject(parameterizedMsg, param1, param2); } } else { // To be failsafe, we handle the case where 'messagePattern' is // not a valid String. Unless the user makes a mistake, this should // not happen. formatedString = improveFormatObject(parameterizedMsg, param1, param2); } return formatedString; } private String improveFormatObject(Object parameterizedMsg, Object param1, Object param2) { StringBuffer myBuf = new StringBuffer(String.valueOf(parameterizedMsg)); myBuf.append("["); myBuf.append(param1); myBuf.append("]"); if (param2 != null) { myBuf.append("["); myBuf.append(param2); myBuf.append("]"); } return myBuf.toString(); } public void trace(Object parameterizedMsg, Object param1) { if (logger.isLoggable(Level.FINEST)) { String msgStr = formatObject(parameterizedMsg, param1, null); logger.finest(msgStr); } } public void trace(Object parameterizedMsg, Object param1, Object param2) { String msgStr = formatObject(parameterizedMsg, param1, param2); logger.finest(msgStr); } public void debug(Object parameterizedMsg, Object param1) { if (logger.isLoggable(Level.FINE)) { String msgStr = formatObject(parameterizedMsg, param1, null); logger.fine(msgStr); } } public void debug(Object parameterizedMsg, Object param1, Object param2) { String msgStr = formatObject(parameterizedMsg, param1, param2); logger.fine(msgStr); } public void info(Object parameterizedMsg, Object param1) { if (logger.isLoggable(Level.INFO)) { String msgStr = (String) parameterizedMsg; msgStr = MessageFormatter.format(msgStr, param1); logger.info(msgStr); } } public void info(Object parameterizedMsg, Object param1, Object param2) { String msgStr = formatObject(parameterizedMsg, param1, param2); logger.info(msgStr); } public void warn(Object parameterizedMsg, Object param1) { if (logger.isLoggable(Level.WARNING)) { String msgStr = formatObject(parameterizedMsg, param1, null); logger.warning(msgStr); } } public void warn(Object parameterizedMsg, Object param1, Object param2) { String msgStr = formatObject(parameterizedMsg, param1, param2); logger.warning(msgStr); } public void error(Object parameterizedMsg, Object param1) { if (logger.isLoggable(Level.SEVERE)) { String msgStr = formatObject(parameterizedMsg, param1, null); logger.severe(msgStr); } } public void error(Object parameterizedMsg, Object param1, Object param2) { String msgStr = formatObject(parameterizedMsg, param1, param2); logger.severe(msgStr); } public void fatal(Object parameterizedMsg, Object param1) { if (logger.isLoggable(Level.SEVERE)) { String msgStr = formatObject(parameterizedMsg, param1, null); logger.severe(msgStr); } } public void fatal(Object parameterizedMsg, Object param1, Object param2) { String msgStr = formatObject(parameterizedMsg, param1, param2); logger.severe(msgStr); } public boolean isDebugEnabled() { return logger.isLoggable(Level.FINE); } public boolean isErrorEnabled() { return logger.isLoggable(Level.SEVERE); } public boolean isFatalEnabled() { return logger.isLoggable(Level.SEVERE); } public boolean isInfoEnabled() { return logger.isLoggable(Level.INFO); } public boolean isTraceEnabled() { return logger.isLoggable(Level.FINEST); } public boolean isWarnEnabled() { return logger.isLoggable(Level.WARNING); } public void trace(Object message) { logger.finest(String.valueOf(message)); } public void trace(Object message, Throwable t) { logger.log(Level.FINEST, String.valueOf(message), t); } public void debug(Object message) { logger.fine(String.valueOf(message)); } public void debug(Object message, Throwable t) { logger.log(Level.FINE, String.valueOf(message), t); } public void info(Object message) { logger.info(String.valueOf(message)); } public void info(Object message, Throwable t) { logger.log(Level.INFO, String.valueOf(message), t); } public void warn(Object message) { logger.warning(String.valueOf(message)); } public void warn(Object message, Throwable t) { logger.log(Level.WARNING, String.valueOf(message), t); } public void error(Object message) { logger.severe(String.valueOf(message)); } public void error(Object message, Throwable t) { logger.log(Level.SEVERE, String.valueOf(message), t); } public void fatal(Object message) { logger.severe(String.valueOf(message)); } public void fatal(Object message, Throwable t) { logger.log(Level.SEVERE, String.valueOf(message), t); } } //End of class JDK14Logger -- Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are on the CC list for the bug, or are watching someone who is. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]