Log of dependency does not show

164 views Asked by At

I have a Play project, named FrontEnd, whose dependency is another project named Logic.

Inside the build.sbt file of the Logic project, I add slf4j and logback:

name := "Logic"

version := "1.0-SNAPSHOT"

scalaVersion := "2.11.1"

seq(bintrayResolverSettings:_*)

libraryDependencies += "org.slf4j" % "slf4j-api" % "1.7.5"

libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.2"

I also created a Logging trait inside the Logic project

package logic.logging

import ch.qos.logback.classic.Logger
import org.slf4j.LoggerFactory

trait Logging {
  protected val logger = LoggerFactory.getLogger(this.getClass().getSimpleName().stripSuffix("$"))
}

Then, I included the Logic project inside the FrontEnd project:

name := "Frontend"

version := "1.0"

scalaVersion := "2.11.1"

lazy val logicProj = RootProject(file("../logic"))

lazy val root = (project in file(".")).enablePlugins(PlayScala).dependsOn(logicProj)

Finally, in order to test the logging feature, I created a Global object inside FrontEnd

import play.api.GlobalSettings
import play.api.Application
import play.api.Logger
import logic.logging.Logging

object Global extends GlobalSettings with Logging {
  override def onStart(app: Application) = {
    logger.info("Logic's logger: Application is started!!!")
    Logger.info("FrontEnd's logger: Application is started!!!")
  }
}

And the output on console is:

[info] application - FrontEnd's logger: Application is started!!!
[info] play - Application started (Dev)

In other words, the logger from logic.logging.Logging cannot log to the console. I wonder if anyone knows why and how to fix it.

PS: according to this, it is possible that it has to do with the multiple bindings, i.e., the logback in Logic and in FrontEnd's Play library. However, I don't want to remove logback from Logic as the project itself is a standalone console application which needs to log the output to the console.

Edit: Even after removing libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.2" from Logic's build.sbt, the problem is still there.

1

There are 1 answers

0
Long Thai On

Finally, I find out that the log does not show as it is not "registered" to Play's logging configuration. It can be fixed by adding the following line into configuration tag of application-logger.xml file:

<logger name="logger" level="INFO" />

Then, I update the Logging object to include a logger prefix at all logger:

package logic.logging

import ch.qos.logback.classic.Logger
import org.slf4j.LoggerFactory

trait Logging {
  protected val logger = LoggerFactory.getLogger("logger.%s".format(this.getClass().getSimpleName().stripSuffix("$")))
}

And it works as the output is:

[info] logger.Global - Logic's logger: Application is started!!!
[info] application - FrontEnd's logger: Application is started!!!