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.
e.g.,
java -Xms128m -Xmx512m -XX:MaxPermSize=256m -jar -Dlogback.configurationFile=./mylogback.xml ./process.jar
mylogback.xml
<configuration> <!-- 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 --> <file>log-${bySecond}.txt</file> <encoder> <pattern>%logger{35} - %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="FILE" /> </root> </configuration>
References:
- http://logback.qos.ch/manual/configuration.html#configFileProperty
- http://logback.qos.ch/manual/appenders.html#uniquelyNamed
No comments:
Post a Comment