1
0

Sort members

This commit is contained in:
Gary Gregory
2023-10-18 17:04:00 -04:00
parent 58ec3139c2
commit 0b4d31d127
40 changed files with 3859 additions and 3859 deletions

View File

@@ -27,6 +27,11 @@ public class LogConfigurationException extends RuntimeException {
/** Serializable version identifier. */
private static final long serialVersionUID = 8486587136871052495L;
/**
* The underlying cause of this exception.
*/
protected Throwable cause;
/**
* Construct a new exception with {@code null} as its detail message.
*/
@@ -42,16 +47,6 @@ public class LogConfigurationException extends RuntimeException {
super(message);
}
/**
* Construct a new exception with the specified cause and a derived
* detail message.
*
* @param cause The underlying cause
*/
public LogConfigurationException(final Throwable cause) {
this(cause == null ? null : cause.toString(), cause);
}
/**
* Construct a new exception with the specified detail message and cause.
*
@@ -64,9 +59,14 @@ public class LogConfigurationException extends RuntimeException {
}
/**
* The underlying cause of this exception.
* Construct a new exception with the specified cause and a derived
* detail message.
*
* @param cause The underlying cause
*/
protected Throwable cause;
public LogConfigurationException(final Throwable cause) {
this(cause == null ? null : cause.toString(), cause);
}
/**
* Return the underlying cause of this exception (if any).

File diff suppressed because it is too large Load Diff

View File

@@ -134,55 +134,18 @@ public class LogSource {
// ------------------------------------------------------------ Constructor
/** Don't allow others to create instances. */
private LogSource() {
/**
* Get a {@code Log} instance by class.
*
* @param clazz a Class.
* @return a {@code Log} instance.
*/
static public Log getInstance(final Class clazz) {
return getInstance(clazz.getName());
}
// ---------------------------------------------------------- Class Methods
/**
* Set the log implementation/log implementation factory by the name of the class. The given class must implement {@link Log}, and provide a constructor
* that takes a single {@link String} argument (containing the name of the log).
*
* @param className class name.
* @throws LinkageError if there is missing dependency.
* @throws NoSuchMethodException if a matching method is not found.
* @throws SecurityException If a security manager, <i>s</i>, is present and the caller's class loader is not the same as or an ancestor of the class
* loader for the current class and invocation of {@link SecurityManager#checkPackageAccess s.checkPackageAccess()} denies
* access to the package of this class.
* @throws ClassNotFoundException if the class cannot be located
*/
static public void setLogImplementation(final String className)
throws LinkageError, NoSuchMethodException, SecurityException, ClassNotFoundException {
try {
final Class logclass = Class.forName(className);
final Class[] argtypes = new Class[1];
argtypes[0] = "".getClass();
logImplctor = logclass.getConstructor(argtypes);
} catch (final Throwable t) {
logImplctor = null;
}
}
/**
* Set the log implementation/log implementation factory by class. The given class must implement {@link Log}, and provide a constructor that takes a single
* {@link String} argument (containing the name of the log).
*
* @param logclass class.
* @throws LinkageError if there is missing dependency.
* @throws ExceptionInInitializerError unexpected exception has occurred in a static initializer.
* @throws NoSuchMethodException if a matching method is not found.
* @throws SecurityException If a security manager, <i>s</i>, is present and the caller's class loader is not the same as or an ancestor of the
* class loader for the current class and invocation of {@link SecurityManager#checkPackageAccess
* s.checkPackageAccess()} denies access to the package of this class.
*/
static public void setLogImplementation(final Class logclass)
throws LinkageError, ExceptionInInitializerError, NoSuchMethodException, SecurityException {
final Class[] argtypes = new Class[1];
argtypes[0] = "".getClass();
logImplctor = logclass.getConstructor(argtypes);
}
/**
* Get a {@code Log} instance by class name.
*
@@ -194,13 +157,14 @@ public class LogSource {
}
/**
* Get a {@code Log} instance by class.
* Returns a {@link String} array containing the names of
* all logs known to me.
*
* @param clazz a Class.
* @return a {@code Log} instance.
* @return a {@link String} array containing the names of
* all logs known to me.
*/
static public Log getInstance(final Class clazz) {
return getInstance(clazz.getName());
static public String[] getLogNames() {
return (String[]) logs.keySet().toArray(EMPTY_STRING_ARRAY);
}
/**
@@ -236,13 +200,49 @@ public class LogSource {
}
/**
* Returns a {@link String} array containing the names of
* all logs known to me.
* Set the log implementation/log implementation factory by class. The given class must implement {@link Log}, and provide a constructor that takes a single
* {@link String} argument (containing the name of the log).
*
* @return a {@link String} array containing the names of
* all logs known to me.
* @param logclass class.
* @throws LinkageError if there is missing dependency.
* @throws ExceptionInInitializerError unexpected exception has occurred in a static initializer.
* @throws NoSuchMethodException if a matching method is not found.
* @throws SecurityException If a security manager, <i>s</i>, is present and the caller's class loader is not the same as or an ancestor of the
* class loader for the current class and invocation of {@link SecurityManager#checkPackageAccess
* s.checkPackageAccess()} denies access to the package of this class.
*/
static public String[] getLogNames() {
return (String[]) logs.keySet().toArray(EMPTY_STRING_ARRAY);
static public void setLogImplementation(final Class logclass)
throws LinkageError, ExceptionInInitializerError, NoSuchMethodException, SecurityException {
final Class[] argtypes = new Class[1];
argtypes[0] = "".getClass();
logImplctor = logclass.getConstructor(argtypes);
}
/**
* Set the log implementation/log implementation factory by the name of the class. The given class must implement {@link Log}, and provide a constructor
* that takes a single {@link String} argument (containing the name of the log).
*
* @param className class name.
* @throws LinkageError if there is missing dependency.
* @throws NoSuchMethodException if a matching method is not found.
* @throws SecurityException If a security manager, <i>s</i>, is present and the caller's class loader is not the same as or an ancestor of the class
* loader for the current class and invocation of {@link SecurityManager#checkPackageAccess s.checkPackageAccess()} denies
* access to the package of this class.
* @throws ClassNotFoundException if the class cannot be located
*/
static public void setLogImplementation(final String className)
throws LinkageError, NoSuchMethodException, SecurityException, ClassNotFoundException {
try {
final Class logclass = Class.forName(className);
final Class[] argtypes = new Class[1];
argtypes[0] = "".getClass();
logImplctor = logclass.getConstructor(argtypes);
} catch (final Throwable t) {
logImplctor = null;
}
}
/** Don't allow others to create instances. */
private LogSource() {
}
}

View File

@@ -57,6 +57,16 @@ public class AvalonLogger implements Log {
/** Ancestral Avalon logger. */
private static volatile Logger defaultLogger;
/**
* Sets the ancestral Avalon logger from which the delegating loggers will descend.
*
* @param logger the default avalon logger,
* in case there is no logger instance supplied in constructor
*/
public static void setDefaultLogger(final Logger logger) {
defaultLogger = logger;
}
/** Avalon logger used to perform log. */
private final transient Logger logger;
@@ -82,22 +92,16 @@ public class AvalonLogger implements Log {
}
/**
* Gets the Avalon logger implementation used to perform logging.
* Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
*
* @return avalon logger implementation
* @param message to log.
* @see org.apache.commons.logging.Log#debug(Object)
*/
public Logger getLogger() {
return logger;
}
/**
* Sets the ancestral Avalon logger from which the delegating loggers will descend.
*
* @param logger the default avalon logger,
* in case there is no logger instance supplied in constructor
*/
public static void setDefaultLogger(final Logger logger) {
defaultLogger = logger;
@Override
public void debug(final Object message) {
if (getLogger().isDebugEnabled()) {
getLogger().debug(String.valueOf(message));
}
}
/**
@@ -115,15 +119,15 @@ public class AvalonLogger implements Log {
}
/**
* Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
* Logs a message with {@code org.apache.avalon.framework.logger.Logger.error}.
*
* @param message to log.
* @see org.apache.commons.logging.Log#debug(Object)
* @param message to log
* @see org.apache.commons.logging.Log#error(Object)
*/
@Override
public void debug(final Object message) {
if (getLogger().isDebugEnabled()) {
getLogger().debug(String.valueOf(message));
public void error(final Object message) {
if (getLogger().isErrorEnabled()) {
getLogger().error(String.valueOf(message));
}
}
@@ -142,15 +146,15 @@ public class AvalonLogger implements Log {
}
/**
* Logs a message with {@code org.apache.avalon.framework.logger.Logger.error}.
* Logs a message with {@code org.apache.avalon.framework.logger.Logger.fatalError}.
*
* @param message to log
* @see org.apache.commons.logging.Log#error(Object)
* @see org.apache.commons.logging.Log#fatal(Object)
*/
@Override
public void error(final Object message) {
if (getLogger().isErrorEnabled()) {
getLogger().error(String.valueOf(message));
public void fatal(final Object message) {
if (getLogger().isFatalErrorEnabled()) {
getLogger().fatalError(String.valueOf(message));
}
}
@@ -169,15 +173,24 @@ public class AvalonLogger implements Log {
}
/**
* Logs a message with {@code org.apache.avalon.framework.logger.Logger.fatalError}.
* Gets the Avalon logger implementation used to perform logging.
*
* @return avalon logger implementation
*/
public Logger getLogger() {
return logger;
}
/**
* Logs a message with {@code org.apache.avalon.framework.logger.Logger.info}.
*
* @param message to log
* @see org.apache.commons.logging.Log#fatal(Object)
* @see org.apache.commons.logging.Log#info(Object)
*/
@Override
public void fatal(final Object message) {
if (getLogger().isFatalErrorEnabled()) {
getLogger().fatalError(String.valueOf(message));
public void info(final Object message) {
if (getLogger().isInfoEnabled()) {
getLogger().info(String.valueOf(message));
}
}
@@ -195,19 +208,6 @@ public class AvalonLogger implements Log {
}
}
/**
* Logs a message with {@code org.apache.avalon.framework.logger.Logger.info}.
*
* @param message to log
* @see org.apache.commons.logging.Log#info(Object)
*/
@Override
public void info(final Object message) {
if (getLogger().isInfoEnabled()) {
getLogger().info(String.valueOf(message));
}
}
/**
* Is logging to {@code org.apache.avalon.framework.logger.Logger.debug} enabled?
* @see org.apache.commons.logging.Log#isDebugEnabled()
@@ -262,6 +262,19 @@ public class AvalonLogger implements Log {
return getLogger().isWarnEnabled();
}
/**
* Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
*
* @param message to log
* @see org.apache.commons.logging.Log#trace(Object)
*/
@Override
public void trace(final Object message) {
if (getLogger().isDebugEnabled()) {
getLogger().debug(String.valueOf(message));
}
}
/**
* Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
*
@@ -277,15 +290,15 @@ public class AvalonLogger implements Log {
}
/**
* Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}.
* Logs a message with {@code org.apache.avalon.framework.logger.Logger.warn}.
*
* @param message to log
* @see org.apache.commons.logging.Log#trace(Object)
* @see org.apache.commons.logging.Log#warn(Object)
*/
@Override
public void trace(final Object message) {
if (getLogger().isDebugEnabled()) {
getLogger().debug(String.valueOf(message));
public void warn(final Object message) {
if (getLogger().isWarnEnabled()) {
getLogger().warn(String.valueOf(message));
}
}
@@ -302,17 +315,4 @@ public class AvalonLogger implements Log {
getLogger().warn(String.valueOf(message), t);
}
}
/**
* Logs a message with {@code org.apache.avalon.framework.logger.Logger.warn}.
*
* @param message to log
* @see org.apache.commons.logging.Log#warn(Object)
*/
@Override
public void warn(final Object message) {
if (getLogger().isWarnEnabled()) {
getLogger().warn(String.valueOf(message));
}
}
}

View File

@@ -43,11 +43,19 @@ public class Jdk13LumberjackLogger implements Log, Serializable {
// ----------------------------------------------------- Instance Variables
/**
* This member variable simply ensures that any attempt to initialize
* this class in a pre-1.4 JVM will result in an ExceptionInInitializerError.
* It must not be private, as an optimising compiler could detect that it
* is not used and optimise it away.
*/
protected static final Level dummyLevel = Level.FINE;
/**
* The underlying Logger implementation we are using.
*/
protected transient Logger logger;
/**
* Name.
*/
@@ -58,18 +66,10 @@ public class Jdk13LumberjackLogger implements Log, Serializable {
/** Source method name. */
private String sourceMethodName = "unknown";
/** Class and method found flag. */
private boolean classAndMethodFound;
/**
* This member variable simply ensures that any attempt to initialize
* this class in a pre-1.4 JVM will result in an ExceptionInInitializerError.
* It must not be private, as an optimising compiler could detect that it
* is not used and optimise it away.
*/
protected static final Level dummyLevel = Level.FINE;
// ----------------------------------------------------------- Constructors
/**
@@ -84,55 +84,6 @@ public class Jdk13LumberjackLogger implements Log, Serializable {
// --------------------------------------------------------- Public Methods
private void log( final Level level, final String msg, final Throwable ex ) {
if ( getLogger().isLoggable(level) ) {
final LogRecord record = new LogRecord(level, msg);
if ( !classAndMethodFound ) {
getClassAndMethod();
}
record.setSourceClassName(sourceClassName);
record.setSourceMethodName(sourceMethodName);
if ( ex != null ) {
record.setThrown(ex);
}
getLogger().log(record);
}
}
/**
* Gets the class and method by looking at the stack trace for the
* first entry that is not this class.
*/
private void getClassAndMethod() {
try {
final Throwable throwable = new Throwable();
throwable.fillInStackTrace();
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter( stringWriter );
throwable.printStackTrace( printWriter );
final String traceString = stringWriter.getBuffer().toString();
final StringTokenizer tokenizer =
new StringTokenizer( traceString, "\n" );
tokenizer.nextToken();
String line = tokenizer.nextToken();
while (!line.contains(this.getClass().getName())) {
line = tokenizer.nextToken();
}
while (line.contains(this.getClass().getName())) {
line = tokenizer.nextToken();
}
final int start = line.indexOf( "at " ) + 3;
final int end = line.indexOf( '(' );
final String temp = line.substring( start, end );
final int lastPeriod = temp.lastIndexOf( '.' );
sourceClassName = temp.substring( 0, lastPeriod );
sourceMethodName = temp.substring( lastPeriod + 1 );
} catch ( final Exception ex ) {
// ignore - leave class and methodname unknown
}
classAndMethodFound = true;
}
/**
* Logs a message with {@code java.util.logging.Level.FINE}.
*
@@ -202,6 +153,40 @@ public class Jdk13LumberjackLogger implements Log, Serializable {
log(Level.SEVERE, String.valueOf(message), exception);
}
/**
* Gets the class and method by looking at the stack trace for the
* first entry that is not this class.
*/
private void getClassAndMethod() {
try {
final Throwable throwable = new Throwable();
throwable.fillInStackTrace();
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter( stringWriter );
throwable.printStackTrace( printWriter );
final String traceString = stringWriter.getBuffer().toString();
final StringTokenizer tokenizer =
new StringTokenizer( traceString, "\n" );
tokenizer.nextToken();
String line = tokenizer.nextToken();
while (!line.contains(this.getClass().getName())) {
line = tokenizer.nextToken();
}
while (line.contains(this.getClass().getName())) {
line = tokenizer.nextToken();
}
final int start = line.indexOf( "at " ) + 3;
final int end = line.indexOf( '(' );
final String temp = line.substring( start, end );
final int lastPeriod = temp.lastIndexOf( '.' );
sourceClassName = temp.substring( 0, lastPeriod );
sourceMethodName = temp.substring( lastPeriod + 1 );
} catch ( final Exception ex ) {
// ignore - leave class and methodname unknown
}
classAndMethodFound = true;
}
/**
* Return the native Logger instance we are using.
*
@@ -285,6 +270,21 @@ public class Jdk13LumberjackLogger implements Log, Serializable {
return getLogger().isLoggable(Level.WARNING);
}
private void log( final Level level, final String msg, final Throwable ex ) {
if ( getLogger().isLoggable(level) ) {
final LogRecord record = new LogRecord(level, msg);
if ( !classAndMethodFound ) {
getClassAndMethod();
}
record.setSourceClassName(sourceClassName);
record.setSourceMethodName(sourceMethodName);
if ( ex != null ) {
record.setThrown(ex);
}
getLogger().log(record);
}
}
/**
* Logs a message with {@code java.util.logging.Level.FINEST}.
*

View File

@@ -43,16 +43,6 @@ public class Jdk14Logger implements Log, Serializable {
// ----------------------------------------------------------- Constructors
/**
* Construct a named instance of this Logger.
*
* @param name Name of the logger to be constructed
*/
public Jdk14Logger(final String name) {
this.name = name;
logger = getLogger();
}
/**
* The underlying Logger implementation we are using.
*/
@@ -64,31 +54,13 @@ public class Jdk14Logger implements Log, Serializable {
protected String name;
/**
* Logs a message at the given level.
* @param level The level.
* @param msg The message.
* @param ex The exception.
* Construct a named instance of this Logger.
*
* @param name Name of the logger to be constructed
*/
protected void log(final Level level, final String msg, final Throwable ex) {
final Logger logger = getLogger();
if (logger.isLoggable(level)) {
// Hack (?) to get the stack trace.
final Throwable dummyException = new Throwable();
final StackTraceElement[] locations = dummyException.getStackTrace();
// LOGGING-132: use the provided logger name instead of the class name
final String cname = name;
String method = "unknown";
// Caller will be the third element
if (locations != null && locations.length > 2) {
final StackTraceElement caller = locations[2];
method = caller.getMethodName();
}
if (ex == null) {
logger.logp(level, cname, method, msg);
} else {
logger.logp(level, cname, method, msg, ex);
}
}
public Jdk14Logger(final String name) {
this.name = name;
logger = getLogger();
}
/**
@@ -243,6 +215,34 @@ public class Jdk14Logger implements Log, Serializable {
return getLogger().isLoggable(Level.WARNING);
}
/**
* Logs a message at the given level.
* @param level The level.
* @param msg The message.
* @param ex The exception.
*/
protected void log(final Level level, final String msg, final Throwable ex) {
final Logger logger = getLogger();
if (logger.isLoggable(level)) {
// Hack (?) to get the stack trace.
final Throwable dummyException = new Throwable();
final StackTraceElement[] locations = dummyException.getStackTrace();
// LOGGING-132: use the provided logger name instead of the class name
final String cname = name;
String method = "unknown";
// Caller will be the third element
if (locations != null && locations.length > 2) {
final StackTraceElement caller = locations[2];
method = caller.getMethodName();
}
if (ex == null) {
logger.logp(level, cname, method, msg);
} else {
logger.logp(level, cname, method, msg, ex);
}
}
}
/**
* Logs a message with {@code java.util.logging.Level.FINEST}.
*

View File

@@ -48,28 +48,8 @@ public class Log4JLogger implements Log, Serializable {
/** The fully qualified name of the Log4JLogger class. */
private static final String FQCN = Log4JLogger.class.getName();
/** Log to this logger */
private transient volatile Logger logger;
/** Logger name */
private final String name;
private static final Priority traceLevel;
// ------------------------------------------------------------
// Static Initializer.
//
// Note that this must come after the static variable declarations
// otherwise initializer expressions associated with those variables
// will override any settings done here.
//
// Verify that log4j is available, and that it is version 1.2.
// If an ExceptionInInitializerError is generated, then LogFactoryImpl
// will treat that as meaning that the appropriate underlying logging
// library is just not present - if discovery is in progress then
// discovery will continue.
// ------------------------------------------------------------
static {
if (!Priority.class.isAssignableFrom(Level.class)) {
// nope, this is log4j 1.3, so force an ExceptionInInitializerError
@@ -90,6 +70,26 @@ public class Log4JLogger implements Log, Serializable {
traceLevel = _traceLevel;
}
/** Log to this logger */
private transient volatile Logger logger;
// ------------------------------------------------------------
// Static Initializer.
//
// Note that this must come after the static variable declarations
// otherwise initializer expressions associated with those variables
// will override any settings done here.
//
// Verify that log4j is available, and that it is version 1.2.
// If an ExceptionInInitializerError is generated, then LogFactoryImpl
// will treat that as meaning that the appropriate underlying logging
// library is just not present - if discovery is in progress then
// discovery will continue.
// ------------------------------------------------------------
/** Logger name */
private final String name;
/**
* Constructs a new instance.
*/
@@ -97,16 +97,6 @@ public class Log4JLogger implements Log, Serializable {
name = null;
}
/**
* Base constructor.
*
* @param name name.
*/
public Log4JLogger(final String name) {
this.name = name;
this.logger = getLogger();
}
/**
* For use with a log4j factory.
*
@@ -122,30 +112,13 @@ public class Log4JLogger implements Log, Serializable {
}
/**
* Logs a message with {@code org.apache.log4j.Priority.TRACE}.
* When using a log4j version that does not support the {@code TRACE}
* level, the message will be logged at the {@code DEBUG} level.
* Base constructor.
*
* @param message to log
* @see org.apache.commons.logging.Log#trace(Object)
* @param name name.
*/
@Override
public void trace(final Object message) {
getLogger().log(FQCN, traceLevel, message, null);
}
/**
* Logs a message with {@code org.apache.log4j.Priority.TRACE}.
* When using a log4j version that does not support the {@code TRACE}
* level, the message will be logged at the {@code DEBUG} level.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#trace(Object, Throwable)
*/
@Override
public void trace(final Object message, final Throwable t) {
getLogger().log(FQCN, traceLevel, message, t);
public Log4JLogger(final String name) {
this.name = name;
this.logger = getLogger();
}
/**
@@ -171,52 +144,6 @@ public class Log4JLogger implements Log, Serializable {
getLogger().log(FQCN, Level.DEBUG, message, t);
}
/**
* Logs a message with {@code org.apache.log4j.Priority.INFO}.
*
* @param message to log
* @see org.apache.commons.logging.Log#info(Object)
*/
@Override
public void info(final Object message) {
getLogger().log(FQCN, Level.INFO, message, null);
}
/**
* Logs a message with {@code org.apache.log4j.Priority.INFO}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#info(Object, Throwable)
*/
@Override
public void info(final Object message, final Throwable t) {
getLogger().log(FQCN, Level.INFO, message, t);
}
/**
* Logs a message with {@code org.apache.log4j.Priority.WARN}.
*
* @param message to log
* @see org.apache.commons.logging.Log#warn(Object)
*/
@Override
public void warn(final Object message) {
getLogger().log(FQCN, Level.WARN, message, null);
}
/**
* Logs a message with {@code org.apache.log4j.Priority.WARN}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#warn(Object, Throwable)
*/
@Override
public void warn(final Object message, final Throwable t) {
getLogger().log(FQCN, Level.WARN, message, t);
}
/**
* Logs a message with {@code org.apache.log4j.Priority.ERROR}.
*
@@ -281,6 +208,29 @@ public class Log4JLogger implements Log, Serializable {
return result;
}
/**
* Logs a message with {@code org.apache.log4j.Priority.INFO}.
*
* @param message to log
* @see org.apache.commons.logging.Log#info(Object)
*/
@Override
public void info(final Object message) {
getLogger().log(FQCN, Level.INFO, message, null);
}
/**
* Logs a message with {@code org.apache.log4j.Priority.INFO}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#info(Object, Throwable)
*/
@Override
public void info(final Object message, final Throwable t) {
getLogger().log(FQCN, Level.INFO, message, t);
}
/**
* Tests whether the Log4j Logger used is enabled for {@code DEBUG} priority.
*/
@@ -330,4 +280,54 @@ public class Log4JLogger implements Log, Serializable {
public boolean isWarnEnabled() {
return getLogger().isEnabledFor(Level.WARN);
}
/**
* Logs a message with {@code org.apache.log4j.Priority.TRACE}.
* When using a log4j version that does not support the {@code TRACE}
* level, the message will be logged at the {@code DEBUG} level.
*
* @param message to log
* @see org.apache.commons.logging.Log#trace(Object)
*/
@Override
public void trace(final Object message) {
getLogger().log(FQCN, traceLevel, message, null);
}
/**
* Logs a message with {@code org.apache.log4j.Priority.TRACE}.
* When using a log4j version that does not support the {@code TRACE}
* level, the message will be logged at the {@code DEBUG} level.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#trace(Object, Throwable)
*/
@Override
public void trace(final Object message, final Throwable t) {
getLogger().log(FQCN, traceLevel, message, t);
}
/**
* Logs a message with {@code org.apache.log4j.Priority.WARN}.
*
* @param message to log
* @see org.apache.commons.logging.Log#warn(Object)
*/
@Override
public void warn(final Object message) {
getLogger().log(FQCN, Level.WARN, message, null);
}
/**
* Logs a message with {@code org.apache.log4j.Priority.WARN}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#warn(Object, Throwable)
*/
@Override
public void warn(final Object message, final Throwable t) {
getLogger().log(FQCN, Level.WARN, message, t);
}
}

View File

@@ -62,49 +62,6 @@ public class LogKitLogger implements Log, Serializable {
// --------------------------------------------------------- Public Methods
/**
* Gets the underlying Logger we are using.
*
* @return the underlying Logger we are using.
*/
public Logger getLogger() {
Logger result = logger;
if (result == null) {
synchronized(this) {
result = logger;
if (result == null) {
logger = result = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
}
}
}
return result;
}
// ----------------------------------------------------- Log Implementation
/**
* Logs a message with {@code org.apache.log.Priority.DEBUG}.
*
* @param message to log
* @see org.apache.commons.logging.Log#trace(Object)
*/
@Override
public void trace(final Object message) {
debug(message);
}
/**
* Logs a message with {@code org.apache.log.Priority.DEBUG}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#trace(Object, Throwable)
*/
@Override
public void trace(final Object message, final Throwable t) {
debug(message, t);
}
/**
* Logs a message with {@code org.apache.log.Priority.DEBUG}.
*
@@ -118,6 +75,8 @@ public class LogKitLogger implements Log, Serializable {
}
}
// ----------------------------------------------------- Log Implementation
/**
* Logs a message with {@code org.apache.log.Priority.DEBUG}.
*
@@ -132,60 +91,6 @@ public class LogKitLogger implements Log, Serializable {
}
}
/**
* Logs a message with {@code org.apache.log.Priority.INFO}.
*
* @param message to log
* @see org.apache.commons.logging.Log#info(Object)
*/
@Override
public void info(final Object message) {
if (message != null) {
getLogger().info(String.valueOf(message));
}
}
/**
* Logs a message with {@code org.apache.log.Priority.INFO}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#info(Object, Throwable)
*/
@Override
public void info(final Object message, final Throwable t) {
if (message != null) {
getLogger().info(String.valueOf(message), t);
}
}
/**
* Logs a message with {@code org.apache.log.Priority.WARN}.
*
* @param message to log
* @see org.apache.commons.logging.Log#warn(Object)
*/
@Override
public void warn(final Object message) {
if (message != null) {
getLogger().warn(String.valueOf(message));
}
}
/**
* Logs a message with {@code org.apache.log.Priority.WARN}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#warn(Object, Throwable)
*/
@Override
public void warn(final Object message, final Throwable t) {
if (message != null) {
getLogger().warn(String.valueOf(message), t);
}
}
/**
* Logs a message with {@code org.apache.log.Priority.ERROR}.
*
@@ -240,6 +145,51 @@ public class LogKitLogger implements Log, Serializable {
}
}
/**
* Gets the underlying Logger we are using.
*
* @return the underlying Logger we are using.
*/
public Logger getLogger() {
Logger result = logger;
if (result == null) {
synchronized(this) {
result = logger;
if (result == null) {
logger = result = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
}
}
}
return result;
}
/**
* Logs a message with {@code org.apache.log.Priority.INFO}.
*
* @param message to log
* @see org.apache.commons.logging.Log#info(Object)
*/
@Override
public void info(final Object message) {
if (message != null) {
getLogger().info(String.valueOf(message));
}
}
/**
* Logs a message with {@code org.apache.log.Priority.INFO}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#info(Object, Throwable)
*/
@Override
public void info(final Object message, final Throwable t) {
if (message != null) {
getLogger().info(String.valueOf(message), t);
}
}
/**
* Checks whether the {@code LogKit} logger will log messages of priority {@code DEBUG}.
*/
@@ -287,4 +237,54 @@ public class LogKitLogger implements Log, Serializable {
public boolean isWarnEnabled() {
return getLogger().isWarnEnabled();
}
/**
* Logs a message with {@code org.apache.log.Priority.DEBUG}.
*
* @param message to log
* @see org.apache.commons.logging.Log#trace(Object)
*/
@Override
public void trace(final Object message) {
debug(message);
}
/**
* Logs a message with {@code org.apache.log.Priority.DEBUG}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#trace(Object, Throwable)
*/
@Override
public void trace(final Object message, final Throwable t) {
debug(message, t);
}
/**
* Logs a message with {@code org.apache.log.Priority.WARN}.
*
* @param message to log
* @see org.apache.commons.logging.Log#warn(Object)
*/
@Override
public void warn(final Object message) {
if (message != null) {
getLogger().warn(String.valueOf(message));
}
}
/**
* Logs a message with {@code org.apache.log.Priority.WARN}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#warn(Object, Throwable)
*/
@Override
public void warn(final Object message, final Throwable t) {
if (message != null) {
getLogger().warn(String.valueOf(message), t);
}
}
}

View File

@@ -40,16 +40,6 @@ public class NoOpLog implements Log, Serializable {
public NoOpLog(final String name) {
}
/** Do nothing */
@Override
public void trace(final Object message) {
}
/** Do nothing */
@Override
public void trace(final Object message, final Throwable t) {
}
/** Do nothing */
@Override
public void debug(final Object message) {
@@ -60,26 +50,6 @@ public class NoOpLog implements Log, Serializable {
public void debug(final Object message, final Throwable t) {
}
/** Do nothing */
@Override
public void info(final Object message) {
}
/** Do nothing */
@Override
public void info(final Object message, final Throwable t) {
}
/** Do nothing */
@Override
public void warn(final Object message) {
}
/** Do nothing */
@Override
public void warn(final Object message, final Throwable t) {
}
/** Do nothing */
@Override
public void error(final Object message) {
@@ -100,6 +70,16 @@ public class NoOpLog implements Log, Serializable {
public void fatal(final Object message, final Throwable t) {
}
/** Do nothing */
@Override
public void info(final Object message) {
}
/** Do nothing */
@Override
public void info(final Object message, final Throwable t) {
}
/**
* Debug is never enabled.
*
@@ -159,4 +139,24 @@ public class NoOpLog implements Log, Serializable {
public final boolean isWarnEnabled() {
return false;
}
/** Do nothing */
@Override
public void trace(final Object message) {
}
/** Do nothing */
@Override
public void trace(final Object message, final Throwable t) {
}
/** Do nothing */
@Override
public void warn(final Object message) {
}
/** Do nothing */
@Override
public void warn(final Object message, final Throwable t) {
}
}

View File

@@ -133,26 +133,6 @@ public class SimpleLog implements Log, Serializable {
// ------------------------------------------------------------ Initializer
private static String getStringProperty(final String name) {
String prop = null;
try {
prop = System.getProperty(name);
} catch (final SecurityException e) {
// Ignore
}
return prop == null ? simpleLogProps.getProperty(name) : prop;
}
private static String getStringProperty(final String name, final String dephault) {
final String prop = getStringProperty(name);
return prop == null ? dephault : prop;
}
private static boolean getBooleanProperty(final String name, final boolean dephault) {
final String prop = getStringProperty(name);
return prop == null ? dephault : "true".equalsIgnoreCase(prop);
}
// Initialize class attributes.
// Load properties file, if found.
// Override with system properties.
@@ -190,423 +170,9 @@ public class SimpleLog implements Log, Serializable {
}
}
// ------------------------------------------------------------- Attributes
/** The name of this simple log instance */
protected volatile String logName;
/** The current log level */
protected volatile int currentLogLevel;
/** The short name of this simple log instance */
private volatile String shortLogName;
// ------------------------------------------------------------ Constructor
/**
* Construct a simple log with given name.
*
* @param name log name
*/
public SimpleLog(String name) {
logName = name;
// Set initial log level
// Used to be: set default log level to ERROR
// IMHO it should be lower, but at least info ( costin ).
setLevel(SimpleLog.LOG_LEVEL_INFO);
// Set log level from properties
String lvl = getStringProperty(systemPrefix + "log." + logName);
int i = String.valueOf(name).lastIndexOf(".");
while(null == lvl && i > -1) {
name = name.substring(0,i);
lvl = getStringProperty(systemPrefix + "log." + name);
i = String.valueOf(name).lastIndexOf(".");
}
if (null == lvl) {
lvl = getStringProperty(systemPrefix + "defaultlog");
}
if ("all".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_ALL);
} else if ("trace".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_TRACE);
} else if ("debug".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_DEBUG);
} else if ("info".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_INFO);
} else if ("warn".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_WARN);
} else if ("error".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_ERROR);
} else if ("fatal".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_FATAL);
} else if ("off".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_OFF);
}
}
// -------------------------------------------------------- Properties
/**
* Set logging level.
*
* @param currentLogLevel new logging level
*/
public void setLevel(final int currentLogLevel) {
this.currentLogLevel = currentLogLevel;
}
/**
* Get logging level.
*
* @return logging level.
*/
public int getLevel() {
return currentLogLevel;
}
// -------------------------------------------------------- Logging Methods
/**
* Do the actual logging.
* <p>
* This method assembles the message and then calls {@code write()}
* to cause it to be written.
*
* @param type One of the LOG_LEVEL_XXX constants defining the log level
* @param message The message itself (typically a String)
* @param t The exception whose stack trace should be logged
*/
protected void log(final int type, final Object message, final Throwable t) {
// Use a string buffer for better performance
final StringBuilder buf = new StringBuilder();
// Append date-time if so configured
if (showDateTime) {
final Date now = new Date();
String dateText;
synchronized(dateFormatter) {
dateText = dateFormatter.format(now);
}
buf.append(dateText);
buf.append(" ");
}
// Append a readable representation of the log level
switch(type) {
case SimpleLog.LOG_LEVEL_TRACE: buf.append("[TRACE] "); break;
case SimpleLog.LOG_LEVEL_DEBUG: buf.append("[DEBUG] "); break;
case SimpleLog.LOG_LEVEL_INFO: buf.append("[INFO] "); break;
case SimpleLog.LOG_LEVEL_WARN: buf.append("[WARN] "); break;
case SimpleLog.LOG_LEVEL_ERROR: buf.append("[ERROR] "); break;
case SimpleLog.LOG_LEVEL_FATAL: buf.append("[FATAL] "); break;
}
// Append the name of the log instance if so configured
if (showShortName) {
if (shortLogName == null) {
// Cut all but the last component of the name for both styles
final String slName = logName.substring(logName.lastIndexOf(".") + 1);
shortLogName = slName.substring(slName.lastIndexOf("/") + 1);
}
buf.append(String.valueOf(shortLogName)).append(" - ");
} else if (showLogName) {
buf.append(String.valueOf(logName)).append(" - ");
}
// Append the message
buf.append(String.valueOf(message));
// Append stack trace if not null
if (t != null) {
buf.append(" <");
buf.append(t.toString());
buf.append(">");
final StringWriter sw = new StringWriter(1024);
final PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
pw.close();
buf.append(sw.toString());
}
// Print to the appropriate destination
write(buf);
}
/**
* Write the content of the message accumulated in the specified
* {@code StringBuffer} to the appropriate output destination. The
* default implementation writes to {@code System.err}.
*
* @param buffer A {@code StringBuffer} containing the accumulated
* text to be logged
*/
protected void write(final StringBuffer buffer) {
System.err.println(buffer.toString());
}
/**
* Write the content of the message accumulated in the specified
* {@code StringBuffer} to the appropriate output destination. The
* default implementation writes to {@code System.err}.
*
* @param buffer A {@code StringBuffer} containing the accumulated
* text to be logged
*/
private void write(final Object buffer) {
System.err.println(buffer.toString());
}
/**
* Tests whether the given log level currently enabled.
*
* @param logLevel is this level enabled?
* @return whether the given log level currently enabled.
*/
protected boolean isLevelEnabled(final int logLevel) {
// log level are numerically ordered so can use simple numeric
// comparison
return logLevel >= currentLogLevel;
}
// -------------------------------------------------------- Log Implementation
/**
* Logs a message with
* {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_DEBUG}.
*
* @param message to log
* @see org.apache.commons.logging.Log#debug(Object)
*/
@Override
public final void debug(final Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
log(SimpleLog.LOG_LEVEL_DEBUG, message, null);
}
}
/**
* Logs a message with
* {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_DEBUG}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#debug(Object, Throwable)
*/
@Override
public final void debug(final Object message, final Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
log(SimpleLog.LOG_LEVEL_DEBUG, message, t);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_TRACE}.
*
* @param message to log
* @see org.apache.commons.logging.Log#trace(Object)
*/
@Override
public final void trace(final Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) {
log(SimpleLog.LOG_LEVEL_TRACE, message, null);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_TRACE}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#trace(Object, Throwable)
*/
@Override
public final void trace(final Object message, final Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) {
log(SimpleLog.LOG_LEVEL_TRACE, message, t);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_INFO}.
*
* @param message to log
* @see org.apache.commons.logging.Log#info(Object)
*/
@Override
public final void info(final Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
log(SimpleLog.LOG_LEVEL_INFO,message,null);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_INFO}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#info(Object, Throwable)
*/
@Override
public final void info(final Object message, final Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
log(SimpleLog.LOG_LEVEL_INFO, message, t);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_WARN}.
*
* @param message to log
* @see org.apache.commons.logging.Log#warn(Object)
*/
@Override
public final void warn(final Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
log(SimpleLog.LOG_LEVEL_WARN, message, null);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_WARN}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#warn(Object, Throwable)
*/
@Override
public final void warn(final Object message, final Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
log(SimpleLog.LOG_LEVEL_WARN, message, t);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_ERROR}.
*
* @param message to log
* @see org.apache.commons.logging.Log#error(Object)
*/
@Override
public final void error(final Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
log(SimpleLog.LOG_LEVEL_ERROR, message, null);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_ERROR}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#error(Object, Throwable)
*/
@Override
public final void error(final Object message, final Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
log(SimpleLog.LOG_LEVEL_ERROR, message, t);
}
}
/**
* Log a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_FATAL}.
*
* @param message to log
* @see org.apache.commons.logging.Log#fatal(Object)
*/
@Override
public final void fatal(final Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
log(SimpleLog.LOG_LEVEL_FATAL, message, null);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_FATAL}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#fatal(Object, Throwable)
*/
@Override
public final void fatal(final Object message, final Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
log(SimpleLog.LOG_LEVEL_FATAL, message, t);
}
}
/**
* Are debug messages currently enabled?
* <p>
* This allows expensive operations such as {@code String}
* concatenation to be avoided when the message will be ignored by the
* logger.
*/
@Override
public final boolean isDebugEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG);
}
/**
* Are error messages currently enabled?
* <p>
* This allows expensive operations such as {@code String}
* concatenation to be avoided when the message will be ignored by the
* logger.
*/
@Override
public final boolean isErrorEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR);
}
/**
* Are fatal messages currently enabled?
* <p>
* This allows expensive operations such as {@code String}
* concatenation to be avoided when the message will be ignored by the
* logger.
*/
@Override
public final boolean isFatalEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL);
}
/**
* Are info messages currently enabled?
* <p>
* This allows expensive operations such as {@code String}
* concatenation to be avoided when the message will be ignored by the
* logger.
*/
@Override
public final boolean isInfoEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_INFO);
}
/**
* Are trace messages currently enabled?
* <p>
* This allows expensive operations such as {@code String}
* concatenation to be avoided when the message will be ignored by the
* logger.
*/
@Override
public final boolean isTraceEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE);
}
/**
* Are warn messages currently enabled?
* <p>
* This allows expensive operations such as {@code String}
* concatenation to be avoided when the message will be ignored by the
* logger.
*/
@Override
public final boolean isWarnEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_WARN);
private static boolean getBooleanProperty(final String name, final boolean dephault) {
final String prop = getStringProperty(name);
return prop == null ? dephault : "true".equalsIgnoreCase(prop);
}
/**
@@ -682,5 +248,439 @@ public class SimpleLog implements Log, Serializable {
}
});
}
// ------------------------------------------------------------- Attributes
private static String getStringProperty(final String name) {
String prop = null;
try {
prop = System.getProperty(name);
} catch (final SecurityException e) {
// Ignore
}
return prop == null ? simpleLogProps.getProperty(name) : prop;
}
private static String getStringProperty(final String name, final String dephault) {
final String prop = getStringProperty(name);
return prop == null ? dephault : prop;
}
/** The name of this simple log instance */
protected volatile String logName;
// ------------------------------------------------------------ Constructor
/** The current log level */
protected volatile int currentLogLevel;
// -------------------------------------------------------- Properties
/** The short name of this simple log instance */
private volatile String shortLogName;
/**
* Construct a simple log with given name.
*
* @param name log name
*/
public SimpleLog(String name) {
logName = name;
// Set initial log level
// Used to be: set default log level to ERROR
// IMHO it should be lower, but at least info ( costin ).
setLevel(SimpleLog.LOG_LEVEL_INFO);
// Set log level from properties
String lvl = getStringProperty(systemPrefix + "log." + logName);
int i = String.valueOf(name).lastIndexOf(".");
while(null == lvl && i > -1) {
name = name.substring(0,i);
lvl = getStringProperty(systemPrefix + "log." + name);
i = String.valueOf(name).lastIndexOf(".");
}
if (null == lvl) {
lvl = getStringProperty(systemPrefix + "defaultlog");
}
if ("all".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_ALL);
} else if ("trace".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_TRACE);
} else if ("debug".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_DEBUG);
} else if ("info".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_INFO);
} else if ("warn".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_WARN);
} else if ("error".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_ERROR);
} else if ("fatal".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_FATAL);
} else if ("off".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_OFF);
}
}
// -------------------------------------------------------- Logging Methods
/**
* Logs a message with
* {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_DEBUG}.
*
* @param message to log
* @see org.apache.commons.logging.Log#debug(Object)
*/
@Override
public final void debug(final Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
log(SimpleLog.LOG_LEVEL_DEBUG, message, null);
}
}
/**
* Logs a message with
* {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_DEBUG}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#debug(Object, Throwable)
*/
@Override
public final void debug(final Object message, final Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
log(SimpleLog.LOG_LEVEL_DEBUG, message, t);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_ERROR}.
*
* @param message to log
* @see org.apache.commons.logging.Log#error(Object)
*/
@Override
public final void error(final Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
log(SimpleLog.LOG_LEVEL_ERROR, message, null);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_ERROR}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#error(Object, Throwable)
*/
@Override
public final void error(final Object message, final Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
log(SimpleLog.LOG_LEVEL_ERROR, message, t);
}
}
// -------------------------------------------------------- Log Implementation
/**
* Log a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_FATAL}.
*
* @param message to log
* @see org.apache.commons.logging.Log#fatal(Object)
*/
@Override
public final void fatal(final Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
log(SimpleLog.LOG_LEVEL_FATAL, message, null);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_FATAL}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#fatal(Object, Throwable)
*/
@Override
public final void fatal(final Object message, final Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
log(SimpleLog.LOG_LEVEL_FATAL, message, t);
}
}
/**
* Get logging level.
*
* @return logging level.
*/
public int getLevel() {
return currentLogLevel;
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_INFO}.
*
* @param message to log
* @see org.apache.commons.logging.Log#info(Object)
*/
@Override
public final void info(final Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
log(SimpleLog.LOG_LEVEL_INFO,message,null);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_INFO}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#info(Object, Throwable)
*/
@Override
public final void info(final Object message, final Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
log(SimpleLog.LOG_LEVEL_INFO, message, t);
}
}
/**
* Are debug messages currently enabled?
* <p>
* This allows expensive operations such as {@code String}
* concatenation to be avoided when the message will be ignored by the
* logger.
*/
@Override
public final boolean isDebugEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG);
}
/**
* Are error messages currently enabled?
* <p>
* This allows expensive operations such as {@code String}
* concatenation to be avoided when the message will be ignored by the
* logger.
*/
@Override
public final boolean isErrorEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR);
}
/**
* Are fatal messages currently enabled?
* <p>
* This allows expensive operations such as {@code String}
* concatenation to be avoided when the message will be ignored by the
* logger.
*/
@Override
public final boolean isFatalEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL);
}
/**
* Are info messages currently enabled?
* <p>
* This allows expensive operations such as {@code String}
* concatenation to be avoided when the message will be ignored by the
* logger.
*/
@Override
public final boolean isInfoEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_INFO);
}
/**
* Tests whether the given log level currently enabled.
*
* @param logLevel is this level enabled?
* @return whether the given log level currently enabled.
*/
protected boolean isLevelEnabled(final int logLevel) {
// log level are numerically ordered so can use simple numeric
// comparison
return logLevel >= currentLogLevel;
}
/**
* Are trace messages currently enabled?
* <p>
* This allows expensive operations such as {@code String}
* concatenation to be avoided when the message will be ignored by the
* logger.
*/
@Override
public final boolean isTraceEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE);
}
/**
* Are warn messages currently enabled?
* <p>
* This allows expensive operations such as {@code String}
* concatenation to be avoided when the message will be ignored by the
* logger.
*/
@Override
public final boolean isWarnEnabled() {
return isLevelEnabled(SimpleLog.LOG_LEVEL_WARN);
}
/**
* Do the actual logging.
* <p>
* This method assembles the message and then calls {@code write()}
* to cause it to be written.
*
* @param type One of the LOG_LEVEL_XXX constants defining the log level
* @param message The message itself (typically a String)
* @param t The exception whose stack trace should be logged
*/
protected void log(final int type, final Object message, final Throwable t) {
// Use a string buffer for better performance
final StringBuilder buf = new StringBuilder();
// Append date-time if so configured
if (showDateTime) {
final Date now = new Date();
String dateText;
synchronized(dateFormatter) {
dateText = dateFormatter.format(now);
}
buf.append(dateText);
buf.append(" ");
}
// Append a readable representation of the log level
switch(type) {
case SimpleLog.LOG_LEVEL_TRACE: buf.append("[TRACE] "); break;
case SimpleLog.LOG_LEVEL_DEBUG: buf.append("[DEBUG] "); break;
case SimpleLog.LOG_LEVEL_INFO: buf.append("[INFO] "); break;
case SimpleLog.LOG_LEVEL_WARN: buf.append("[WARN] "); break;
case SimpleLog.LOG_LEVEL_ERROR: buf.append("[ERROR] "); break;
case SimpleLog.LOG_LEVEL_FATAL: buf.append("[FATAL] "); break;
}
// Append the name of the log instance if so configured
if (showShortName) {
if (shortLogName == null) {
// Cut all but the last component of the name for both styles
final String slName = logName.substring(logName.lastIndexOf(".") + 1);
shortLogName = slName.substring(slName.lastIndexOf("/") + 1);
}
buf.append(String.valueOf(shortLogName)).append(" - ");
} else if (showLogName) {
buf.append(String.valueOf(logName)).append(" - ");
}
// Append the message
buf.append(String.valueOf(message));
// Append stack trace if not null
if (t != null) {
buf.append(" <");
buf.append(t.toString());
buf.append(">");
final StringWriter sw = new StringWriter(1024);
final PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
pw.close();
buf.append(sw.toString());
}
// Print to the appropriate destination
write(buf);
}
/**
* Set logging level.
*
* @param currentLogLevel new logging level
*/
public void setLevel(final int currentLogLevel) {
this.currentLogLevel = currentLogLevel;
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_TRACE}.
*
* @param message to log
* @see org.apache.commons.logging.Log#trace(Object)
*/
@Override
public final void trace(final Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) {
log(SimpleLog.LOG_LEVEL_TRACE, message, null);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_TRACE}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#trace(Object, Throwable)
*/
@Override
public final void trace(final Object message, final Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) {
log(SimpleLog.LOG_LEVEL_TRACE, message, t);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_WARN}.
*
* @param message to log
* @see org.apache.commons.logging.Log#warn(Object)
*/
@Override
public final void warn(final Object message) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
log(SimpleLog.LOG_LEVEL_WARN, message, null);
}
}
/**
* Logs a message with {@code org.apache.commons.logging.impl.SimpleLog.LOG_LEVEL_WARN}.
*
* @param message to log
* @param t log this cause
* @see org.apache.commons.logging.Log#warn(Object, Throwable)
*/
@Override
public final void warn(final Object message, final Throwable t) {
if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
log(SimpleLog.LOG_LEVEL_WARN, message, t);
}
}
/**
* Write the content of the message accumulated in the specified
* {@code StringBuffer} to the appropriate output destination. The
* default implementation writes to {@code System.err}.
*
* @param buffer A {@code StringBuffer} containing the accumulated
* text to be logged
*/
private void write(final Object buffer) {
System.err.println(buffer.toString());
}
/**
* Write the content of the message accumulated in the specified
* {@code StringBuffer} to the appropriate output destination. The
* default implementation writes to {@code System.err}.
*
* @param buffer A {@code StringBuffer} containing the accumulated
* text to be logged
*/
protected void write(final StringBuffer buffer) {
System.err.println(buffer.toString());
}
}

