How do I configure Logback to print out the class name

后端 未结 6 1037
渐次进展
渐次进展 2020-12-10 11:08

I\'m using Play 2.1. I\'m using the default logger play.api.Logger. I\'m confused about how it works.

In my scala code, a line in class \"com.myapp.tickets\" in th

6条回答
  •  粉色の甜心
    2020-12-10 11:54

    I'm in the process of abandoning the single application.log approach that Play seems to default to with its Logger. My application requires the kind-of fine grained logging and runtime adjustment of it that straight-up logback does so well when classname == Logger name. I've had pretty good success going just "old school" in my controllers like...

    package controllers
    import play.api._
    import play.api.mvc._
    import org.slf4j.LoggerFactory
    
    object Application extends Controller {
      val log = LoggerFactory.getLogger(getClass())
    
      def index = Action {
        log.trace("index")
        NotFound
      }
    
      def hb = Action {
        log.trace("hb")
        val message = makeMessage()
        log.info(message)
        Ok(message)
      }
    
      def makeMessage(): String = {
        val version = "@buildsig.version@"
        val tag = "@buildsig.tag@"
        val timestamp = "@buildsig.timestamp@"
        val status = makeStatus()
        return "DM2 [Version: %s] [Build: %s] [Date: %s] [Status: %s]".format(version, tag, timestamp, status)
      }
    
      def makeStatus(): String = {
        // TODO: Implement datastore healthcheck
        return "TODO"
      }
    }
    

    For any developer used to slf4j/logback or log4j, this approach will seem familiar. On the other hand, I am currently struggling through start shell script from "play dist" fails to locate logger.xml in JAR file where by the start script is failing to use my conf/logger.xml that gets JARed up by the "play dist" command.

    If I was just a little bit better of a Scala developer, I think the same effect can be achieved with something like a Logging trait.

提交回复
热议问题