[JAVA] Rotate by file size of logback

Premise

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.

logback-spring.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE logback>
<configuration>
    <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-->
                <minIndex>1</minIndex>
                <maxIndex>20</maxIndex>
            </rollingPolicy>
            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <!--Rotate according to file size-->
                <maxFileSize>20MB</maxFileSize>
            </triggeringPolicy>
            < encoder>
                <!--Log output format-->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%thread]  [%file:%line %method] %msg%n</pattern>
            </encoder>
        </appender>
    </springProfile>

    <!--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" />
・ ・ ・
    </springProfile>

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

application.properties (excerpt)

application.properties


#Local environment settings
spring.profiles.active=local

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.

LogTestController.java


package jp.adg.fuji.log.controllers;

import lombok.extern.slf4j.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);
        }
        return;
    }
}

Failure

-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

Other

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