View File

@@ -109,6 +109,144 @@ import java.util.Set;
*/
public final class WeakHashtable extends Hashtable {
/** Entry implementation */
private final static class Entry implements Map.Entry {
private final Object key;
private final Object value;
private Entry(final Object key, final Object value) {
this.key = key;
this.value = value;
}
@Override
public boolean equals(final Object o) {
boolean result = false;
if (o instanceof Map.Entry) {
final Map.Entry entry = (Map.Entry) o;
result = (getKey()==null ?
entry.getKey() == null :
getKey().equals(entry.getKey())) &&
(getValue()==null ?
entry.getValue() == null :
getValue().equals(entry.getValue()));
}
return result;
}
@Override
public Object getKey() {
return key;
}
@Override
public Object getValue() {
return value;
}
@Override
public int hashCode() {
return (getKey()==null ? 0 : getKey().hashCode()) ^
(getValue()==null ? 0 : getValue().hashCode());
}
@Override
public Object setValue(final Object value) {
throw new UnsupportedOperationException("Entry.setValue is not supported.");
}
}
/** Wrapper giving correct symantics for equals and hash code */
private final static class Referenced {
private final WeakReference reference;
private final int hashCode;
/**
*
* @throws NullPointerException if referant is {@code null}
*/
private Referenced(final Object referant) {
reference = new WeakReference(referant);
// Calc a permanent hashCode so calls to Hashtable.remove()
// work if the WeakReference has been cleared
hashCode = referant.hashCode();
}
/**
*
* @throws NullPointerException if key is {@code null}
*/
private Referenced(final Object key, final ReferenceQueue queue) {
reference = new WeakKey(key, queue, this);
// Calc a permanent hashCode so calls to Hashtable.remove()
// work if the WeakReference has been cleared
hashCode = key.hashCode();
}
@Override
public boolean equals(final Object o) {
boolean result = false;
if (o instanceof Referenced) {
final Referenced otherKey = (Referenced) o;
final Object thisKeyValue = getValue();
final Object otherKeyValue = otherKey.getValue();
if (thisKeyValue == null) {
result = otherKeyValue == null;
// Since our hash code was calculated from the original
// non-null referant, the above check breaks the
// hash code/equals contract, as two cleared Referenced
// objects could test equal but have different hash codes.
// We can reduce (not eliminate) the chance of this
// happening by comparing hash codes.
result = result && this.hashCode() == otherKey.hashCode();
// In any case, as our c'tor does not allow null referants
// and Hashtable does not do equality checks between
// existing keys, normal hashtable operations should never
// result in an equals comparison between null referants
}
else
{
result = thisKeyValue.equals(otherKeyValue);
}
}
return result;
}
private Object getValue() {
return reference.get();
}
@Override
public int hashCode() {
return hashCode;
}
}
/**
* WeakReference subclass that holds a hard reference to an
* associated {@code value} and also makes accessible
* the Referenced object holding it.
*/
private final static class WeakKey extends WeakReference {
private final Referenced referenced;
private WeakKey(final Object key,
final ReferenceQueue queue,
final Referenced referenced) {
super(key, queue);
this.referenced = referenced;
}
private Referenced getReferenced() {
return referenced;
}
}
/** Serializable version identifier. */
private static final long serialVersionUID = -1546036869799732453L;
@@ -186,6 +324,15 @@ public final class WeakHashtable extends Hashtable {
return super.get(referenceKey);
}
/**
*@see Hashtable
*/
@Override
public boolean isEmpty() {
purge();
return super.isEmpty();
}
/**
*@see Hashtable
*/
@@ -224,6 +371,41 @@ public final class WeakHashtable extends Hashtable {
return unreferencedKeys;
}
/**
* Purges all entries whose wrapped keys
* have been garbage collected.
*/
private void purge() {
final List toRemove = new ArrayList();
synchronized (queue) {
WeakKey key;
while ((key = (WeakKey) queue.poll()) != null) {
toRemove.add(key.getReferenced());
}
}
// LOGGING-119: do the actual removal of the keys outside the sync block
// to prevent deadlock scenarios as purge() may be called from
// non-synchronized methods too
final int size = toRemove.size();
for (int i = 0; i < size; i++) {
super.remove(toRemove.get(i));
}
}
/**
* Purges one entry whose wrapped key
* has been garbage collected.
*/
private void purgeOne() {
synchronized (queue) {
final WeakKey key = (WeakKey) queue.poll();
if (key != null) {
super.remove(key.getReferenced());
}
}
}
/**
*@see Hashtable
*/
@@ -263,12 +445,13 @@ public final class WeakHashtable extends Hashtable {
}
/**
*@see Hashtable
* @see Hashtable
*/
@Override
public Collection values() {
protected void rehash() {
// purge here to save the effort of rehashing dead entries
purge();
return super.values();
super.rehash();
}
/**
@@ -289,15 +472,6 @@ public final class WeakHashtable extends Hashtable {
return super.remove(new Referenced(key));
}
/**
*@see Hashtable
*/
@Override
public boolean isEmpty() {
purge();
return super.isEmpty();
}
/**
*@see Hashtable
*/
@@ -317,185 +491,11 @@ public final class WeakHashtable extends Hashtable {
}
/**
* @see Hashtable
*@see Hashtable
*/
@Override
protected void rehash() {
// purge here to save the effort of rehashing dead entries
public Collection values() {
purge();
super.rehash();
return super.values();
}
/**
* Purges all entries whose wrapped keys
* have been garbage collected.
*/
private void purge() {
final List toRemove = new ArrayList();
synchronized (queue) {
WeakKey key;
while ((key = (WeakKey) queue.poll()) != null) {
toRemove.add(key.getReferenced());
}
}
// LOGGING-119: do the actual removal of the keys outside the sync block
// to prevent deadlock scenarios as purge() may be called from
// non-synchronized methods too
final int size = toRemove.size();
for (int i = 0; i < size; i++) {
super.remove(toRemove.get(i));
}
}
/**
* Purges one entry whose wrapped key
* has been garbage collected.
*/
private void purgeOne() {
synchronized (queue) {
final WeakKey key = (WeakKey) queue.poll();
if (key != null) {
super.remove(key.getReferenced());
}
}
}
/** Entry implementation */
private final static class Entry implements Map.Entry {
private final Object key;
private final Object value;
private Entry(final Object key, final Object value) {
this.key = key;
this.value = value;
}
@Override
public boolean equals(final Object o) {
boolean result = false;
if (o instanceof Map.Entry) {
final Map.Entry entry = (Map.Entry) o;
result = (getKey()==null ?
entry.getKey() == null :
getKey().equals(entry.getKey())) &&
(getValue()==null ?
entry.getValue() == null :
getValue().equals(entry.getValue()));
}
return result;
}
@Override
public int hashCode() {
return (getKey()==null ? 0 : getKey().hashCode()) ^
(getValue()==null ? 0 : getValue().hashCode());
}
@Override
public Object setValue(final Object value) {
throw new UnsupportedOperationException("Entry.setValue is not supported.");
}
@Override
public Object getValue() {
return value;
}
@Override
public Object getKey() {
return key;
}
}
/** Wrapper giving correct symantics for equals and hash code */
private final static class Referenced {
private final WeakReference reference;
private final int hashCode;
/**
*
* @throws NullPointerException if referant is {@code null}
*/
private Referenced(final Object referant) {
reference = new WeakReference(referant);
// Calc a permanent hashCode so calls to Hashtable.remove()
// work if the WeakReference has been cleared
hashCode = referant.hashCode();
}
/**
*
* @throws NullPointerException if key is {@code null}
*/
private Referenced(final Object key, final ReferenceQueue queue) {
reference = new WeakKey(key, queue, this);
// Calc a permanent hashCode so calls to Hashtable.remove()
// work if the WeakReference has been cleared
hashCode = key.hashCode();
}
@Override
public int hashCode() {
return hashCode;
}
private Object getValue() {
return reference.get();
}
@Override
public boolean equals(final Object o) {
boolean result = false;
if (o instanceof Referenced) {
final Referenced otherKey = (Referenced) o;
final Object thisKeyValue = getValue();
final Object otherKeyValue = otherKey.getValue();
if (thisKeyValue == null) {
result = otherKeyValue == null;
// Since our hash code was calculated from the original
// non-null referant, the above check breaks the
// hash code/equals contract, as two cleared Referenced
// objects could test equal but have different hash codes.
// We can reduce (not eliminate) the chance of this
// happening by comparing hash codes.
result = result && this.hashCode() == otherKey.hashCode();
// In any case, as our c'tor does not allow null referants
// and Hashtable does not do equality checks between
// existing keys, normal hashtable operations should never
// result in an equals comparison between null referants
}
else
{
result = thisKeyValue.equals(otherKeyValue);
}
}
return result;
}
}
/**
* WeakReference subclass that holds a hard reference to an
* associated {@code value} and also makes accessible
* the Referenced object holding it.
*/
private final static class WeakKey extends WeakReference {
private final Referenced referenced;
private WeakKey(final Object key,
final ReferenceQueue queue,
final Referenced referenced) {
super(key, queue);
this.referenced = referenced;
}
private Referenced getReferenced() {
return referenced;
}
}
}