Logging in Scala

前端 未结 14 2025
南旧
南旧 2020-12-07 06:51

What is a good way to do logging in a Scala application? Something that is consistent with the language philosophy, does not clutter the code, and is low-maintenance and uno

14条回答
  •  一向
    一向 (楼主)
    2020-12-07 07:28

    I use SLF4J + Logback classic and apply it like this:

    trait Logging {
      lazy val logger = LoggerFactory.getLogger(getClass)
    
      implicit def logging2Logger(anything: Logging): Logger = anything.logger
    }
    

    Then you can use it whichever fits your style better:

    class X with Logging {
        logger.debug("foo")
        debug("bar")
    }
    

    but this approach of course uses a logger instance per class instance.

提交回复
热议问题