6

I have a pretty standard Scalatra project using Logback for logging.

Following the logback manual I have added a logback-test.xml for my development configuration (logs of debugs), whilst maintaining a production logback.xml.

However, in development while using the xsbt-web-plugin to run a container with code reloading, my app only seems to pick up the logback.xml.

How do I get the desired behavior?:

  1. In development mode (./sbt container:start) the app uses logback-test.xml
  2. When assembled into a zip using SBT-assembly, exclude the test config.

Currently neither of these seems to work.

Matthew Rathbone
  • 8,144
  • 7
  • 49
  • 79

1 Answers1

3

You're misusing logback-test.xml. It's intended for unit-like tests only and should be placed in src/test/resources (which is automatically excluded from prod). To achieve what you want - you may set up path to your logback-dev.xml by system property:

 javaOptions in container += "-Dlogback.configurationFile=/some/path/logback-dev.xml"

This path may be relative. See, https://stackoverflow.com/a/26538449/1809978

In my practice we don't pack logback.xml even in prod (it's pointed to some external place) to have ability to change logging configuration ad-hoc.

P.S. If you're also interested about excluding files from sbt-assembly - this may help

dk14
  • 22,206
  • 4
  • 51
  • 88
  • 1
    Seems like this requires me to be using JVM forking? Is that considered standard practice? – Matthew Rathbone Dec 18 '14 at 16:16
  • jvm forking is usually fine for web-plugin. if it's a problem - you can just add `-Dlogback...` to your sbt launcher script or just `System.setProperty(...)` inside your build.sbt/Project.scala. The standard practice (for logback) is moving your logback.xml out - the concrete solution depends on your needs. – dk14 Dec 18 '14 at 17:46
  • Ok, so I started rearranging things. I moved logback-test.xml to `src/test/resources`. There still seem to be problems, as it isn't picked up when running unit tests. It still seems to be reading from `src/main/resources/logback.xml`. Any ideas? – Matthew Rathbone Dec 18 '14 at 19:48
  • So it seems like `sbt test` doesn't put `src/test/resources` on the classpath when it runs tests. This seems to have come up before - http://stackoverflow.com/questions/4755234/how-can-i-change-the-directory-into-which-sbt-puts-test-resources, but I don't seem to be able to fix it in sbt 13.5. I just don't understand SBT enough. – Matthew Rathbone Dec 18 '14 at 20:48
  • not sure that it's connected - did you try `sbt test:clean`? How do you run the tests - from Idea or Sbt? Sbt uses `target/scala_XX/xxxxx-test.jar` for tests (is there logback-test inside? Idea uses `target/scala_XX/test-classes` folder - you may try to clean both because sometimes sbt just forgetting to add a file. – dk14 Dec 19 '14 at 01:42
  • 1
    I did run clean. IntelliJ works fine and picks up the right file. I actually made another question with a solid example - http://stackoverflow.com/questions/27555677/why-is-logback-loading-configurations-in-a-different-order-and-ignoring-system-p – Matthew Rathbone Dec 19 '14 at 20:19