1
votes

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

2
Have you tried setting your file encoding? scala-sbt.org/0.13/docs/Setup-Notes.html Also, are you getting any warnings/errors?Nathaniel Ford
Thanks for the reply.@NathanielFord. It working now.PGS
Did that help? Or are you still stuck? @GopiNathaniel Ford
VM argument needs to be passed like this. java com.test.SampleProj -Dlogback.configurationFile=/tmp/logback.xmlPGS

2 Answers

3
votes

Arguments to the java should be as below.

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

In my case, the solutions above were not enough. My problem was that the logback.xml file was not read by default.

I believe that the solution provided at http://www.scala-sbt.org/0.13/docs/Setup-Notes.html, by specifying the file encoding works, it is better to fix that from the shell.

In my shell, I run the following:

export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales

After that, the logback.xml was read properly.