본문 바로가기

개발/java_spring

[Spring Boot] log4j2, hibernate 설정

반응형

gradle 설정

// 기본 로거와 충돌방지위해 exclude 설정
configurations {
	all {
		exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
	}
}

//dependencies 추가
implementation 'org.springframework.boot:spring-boot-starter-log4j2'

 

resources/log4j2.xml 추가

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" monitorInterval="5">
    <Properties>
        <Property name="logFileName">log</Property>
        <Property name="consoleLayout">[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] [%c{1}:%L] - %m%n</Property>
        <Property name="fileLayout">%d [%t] %-5level %c(%M:%L) - %m%n</Property>
    </Properties>

    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="${consoleLayout}" />
        </Console>
        <RollingFile name="file"
                     fileName="logs/${logFileName}.log"
                     filePattern="logs/${logFileName}.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="${fileLayout}" />
            <Policies>
            	<SizeBasedTriggeringPolicy size="20 MB"/>
                <TimeBasedTriggeringPolicy interval="1" /> <!-- 파일패턴의 날짜 포매시 초단위로 설정되어 있을 경우 1초에 한번씩 trigger -->
            </Policies>
            <DefaultRolloverStrategy max="5" fileIndex="max" >
                <Delete basePath="/logs" maxDepth="3">
                    <IfLastModified age="10d" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
    </Appenders>

    <!--TRACE > DEBUG > INFO > WARN > ERROR > FATAL -->
    <Loggers>
        <logger name="org.springframework" level="INFO" additivity="false" >
            <AppenderRef ref="console" />
        </logger>

		<!-- sql binding variables logging -->
        <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" additivity="false">
            <AppenderRef ref="console" />
        </logger>

		<!-- sql logging -->
        <logger name="org.hibernate.SQL" level="DEBUG" additivity="false" >
            <AppenderRef ref="console" />
        </logger>

		<!-- ROOT logger-->
        <Root level="ERROR">
            <AppenderRef ref="console" />
            <AppenderRef ref="file" />
        </Root>
    </Loggers>
</Configuration>

application.properties 수정 (hibernate 설정)

spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.highlight_sql=true

logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type=trace

hibernate logging example

sql :

sql

format이 정돈되고 highlight로 보기 편하게 출력된다.

 

binding data :

binding data 및 select data도 출력된다.

 

※ 참고

 

Hibernate Tips: How to log SQL statements and their param

The Hibernate Tips series provides quick answers to common questions. This week, I show you how to configure Hibernate to log SQL statements and parameters.

thorben-janssen.com

 

 

Log4j – Configuring Log4j 2

Configuration Inserting log requests into the application code requires a fair amount of planning and effort. Observation shows that approximately 4 percent of code is dedicated to logging. Consequently, even moderately sized applications will have thousan

logging.apache.org

 

반응형