프로그래밍/Java

스프링부트(spring boot) mysql 연동시 log4j2 설정 gradle intellij

소행성왕자 2021. 11. 17. 18:10
스프링 부트 사용시 기본 로그 spring-boot-starter-logging 제거해야 합니다.
 
all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }

build.gradle 에 log4j2 와 쿼리로그를 볼수 있도록 추가하겠습니다.

build.gradle log4j2 추가
implementation 'org.springframework.boot:spring-boot-starter-log4j2'

build.gradle 쿼리로그 추가 
implementation 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16'

build.gradle 전체

plugins {
    id 'org.springframework.boot' version '2.5.6'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'war'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-web-services'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'com.h2database:h2'
    runtimeOnly 'mysql:mysql-connector-java'
    annotationProcessor 'org.projectlombok:lombok'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
    implementation 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16'

}

test {
    useJUnitPlatform()
}

resources 하위 2개 파일 생성

  • log4j2.xml
  • log4jdbc.log4j2.properties

log4j2.xml 전체

<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="DEBUG">
    <Properties>
        <Property name="LOG_PATTERN">%d{HH:mm:ss.SSSZ} [%t] %-5level %logger{36} - %msg%n</Property>
    </Properties>
    <Appenders>
        <Console name="ConsoleLog" target="SYSTEM_OUT">
            <PatternLayout pattern="${LOG_PATTERN}" charset="UTF-8"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="ConsoleLog" />
            <AppenderRef ref="FileLog" />
        </Root>
    </Loggers>
</Configuration>

log4jdbc.log4j2.properties 전체

log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
log4jdbc.dump.sql.maxlinelength=0

application.properties 전체

spring.thymeleaf.cache=false

spring.datasource.url=jdbc:log4jdbc:mysql://1.1.1.1:3306/DBID?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Seoul
spring.datasource.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
spring.datasource.username=DBID
spring.datasource.password=DBPW

# MyBatis
mybatis.mapper-locations=classpath:mapper/**/*.xml

# model 프로퍼티 camel case 설정
mybatis.configuration.map-underscore-to-camel-case=true

# xml파일 result type에 패키지명을 생략할 수 있도록 alias 설정
mybatis.type-aliases-package=com.example.demo.domain

server.servlet.jsp.init-parameters.development=true

# log4j2
logging.config=classpath:log4j2.xml