[LOGGING-135] Use double-checked locking idiom to improve thread-safety of Log4JLogger and LogKitLogger.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/logging/trunk@1448119 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -53,7 +53,7 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
private static final String FQCN = Log4JLogger.class.getName();
|
private static final String FQCN = Log4JLogger.class.getName();
|
||||||
|
|
||||||
/** Log to this logger */
|
/** Log to this logger */
|
||||||
private transient Logger logger = null;
|
private transient volatile Logger logger = null;
|
||||||
|
|
||||||
/** Logger name */
|
/** Logger name */
|
||||||
private final String name;
|
private final String name;
|
||||||
@@ -111,7 +111,7 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
/**
|
/**
|
||||||
* For use with a log4j factory.
|
* For use with a log4j factory.
|
||||||
*/
|
*/
|
||||||
public Log4JLogger(Logger logger ) {
|
public Log4JLogger(Logger logger) {
|
||||||
if (logger == null) {
|
if (logger == null) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Warning - null logger in constructor; possible log4j misconfiguration.");
|
"Warning - null logger in constructor; possible log4j misconfiguration.");
|
||||||
@@ -129,7 +129,7 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
* @see org.apache.commons.logging.Log#trace(Object)
|
* @see org.apache.commons.logging.Log#trace(Object)
|
||||||
*/
|
*/
|
||||||
public void trace(Object message) {
|
public void trace(Object message) {
|
||||||
getLogger().log(FQCN, traceLevel, message, null );
|
getLogger().log(FQCN, traceLevel, message, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -142,7 +142,7 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
* @see org.apache.commons.logging.Log#trace(Object, Throwable)
|
* @see org.apache.commons.logging.Log#trace(Object, Throwable)
|
||||||
*/
|
*/
|
||||||
public void trace(Object message, Throwable t) {
|
public void trace(Object message, Throwable t) {
|
||||||
getLogger().log(FQCN, traceLevel, message, t );
|
getLogger().log(FQCN, traceLevel, message, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -152,7 +152,7 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
* @see org.apache.commons.logging.Log#debug(Object)
|
* @see org.apache.commons.logging.Log#debug(Object)
|
||||||
*/
|
*/
|
||||||
public void debug(Object message) {
|
public void debug(Object message) {
|
||||||
getLogger().log(FQCN, Level.DEBUG, message, null );
|
getLogger().log(FQCN, Level.DEBUG, message, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -163,7 +163,7 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
* @see org.apache.commons.logging.Log#debug(Object, Throwable)
|
* @see org.apache.commons.logging.Log#debug(Object, Throwable)
|
||||||
*/
|
*/
|
||||||
public void debug(Object message, Throwable t) {
|
public void debug(Object message, Throwable t) {
|
||||||
getLogger().log(FQCN, Level.DEBUG, message, t );
|
getLogger().log(FQCN, Level.DEBUG, message, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -173,7 +173,7 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
* @see org.apache.commons.logging.Log#info(Object)
|
* @see org.apache.commons.logging.Log#info(Object)
|
||||||
*/
|
*/
|
||||||
public void info(Object message) {
|
public void info(Object message) {
|
||||||
getLogger().log(FQCN, Level.INFO, message, null );
|
getLogger().log(FQCN, Level.INFO, message, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -184,7 +184,7 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
* @see org.apache.commons.logging.Log#info(Object, Throwable)
|
* @see org.apache.commons.logging.Log#info(Object, Throwable)
|
||||||
*/
|
*/
|
||||||
public void info(Object message, Throwable t) {
|
public void info(Object message, Throwable t) {
|
||||||
getLogger().log(FQCN, Level.INFO, message, t );
|
getLogger().log(FQCN, Level.INFO, message, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -194,7 +194,7 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
* @see org.apache.commons.logging.Log#warn(Object)
|
* @see org.apache.commons.logging.Log#warn(Object)
|
||||||
*/
|
*/
|
||||||
public void warn(Object message) {
|
public void warn(Object message) {
|
||||||
getLogger().log(FQCN, Level.WARN, message, null );
|
getLogger().log(FQCN, Level.WARN, message, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -205,7 +205,7 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
* @see org.apache.commons.logging.Log#warn(Object, Throwable)
|
* @see org.apache.commons.logging.Log#warn(Object, Throwable)
|
||||||
*/
|
*/
|
||||||
public void warn(Object message, Throwable t) {
|
public void warn(Object message, Throwable t) {
|
||||||
getLogger().log(FQCN, Level.WARN, message, t );
|
getLogger().log(FQCN, Level.WARN, message, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -215,7 +215,7 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
* @see org.apache.commons.logging.Log#error(Object)
|
* @see org.apache.commons.logging.Log#error(Object)
|
||||||
*/
|
*/
|
||||||
public void error(Object message) {
|
public void error(Object message) {
|
||||||
getLogger().log(FQCN, Level.ERROR, message, null );
|
getLogger().log(FQCN, Level.ERROR, message, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -226,7 +226,7 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
* @see org.apache.commons.logging.Log#error(Object, Throwable)
|
* @see org.apache.commons.logging.Log#error(Object, Throwable)
|
||||||
*/
|
*/
|
||||||
public void error(Object message, Throwable t) {
|
public void error(Object message, Throwable t) {
|
||||||
getLogger().log(FQCN, Level.ERROR, message, t );
|
getLogger().log(FQCN, Level.ERROR, message, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -236,7 +236,7 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
* @see org.apache.commons.logging.Log#fatal(Object)
|
* @see org.apache.commons.logging.Log#fatal(Object)
|
||||||
*/
|
*/
|
||||||
public void fatal(Object message) {
|
public void fatal(Object message) {
|
||||||
getLogger().log(FQCN, Level.FATAL, message, null );
|
getLogger().log(FQCN, Level.FATAL, message, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -247,17 +247,23 @@ public class Log4JLogger implements Log, Serializable {
|
|||||||
* @see org.apache.commons.logging.Log#fatal(Object, Throwable)
|
* @see org.apache.commons.logging.Log#fatal(Object, Throwable)
|
||||||
*/
|
*/
|
||||||
public void fatal(Object message, Throwable t) {
|
public void fatal(Object message, Throwable t) {
|
||||||
getLogger().log(FQCN, Level.FATAL, message, t );
|
getLogger().log(FQCN, Level.FATAL, message, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the native Logger instance we are using.
|
* Return the native Logger instance we are using.
|
||||||
*/
|
*/
|
||||||
public Logger getLogger() {
|
public Logger getLogger() {
|
||||||
if (logger == null) {
|
Logger result = logger;
|
||||||
logger = Logger.getLogger(name);
|
if (result == null) {
|
||||||
|
synchronized(this) {
|
||||||
|
result = logger;
|
||||||
|
if (result == null) {
|
||||||
|
logger = result = Logger.getLogger(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this.logger;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public class LogKitLogger implements Log, Serializable {
|
|||||||
// ------------------------------------------------------------- Attributes
|
// ------------------------------------------------------------- Attributes
|
||||||
|
|
||||||
/** Logging goes to this <code>LogKit</code> logger */
|
/** Logging goes to this <code>LogKit</code> logger */
|
||||||
protected transient Logger logger = null;
|
protected transient volatile Logger logger = null;
|
||||||
|
|
||||||
/** Name of this logger */
|
/** Name of this logger */
|
||||||
protected String name = null;
|
protected String name = null;
|
||||||
@@ -65,12 +65,16 @@ public class LogKitLogger implements Log, Serializable {
|
|||||||
* Return the underlying Logger we are using.
|
* Return the underlying Logger we are using.
|
||||||
*/
|
*/
|
||||||
public Logger getLogger() {
|
public Logger getLogger() {
|
||||||
|
Logger result = logger;
|
||||||
if (logger == null) {
|
if (result == null) {
|
||||||
logger = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
|
synchronized(this) {
|
||||||
|
result = logger;
|
||||||
|
if (result == null) {
|
||||||
|
logger = result = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return logger;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------- Log Implementation
|
// ----------------------------------------------------- Log Implementation
|
||||||
|
|||||||
Reference in New Issue
Block a user