scala 自我意识特征

发布于 2024-11-05 09:22:44 字数 1718 浏览 1 评论 0原文

我制作了一个 Logging 特征,它封装了日志记录实现的细节,它也很好而且很懒惰,所以非常有效,特别是当特定日志级别不活动时。

/**
* A SLF4J based logging trait 
*/
trait Log {
import org.slf4j.Logger
import org.slf4j.LoggerFactory

val loggedClazz: Class[_]

lazy val logger: Logger = LoggerFactory.getLogger(loggedClazz.getClass)

def logDebug(codeblock: => String) = {
  if (logger.isDebugEnabled) {
    logger.debug(codeblock)
  }
}

def logError(codeblock: => String) = {
  if (logger.isErrorEnabled) {
    logger.error(codeblock)
  }
}

def logInfo(codeblock: => String) = {
  if (logger.isInfoEnabled) {
    logger.info(codeblock)
  }
}

def logWarn(codeblock: => String) = {
  if (logger.isWarnEnabled) {
    logger.warn(codeblock)
  }
}
}

然而,它需要混合该特征的类来实现以下功能。

object MyServer extends Log {
   val loggedClazz = MyServer.getClass
}

我的问题是,是否有可能以某种方式使该特征知道它已混合到哪个类中?删除需要执行的操作:

   val loggedClazz = MyServer.getClass

解决方案:根据提供的反馈,我按以下方式重写了该类。

/**
 * A SLF4J based logging trait 
 */
trait Log {
  import org.slf4j.Logger
  import org.slf4j.LoggerFactory

  lazy val logger: Logger = LoggerFactory.getLogger(getClass)

  def logDebug(codeblock: => String) = {
    if (logger.isDebugEnabled) {
      logger.debug(codeblock)
    }
  }

  def logError(codeblock: => String) = {
    if (logger.isErrorEnabled) {
      logger.error(codeblock)
    }
  }

  def logInfo(codeblock: => String) = {
    if (logger.isInfoEnabled) {
      logger.info(codeblock)
    }
  }

  def logWarn(codeblock: => String) = {
    if (logger.isWarnEnabled) {
      logger.warn(codeblock)
    }
  }
}

完全简单。当你第一次就做对了;)

I've made a Logging trait which encapsulates the details of a logging implementation, it's also nice and lazy so is efficient especially when a particular log level is not active.

/**
* A SLF4J based logging trait 
*/
trait Log {
import org.slf4j.Logger
import org.slf4j.LoggerFactory

val loggedClazz: Class[_]

lazy val logger: Logger = LoggerFactory.getLogger(loggedClazz.getClass)

def logDebug(codeblock: => String) = {
  if (logger.isDebugEnabled) {
    logger.debug(codeblock)
  }
}

def logError(codeblock: => String) = {
  if (logger.isErrorEnabled) {
    logger.error(codeblock)
  }
}

def logInfo(codeblock: => String) = {
  if (logger.isInfoEnabled) {
    logger.info(codeblock)
  }
}

def logWarn(codeblock: => String) = {
  if (logger.isWarnEnabled) {
    logger.warn(codeblock)
  }
}
}

However it requires the class into which this trait is mixed-in to implement the following..

object MyServer extends Log {
   val loggedClazz = MyServer.getClass
}

My question is, is it possible to somehow enable the Trait to know into which class it has been mixed into? Removing the need to do:

   val loggedClazz = MyServer.getClass

SOLUTION: Following the provided feedback, I rewrote the class in the following manner.

/**
 * A SLF4J based logging trait 
 */
trait Log {
  import org.slf4j.Logger
  import org.slf4j.LoggerFactory

  lazy val logger: Logger = LoggerFactory.getLogger(getClass)

  def logDebug(codeblock: => String) = {
    if (logger.isDebugEnabled) {
      logger.debug(codeblock)
    }
  }

  def logError(codeblock: => String) = {
    if (logger.isErrorEnabled) {
      logger.error(codeblock)
    }
  }

  def logInfo(codeblock: => String) = {
    if (logger.isInfoEnabled) {
      logger.info(codeblock)
    }
  }

  def logWarn(codeblock: => String) = {
    if (logger.isWarnEnabled) {
      logger.warn(codeblock)
    }
  }
}

Totally simple. When you do it right, first time ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住我的手 2024-11-12 09:22:44

您可以将 valloggingClazz: Class[_] 替换为 valloggingClazz = getClass

You could replace val loggedClazz: Class[_] with val loggedClazz = getClass.

原野 2024-11-12 09:22:44

您当前的代码将无法按预期工作,因为返回的记录器将始终针对类 Class[Class[_]],因为您在 上调用 getClass >Class[_] 对象。

请使用以下代码:

lazy val logger: Logger = LoggerFactory.getLogger(getClass)

您可能还想看看SLF4S,SLF4J 的薄包装,与您正在做的非常相似。

Your current code won't work as expected as the returned logger will always be for class Class[Class[_]], as you're calling getClass on a Class[_] object.

Use this instead:

lazy val logger: Logger = LoggerFactory.getLogger(getClass)

You may also want to have a look SLF4S, a thin wrapper around SLF4J, which is very similar to what you're doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文