공부/프로그래밍

[SpringBoot] 간단 Slf4j 로그설정

demonic_ 2018. 12. 9. 18:02

1. gradle 추가


compile group: 'org.bgee.log4jdbc-log4j2', name: 'log4jdbc-log4j2-jdbc4', version: '1.16'


2. application.yml 추가 (또는 application.properties)


spring:

  profiles:

    active: local


[...]


# logging 설정

logging:

  file: ./log/local


3. 파일추가


resources/log/console.xml

resources/log/file.xml

resources/log4jdbc.log4j2.properties

resources/logback-spring.xml


- log4jdbc.log4j2.properties 파일


log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator

# 최대 몇개라인까지 출력할지 결졍하는 것. 0일 경우 제한없이 실행된 그대로 출력

log4jdbc.dump.sql.maxlinelength=0


- log/console.xml 파일


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

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

    <encoder>

        <charset>UTF-8</charset>

        <pattern>%d{yyyyMMdd HH:mm:ss.SSS} [%thread] %-3level %logger{5} : %msg %n</pattern>

    </encoder>

</appender>


- log/file.xml 파일


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

<appender name="dailyRollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

        <fileNamePattern>${LOG_FILE}/tomcat_log.%d{yyyy-MM-dd}-%i.log</fileNamePattern>

        <maxHistory>30</maxHistory>

        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

            <maxFileSize>100MB</maxFileSize>

        </timeBasedFileNamingAndTriggeringPolicy>

    </rollingPolicy>

    <encoder>

        <charset>UTF-8</charset>

        <pattern>%d{yyyy:MM:dd HH:mm:ss.SSS} %-5level --- [%thread] %logger{35} : %msg %n</pattern>

    </encoder>

</appender>


- logback-spring.xml 파일


이 파일은 필요에 따라 수정하시면 됩니다.


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

<configuration>

    <include resource="log/console.xml" />

    <include resource="log/file.xml" />


    <root level="info">

        <springProfile name="local">

            <appender-ref ref="STDOUT"/>

            <appender-ref ref="dailyRollingFileAppender"/>

        </springProfile>

        <springProfile name="dev">

        </springProfile>

        <springProfile name="prod">

            <appender-ref ref="dailyRollingFileAppender"/>

        </springProfile>

        <springProfile name="test">

            <appender-ref ref="STDOUT"/>

        </springProfile>

    </root>

</configuration>



끝.