6

My sbt is setup based on this great answer: Confused how to setup a multi project sbt project

So in the Dependencies.scala have added:

val scalaLogging = "com.typesafe.scala-logging" %% "scala-logging-slf4j" % scalaLoggingVersion
//val logbackCore = "ch.qos.logback" % "logback-core" % logbackVersion
val logbackClassic = "ch.qos.logback" % "logback-classic" % logbackVersion

(Have commented out logbackCore - Do I even need that?)

Have added scalaLogging and logbackClassic to commonDependencies (other libs removed):

val commonDependencies: Seq[ModuleID] = Seq(
    scalaLogging,
    //slf4j,
    //logbackCore,
    logbackClassic
)

Also in the /services sbt module , added this to a UserServiceImpl class:

import com.typesafe.scalalogging.slf4j.LazyLogging
class UserServiceImpl (.....) extends SecurityService with LazyLogging {

    def show(.....) {
        logger.trace("UserService.show called")
        logger.debug("UserService.show called")
    }
}

Also, in /services/resources , added logback.xml:

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <encoder>
            <pattern>%logger:%line - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

Now when my play app is launched , log is not seen when sbt run, regular play logs are just fine that I put in my controllers etc.

For good measure , have also dropped a logback.xml in the /conf folder, but still no output.

I'm not sure what I am doing wrong here, hoping someone can clarify things for me.

Note: I am using sbt to compile my project, but I make edits in IntelliJ and for some reason IntelliJ doesn't not pickup scala-logging correctly. My import is highlighted in red, my LazyLogging trait keyword that I am using on my UserService 'with LazyLogging' is in red. Also my actual log statements like logger.debug the word 'logger' is red. It doesnt' seem to resolve these, yet sbt compiles it fine.

Community
  • 1
  • 1
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • Blankman have you tried putting logback.xml into src/main/resources ? Related question: http://stackoverflow.com/questions/14149798/akka-slf4j-logback-configuration-and-usage/20571679#20571679 – reggoodwin May 26 '14 at 06:20
  • No, I don't have a src/main/resources. I did try /services/src/main/resources and /play-web/conf/ – Blankman May 26 '14 at 13:42
  • What values are you using for logbackVersion and scalaLoggingVersion? – drstevens May 28 '14 at 17:16
  • @drstevens logback is 1.1.2 and scalaLogging is 2.1.2 – Blankman May 28 '14 at 17:43
  • Just for kicks, what happens if you explicitly add `commonDependencies` to dependencies of all sub-projects in question opposed to inheriting them through `dependsOn`? Make sure to always be regenerating intellij project from sbt. Additionally, I've noticed that project does not get generated correctly if code does not build via sbt. – drstevens May 28 '14 at 19:12
  • @drstevens I use sbt for my builds and running the play app, I just use intellij for coding. – Blankman May 28 '14 at 19:51
  • right, what happens if you try and build in sbt after making the change I suggested? Sorry if I wasn't clear. – drstevens May 28 '14 at 20:49
  • @drstevens I added scalaLogging and logbackClass explicitly to my commonDependencies, serviceDependencies and webDependencies and I still see no trace/debug messages. – Blankman May 29 '14 at 18:18
  • 1
    @Blankman Have you tried editing `application.conf` and commenting out all logger settings: `logger.rot`, `logger.play`, `logger.application`, and then placing **`application-logger.xml`** (the name is important) in your `/conf` directory (or any directory that is shown when you execute `show resourceDirectories`? IMHO when you think of it, you'd like to configure it globally anyway for the whole application, and not module by module. – lpiepiora May 31 '14 at 21:54
  • @lpiepiora ok that worked! I now have the same logging as before. if you want, you can move this to an answer so you can get credit. – Blankman Jun 24 '14 at 21:11
  • @Blankman I think you should be using a name derived from your package name and class name. LazyLogging defines a logger as `Logger(LoggerFactory.getLogger(getClass.getName))`, so I guess your logger's name should be the package or the full qualified class name. If you don't use package then the class name, e.g. `UserServiceImpl` – lpiepiora Jun 24 '14 at 21:18
  • @lpiepiora thanks I figured that out. Please put your answer in a proper answer so I can give you credit. – Blankman Jun 24 '14 at 21:19
  • @Blankman thanks! I've done it. – lpiepiora Jun 24 '14 at 21:26

1 Answers1

2

To make the logback working, you have to edit the application.conf and comment out all logger settings: logger.root, logger.play, logger.application, etc.

Then place a logback configuration in a file called application-logger.xml (the name is important) in your /conf directory.

lpiepiora
  • 13,659
  • 1
  • 35
  • 47