2010-09-29 02:12:23 -04:00
|
|
|
package spark
|
|
|
|
|
|
|
|
import org.slf4j.Logger
|
|
|
|
import org.slf4j.LoggerFactory
|
|
|
|
|
|
|
|
/**
|
2012-02-10 11:19:53 -05:00
|
|
|
* Utility trait for classes that want to log data. Creates a SLF4J logger for the class and allows
|
|
|
|
* logging messages at different levels using methods that only evaluate parameters lazily if the
|
|
|
|
* log level is enabled.
|
2010-09-29 02:12:23 -04:00
|
|
|
*/
|
|
|
|
trait Logging {
|
|
|
|
// Make the log field transient so that objects with Logging can
|
|
|
|
// be serialized and used on another machine
|
2012-02-10 11:19:53 -05:00
|
|
|
@transient
|
|
|
|
private var log_ : Logger = null
|
2010-09-29 02:12:23 -04:00
|
|
|
|
2010-09-29 03:10:46 -04:00
|
|
|
// Method to get or create the logger for this object
|
2012-10-05 01:59:36 -04:00
|
|
|
protected def log: Logger = {
|
2010-09-29 03:10:46 -04:00
|
|
|
if (log_ == null) {
|
2012-06-29 21:47:12 -04:00
|
|
|
var className = this.getClass.getName
|
2010-09-29 03:10:46 -04:00
|
|
|
// Ignore trailing $'s in the class names for Scala objects
|
2012-02-10 11:19:53 -05:00
|
|
|
if (className.endsWith("$")) {
|
2010-09-29 03:10:46 -04:00
|
|
|
className = className.substring(0, className.length - 1)
|
2012-02-10 11:19:53 -05:00
|
|
|
}
|
2010-09-29 03:10:46 -04:00
|
|
|
log_ = LoggerFactory.getLogger(className)
|
|
|
|
}
|
2010-09-29 02:12:23 -04:00
|
|
|
return log_
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log methods that take only a String
|
2012-10-05 01:59:36 -04:00
|
|
|
protected def logInfo(msg: => String) {
|
2012-06-29 21:47:12 -04:00
|
|
|
if (log.isInfoEnabled) log.info(msg)
|
|
|
|
}
|
2010-09-29 02:12:23 -04:00
|
|
|
|
2012-10-05 01:59:36 -04:00
|
|
|
protected def logDebug(msg: => String) {
|
2012-06-29 21:47:12 -04:00
|
|
|
if (log.isDebugEnabled) log.debug(msg)
|
|
|
|
}
|
2012-06-07 03:25:47 -04:00
|
|
|
|
2012-10-05 01:59:36 -04:00
|
|
|
protected def logTrace(msg: => String) {
|
2012-06-29 21:47:12 -04:00
|
|
|
if (log.isTraceEnabled) log.trace(msg)
|
|
|
|
}
|
2010-09-29 02:12:23 -04:00
|
|
|
|
2012-10-05 01:59:36 -04:00
|
|
|
protected def logWarning(msg: => String) {
|
2012-06-29 21:47:12 -04:00
|
|
|
if (log.isWarnEnabled) log.warn(msg)
|
|
|
|
}
|
2010-09-29 02:12:23 -04:00
|
|
|
|
2012-10-05 01:59:36 -04:00
|
|
|
protected def logError(msg: => String) {
|
2012-06-29 21:47:12 -04:00
|
|
|
if (log.isErrorEnabled) log.error(msg)
|
|
|
|
}
|
2010-09-29 02:12:23 -04:00
|
|
|
|
|
|
|
// Log methods that take Throwables (Exceptions/Errors) too
|
2012-10-05 01:59:36 -04:00
|
|
|
protected def logInfo(msg: => String, throwable: Throwable) {
|
2012-07-30 13:41:09 -04:00
|
|
|
if (log.isInfoEnabled) log.info(msg, throwable)
|
2012-06-29 21:47:12 -04:00
|
|
|
}
|
2010-09-29 02:12:23 -04:00
|
|
|
|
2012-10-05 01:59:36 -04:00
|
|
|
protected def logDebug(msg: => String, throwable: Throwable) {
|
2012-07-30 13:41:09 -04:00
|
|
|
if (log.isDebugEnabled) log.debug(msg, throwable)
|
2012-06-29 21:47:12 -04:00
|
|
|
}
|
2010-09-29 02:12:23 -04:00
|
|
|
|
2012-10-05 01:59:36 -04:00
|
|
|
protected def logTrace(msg: => String, throwable: Throwable) {
|
2012-07-30 13:41:09 -04:00
|
|
|
if (log.isTraceEnabled) log.trace(msg, throwable)
|
2012-06-29 21:47:12 -04:00
|
|
|
}
|
2012-06-07 03:25:47 -04:00
|
|
|
|
2012-10-05 01:59:36 -04:00
|
|
|
protected def logWarning(msg: => String, throwable: Throwable) {
|
2010-09-29 02:12:23 -04:00
|
|
|
if (log.isWarnEnabled) log.warn(msg, throwable)
|
2012-06-29 21:47:12 -04:00
|
|
|
}
|
2010-09-29 02:12:23 -04:00
|
|
|
|
2012-10-05 01:59:36 -04:00
|
|
|
protected def logError(msg: => String, throwable: Throwable) {
|
2010-09-29 02:12:23 -04:00
|
|
|
if (log.isErrorEnabled) log.error(msg, throwable)
|
2012-06-29 21:47:12 -04:00
|
|
|
}
|
2011-06-01 02:47:32 -04:00
|
|
|
|
|
|
|
// Method for ensuring that logging is initialized, to avoid having multiple
|
|
|
|
// threads do it concurrently (as SLF4J initialization is not thread safe).
|
2012-10-05 01:59:36 -04:00
|
|
|
protected def initLogging() { log }
|
2010-09-29 02:12:23 -04:00
|
|
|
}
|