I've got a small java program that will run as a jar file on end users' desktops. This small program has one specific task to accomplish, and it is to run as a simple java program (not swing or anything like that).
I'm wanting to use logback for the purpose of capturing the output of this program, and I want the log file to roll each and every time that the program is run. In other words, the first time the program runs, I want a log file generated that is named c:\temp\logFile.log. The second time the program runs, I want the first log file renamed to logFile.1.log and a new logFile.log created. The third time the program runs, I want the first log file renamed to logFile.2.log, the second log file renamed to logFile.1.log, and a new logFile.log created. I want there to be a maximum of 5 archived files.
So far, I've been able to configure logback to roll the log file based on file size. I've gotten it to rename the log files exactly how I want and to keep the exact number of archives. The problem is that I don't want the log file to roll on file size, I want it to roll each and every time the program runs. How do I go about doing this? Here's what I've got in my logback.xml file so far:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>C:\temp\logFile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>C:\temp\logFile.%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>5</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5KB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="fileAppender" />
</root>
</configuration>
I know that the maxFileSize triggering policy is what's causing my log files to roll based on file size. I have not been able to find a triggering policy that rolls the file based on the program running. Can anyone help me out?
Thanks!
-Stephen Spalding
The following configuration will create a new logfile (the file name will have a datestamp on it) and console output every time the program is run, please note that it does not use RollingFileAppender. For more information please refer to logback documentation https://logback.qos.ch/manual/configuration.html
<configuration>
<timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logfile-${bySecond}.txt</file>
<append>true</append>
<encoder>
<Pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
</appender>
<root level="info" additivity="false">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"></appender-ref>
</root>
</configuration>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With