Search code examples
javaeclipsejarlogback

My Jar not creating a log file whereas normal application does


I developed an application which is using logback with slf4j. Here is my logback.xml :

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
   <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
    </encoder>
  </appender>
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>/tmp/alerttest.log</file>
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
    </encoder>
  </appender>


  <!-- Strictly speaking, the level attribute is not necessary since -->
  <!-- the level of the root level is set to DEBUG by default.       -->
  <root level="DEBUG">          
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />

  </root>  

</configuration>

So it is writing logs to /tmp/alerttest.log . When I run it in eclipse, it runs and creates a log file and write logs to it. But Now I created an executable Jar file of the project named alerttesting.jar. When I run it using java -jar alerttesting.jar , it runs successfully but doesn't create the file in /tmp/. Is there something I am doing wrong?


Solution

  • logback.xml should be in your classpath or use "-Dlogback.configurationFile=/path/to/config.xml"

    Other than that, make sure that you are packaging it via maven and not eclipse.