[JAVA] Rotate by file size of logback


Logback is used for Java (Spring Boot) log output.

"I want you to rotate the log when it reaches 20MB and keep 20 files for the past." I was told, but I didn't know how to do it at all, so I looked it up.

logback-spring.xml I put this under src / main / resource and rotated it.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE logback>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <!--Settings for each environment-->
    <springProfile name="local">
        <!--Specify the log directory. what if/tmp -->
        <property name="LOG_DIR" value="/tmp" />
        <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!--Log filename and path-->
            <file> ${LOG_DIR}/adg.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <!--File name after rotation.".zip"If you put it on, it will be compressed without permission-->
                <fileNamePattern> ${LOG_DIR}/adg_%i.log</fileNamePattern>
                <!--Maximum number after the file/minimum. Number to hold-->
            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <!--Rotate according to file size-->
            < encoder>
                <!--Log output format-->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%thread]  [%file:%line %method] %msg%n</pattern>

    <!--Settings for each environment(I tried to write it like this) -->
    <springProfile name="prod">
        <!--Specify the log directory(I changed it in the environment) -->
        <property name="LOG_DIR" value="/var/log" />
・ ・ ・

    <!--Log level-->
    <root level="INFO">
        <appender-ref ref="FILE" />

application.properties (excerpt)


#Local environment settings

Log output

By the way, like me, how do you output the log in the first place? Some people say that, so it's a memo.


package jp.adg.fuji.log.controllers;

import lombok.extern.slf4j.Slf4j;

public class LogTestController {
    public void logLog() {
        try {
            //This alone will give you a log
            log.info("info log");
            //Describe the process
        } catch (Exception e) {
            // log.warn()There is also
            log.error("error log", e);


-First created with the file name logback.xml → An error occurs when using springProfile → Change the file name to logback-spring.xml

-Although I used the layout tag, it seems to be deprecated, so I modified it to encoder https://logback.qos.ch/codes.html#layoutInsteadOfEncoder

-An error occurred because the log output destination was also described in application.properties. → Deleted the description on the application.properties side

・ Copy and paste without thinking org/springframework/boot/logging/logback/base.xml When I included, the app log was also (unintentionally) in catalina.out


You should have this too, It is better to set each environment like this, I want you to tell me if there is such a setting!

Recommended Posts

Rotate by file size of logback
Utilization of seed file
Check file size before deploying
About size comparison of compareTo
List of hosts file locations
Put the date (e.g. yyyy-MM-dd) in the log file output by logback