[Spring Boot] logback 간단 설정

새로 세팅할 때마다 찾아보는거 귀찮아서

그래서! 정리해봄

resources/logback.properties 파일 추가

log.file.path=C:/logs/

resources/logback.xml 파일 추가

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <!-- properties 파일 지정 -->
	<property resource="logback.properties"/>

    <!--로그 파일 저장 위치-->
    <property name="LOGS_PATH" value="${log.file.path}"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm} %-5level %logger{36} - %msg%n</Pattern>
        </layout>
    </appender>
    
    <!-- 
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>
     -->
    <appender name="DAILY_ROLLING_FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS_PATH}/logback.log</file>
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOGS_PATH}/logback.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- or whenever the file size reaches 100MB -->
                <maxFileSize>5MB</maxFileSize>
                <!-- kb, mb, gb -->
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
    </appender>
    
    <!-- 
    <logger name="com.iut.mes" level="INFO">
        <appender-ref ref="CONSOLE" />
    </logger>
     -->
    <logger name="com.iut.mes" level="INFO">
        <appender-ref ref="DAILY_ROLLING_FILE_APPENDER" />
    </logger>
    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

로그 찍어보기

public static Logger logger = LoggerFactory.getLogger(ClassName.class);
...
...
...
logger.trace("trace -- Hello world.");
logger.debug("debug -- Hello world.");
logger.info("info -- Hello world.");
logger.warn("warn -- Hello world.");
logger.error("error -- Hello world.");		

파일도 제대로 생성 ㄳ

좋은 웹페이지 즐겨찾기