Search code examples
scalaloggingsbtlogbackslf4j

scala project is not taking logback xml configuration when passed via JVM argument


I have a scala project where i am using scala-slf4j logging and logback. When the project is executed via command line as below

java -jar <sample_project.jar> -Dlogback.configurationFile=/tmp/logback.xml

It uses logger class as 'ch.qos.logback.classic.Logger' but is not taking the logback.xml configuration.

Same works fine if i put the logback configuration xml under /src/main/resources.

import org.slf4j.LoggerFactory

object SampleProj {
  val logger = LoggerFactory.getLogger(this.getClass)
  logger.info("SCALA Logging")
}

build.sbt

"ch.qos.logback" % "logback-classic" % "1.1.2",
"com.typesafe.scala-logging" %% "scala-logging-slf4j" % "2.1.2",

Another approach which is tried is extracted the scala project jar and executed the main class as below

java com.test.SampleProj -Dlogback.configurationFile=/tmp/logback.xml

Some other methods i tried was,

  1. Set the classpath and put the logback xml
  2. passing as "-Dlogger.file=/tmp/logger.xml"
  3. passing as -Dlogger.resource=/tmp/logger.xml

Can someone help me in resolving this. Thanks


Solution

  • Arguments to the java should be as below.

    java -Dlogback.configurationFile=/tmp/logback.xml com.test.SampleProj