Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new Spring log file every day

I want to configure Spring to write the log messages into file:

logging.file=/my-logs/app.log
logging.path=/my-logs/spring.log

Is it possible to rotate the file every day? I want to create a new file every day.

like image 822
Peter Penzov Avatar asked Oct 29 '25 22:10

Peter Penzov


1 Answers

From the doc:

Log files rotate when they reach 10 MB and, as with console output, ERROR-level, WARN-level, and INFO-level messages are logged by default. Size limits can be changed using the logging.file.max-size property. Previously rotated files are archived indefinitely unless the logging.file.max-history property has been set.

Also, if you just want to log to "/my-logs/app.log", delete logging.path and change logging.file to:

logging.file=/my-logs/app.log

Edit: about rotating the log every day, Spring default logger does not support it, you can use Logback. Create a file called logback-spring.xml in src/main/resources with the following content:

<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}[%M:%L] - %msg%n</pattern>
    </encoder>
</appender>

<appender name="ROTATE_FILE_DAILY" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>/my-logs/app.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>app-%d{yyyy-MM-dd}.log</fileNamePattern>
    </rollingPolicy>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}[%M:%L] - %msg%n</pattern>
    </encoder>
</appender>

<root level="INFO">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="ROTATE_FILE_DAILY"/>
</root></configuration>
like image 170
Marc Avatar answered Oct 31 '25 21:10

Marc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!