Wednesday, May 7, 2014

Overriding logback file embedded in jar file

I was trying to run a jar file by setting it up in cronjob. Now this jar file contains a logback configuration file included in it which is causing me problem because all logs are being written to a specific file name, like processnamelogs.log. All runs of jar file will append to the same log file.

Now this is not what I wanted since the log file is going to grow huge in few runs. Also if all runs of jar file are writing to same log file it will be difficult to setup a monitoring script to check for errors.

What I wanted is that each run of jar file writes to a separate log file with its name containing start time of that run. So it will be easy to go through logs in case of errors. Also I can easily setup a monitoring script to only search in the log files with current date in their names.

So I need to write my own logback configuration file and a way to make sure the jar file uses my configuration file instead of embedded one. I have searched and got one system property (logback.configurationFile) which does what I wanted.

  java  -Xms128m -Xmx512m -XX:MaxPermSize=256m -jar -Dlogback.configurationFile=./mylogback.xml ./process.jar



  <!-- Insert the current time formatted as "yyyyMMdd'T'HHmmss" under
       the key "bySecond" into the logger context. This value will be
       available to all subsequent configuration elements. -->
  <timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss"/>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <!-- use the previously created timestamp to create a uniquely
         named log file -->
      <pattern>%logger{35} - %msg%n</pattern>

  <root level="DEBUG">
    <appender-ref ref="FILE" />


No comments:

Post a Comment

Latest Posts