spark-instrumented-optimizer/core/src/main/scala/spark/Logging.scala

61 lines
2 KiB
Scala
Raw Normal View History

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
// Method to get or create the logger for this object
2010-09-29 02:12:23 -04:00
def log: Logger = {
if (log_ == null) {
var className = this.getClass().getName()
// Ignore trailing $'s in the class names for Scala objects
2012-02-10 11:19:53 -05:00
if (className.endsWith("$")) {
className = className.substring(0, className.length - 1)
2012-02-10 11:19:53 -05:00
}
log_ = LoggerFactory.getLogger(className)
}
2010-09-29 02:12:23 -04:00
return log_
}
// Log methods that take only a String
def logInfo(msg: => String) = if (log.isInfoEnabled /*&& msg.contains("job finished in")*/) log.info(msg)
2010-09-29 02:12:23 -04:00
def logDebug(msg: => String) = if (log.isDebugEnabled) log.debug(msg)
def logTrace(msg: => String) = if (log.isTraceEnabled) log.trace(msg)
2010-09-29 02:12:23 -04:00
def logWarning(msg: => String) = if (log.isWarnEnabled) log.warn(msg)
def logError(msg: => String) = if (log.isErrorEnabled) log.error(msg)
// Log methods that take Throwables (Exceptions/Errors) too
def logInfo(msg: => String, throwable: Throwable) =
if (log.isInfoEnabled) log.info(msg)
def logDebug(msg: => String, throwable: Throwable) =
if (log.isDebugEnabled) log.debug(msg)
def logTrace(msg: => String, throwable: Throwable) =
if (log.isTraceEnabled) log.trace(msg)
2010-09-29 02:12:23 -04:00
def logWarning(msg: => String, throwable: Throwable) =
if (log.isWarnEnabled) log.warn(msg, throwable)
def logError(msg: => String, throwable: Throwable) =
if (log.isErrorEnabled) log.error(msg, throwable)
// Method for ensuring that logging is initialized, to avoid having multiple
// threads do it concurrently (as SLF4J initialization is not thread safe).
def initLogging() { log }
2010-09-29 02:12:23 -04:00
}