Create a new log file, each run in a log?

How do I create a new log file every time I start the application?

I would like to keep the previous logs. For example, I would prefer to name each new log file by its creation time and date. Otherwise, I agree to archive the old log file at the date and time of the file name.

Unfortunately I don't see any relevant policies and / or triggers here: http://logback.qos.ch/manual/appenders.html

UPDATE

I did something like "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 is com.inthemoon.toolkit.StartupTimeBasedTriggeringPolicy

never called. I put a breakpoint in the method start()

, but it never went up.

There is also no swing. The log file is created but always has a name routine.log

.

Also I don't understand how the file

and parameters coexist filenamePattern

.

UPDATE 2

I have the class reference fixed UPDATE 1

, but I don't have what I need yet. In this solution, the date time is inserted in the name of the log OLD file. For example, if I ran the program in 2013, it created routine.log

. Then I waited a year and launched the program in 2014. A new log file will be created and will have a name routine.log

, and the OLD 2013 log will be placed in routine2014XXXXXXXXXX.log

, which is absolutely irrelevant.

I need to create a new file every time I run the program and have this file tagged with a date and time stamp.

+3


source to share


1 answer


The following configuration will create a new log file and console output every time you run the program, please note that it does not use RollingFileAppender. For more information see the protocol 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>

      

+3


source







All Articles