How to properly manage logback configrations in development and production using SBT & Scala?
Asked Answered
B

1

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.

Brundage answered 18/12, 2014 at 3:18 Comment(0)
D
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://mcmap.net/q/1915532/-how-to-set-system-property-for-xsbt-web-plugin-39-s-jetty

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

Drypoint answered 18/12, 2014 at 4:17 Comment(6)
Seems like this requires me to be using JVM forking? Is that considered standard practice?Brundage
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.Drypoint
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?Brundage
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 - #4755734, but I don't seem to be able to fix it in sbt 13.5. I just don't understand SBT enough.Brundage
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.Drypoint
I did run clean. IntelliJ works fine and picks up the right file. I actually made another question with a solid example - #27556177Brundage

© 2022 - 2024 — McMap. All rights reserved.