hminle
hminle

Reputation: 521

How to configure logback.xml for JAR application?

I am writing Scala application using logback for logging. I want to configure logback so that when I run my application JAR file I can get a log file. This is my current configuration, which cannot produce any log file

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <!-- path to your log file, where you want to store logs -->
        <file>./log/test.log</file>
        <append>false</append>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

Here is my build sbt:

name := "distributed-sorting"

version := "1.0"

scalaVersion := "2.11.8"

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.0" % "test"

libraryDependencies ++= Seq(
  "junit" % "junit" % "4.8.1" % "test"
)
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.7"
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.5.0"
libraryDependencies += "com.lihaoyi" %% "upickle" % "0.4.3"

Thank you

Upvotes: 3

Views: 9011

Answers (1)

binshi
binshi

Reputation: 1328

Check if the log configuration file is in main/src/resources and named logback.xml This is the default path.

If the logback.xml is placed elsewhere, give the path in build.sbt file as:

resourceDirectory in Compile := baseDirectory.value / "yourFilePath"

When you run sbt run, check if the xx.xml file present in target/scala.XX/classes folder.

Use sbt assembly plugin or package to build your jar and execute

java -Dlogback.configurationFile=xx.xml -Dfile.ending=UTF8  -cp $SCALA_HOME/lib/scala-library.jar  -jar myProject.jar -Dconfig.file=application.conf

Upvotes: 3

Related Questions