How to create new log file each time application runs?
I would like to keep previous logs in any way. For example, I would prefer to name each new log file by time and date it was created. Otherwise, I agree to backup old log file into date and time filename.
Unfortunately, I can't see appropriate policies and/or triggers here: http://logback.qos.ch/manual/appenders.html
UPDATE
I made approximately as said in "duplicate"
<appender name="ROUTINEAPPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/routine.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/routine%d{yyyyMMdd}%d{HHmmss,aux}.log</fileNamePattern>
<TimeBasedFileNamingAndTriggeringPolicy class="com.inthemoon.toolkit.StartupTimeBasedTriggeringPolicy" />
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} - %C{0} - %msg%n</pattern>
</encoder>
</appender>
but my class com.inthemoon.toolkit.StartupTimeBasedTriggeringPolicy
is never called. I put breakpoint in start()
method but it never raised.
Also, no rolling occurs. Log file is created, but it always has the name routine.log
.
Also I don't understand, how parameters file
and filenamePattern
should coexist.
UPDATE 2
I have fixed UPDATE 1
class reference, but still don't have what I need. In given solution, the date time is inserted into OLD log filename. For example, if I ran program in 2013, it created routine.log
. Then I waited a year and run program in 2014. New log file will be created and have name routine.log
, while OLD 2013 log will be put into routine2014XXXXXXXXXX.log
, which is absolutely irrelevant.
I need create new file each time program starts and have this namely file marked with date-time stamp.
RollingFileAppender extends FileAppender with the capability to rollover log files. For example, RollingFileAppender can log to a file named log. txt file and, once a certain condition is met, change its logging target to another file.
If you want to continue using RollingFileAppender
you can create your own TriggeringPolicy
which will always trigger a new log file to be generated upon it's first call.
@NoAutoStart
public class StartupTriggeringPolicy<E> extends TriggeringPolicyBase<E> {
private boolean triggerRollover = true;
@Override
public boolean isTriggeringEvent(final File activeFile, final E event) {
if (!triggerRollover) { return false; }
triggerRollover = false;
return true;
}
}
You can then set the triggeringPolicy
property within you logback.xml
's <appender>
configuration:
<appender name="startupRolloverAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<triggeringPolicy class="com.your.package.StartupTriggeringPolicy"/>
<!-- other configurations -->
</appender>
The following configuration will create a new logfile 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