Home > Net >  logback: How to create a file for each execution while also checking the size and maximum number of
logback: How to create a file for each execution while also checking the size and maximum number of

Time:10-08

I have a spring boot application and I would like to have logback create a log file, with the current date (including minutes hours and seconds). So there will be a file for each time the application is run.

To do this I did this:

<springProperty name="log_path_folder" source="log.path.folder"/>
<property name="application_filename" value="application"/>
<timestamp key="currentDate" datePattern="yyyy-MM-dd'_'HH.mm.ss"/>

<appender name="APPLICATION_FILE" >
    <file>${log_path_folder}/${application_filename}-${currentDate}.log</file>
    <encoder>
        <pattern>%d [%thread] %-5level [%marker] %msg%n</pattern>
    </encoder>
</appender>


<root level="info" additivity="false">
    <appender-ref ref="APPLICATION_FILE"/>
</root>

This works correctly. The problem is that I don't know how to add checks on a maximum number of files or on the maximum size of the folder with all the logs. I know it is possible to use the RollingFileAppender. The problem is that if I put the seconds in the filename, it creates a file for every minute. So I won't have all the logs of a run in one file.

CodePudding user response:

If you mean to keep only specific numbers of log files inside your logging folder, there is a parameter you can set: MaxBackupIndex

By setting this, say we set to 2.
Your log files under your log folder will only have at most three logs, which are:

  1. logging.log4j
  2. logging.log4j.1
  3. logging.log4j.2

You can check out to Article from baeldung or Apache logging for more details.

CodePudding user response:

I don't think it's possible with onboard resources, but there is a discussion on stackoverflow about custom implementations.

The RollingFileAppender rotates under certain circumstances, but not on application start or something you would need. The FixedWindowRollingPolicy uses a triggeringPolicy to determine when to rotate. There is only the SizeBasedTriggeringPolicy to rotate based on the size of a file. But you can write you own policy as discussed on the other thread mentioned above.

At least those are my findings after looking into the manual of logback.


About your problem with rotating on application start found this in the manual. It works with other appenders as well, but I couldn't figure out how to rotate the way you want it.

<timestamp key="time" datePattern="yyyyMMdd'T'HHmmssSSS"/>

<appender name="FILE" >
    <!-- use the previously created timestamp to create a uniquely
         named log file -->
    <file>logs/log-${time}.txt</file>
    <encoder>
        <pattern>%logger{35} - %msg%n</pattern>
    </encoder>
</appender>

By using the timestamp with seconds, as you already did. I tested it and works on my machine. I added the milliseconds to be more precise.

You can also tell logback to use the application start time as timestamp by setting the timeReference to contextBirth:

<timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss" 
         timeReference="contextBirth"/>
  • Related