헤르메스 LIFE

[Log] Log4Jdbc 설정 본문

Spring Framework

[Log] Log4Jdbc 설정

헤르메스의날개 2024. 2. 23. 00:06
728x90

개발환경

Spring boot 기반


build.gradle

	implementation 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16'  /* Log4JDBC */

log4jdbc.log4j2.properties

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

 

application.properties

# Postgresql Driver 설정
#spring.datasource.hikari.driver-class-name=org.postgresql.Driver
#spring.datasource.hikari.jdbc-url=jdbc:postgresql://[URL]:5432/[DB명]

# Oracle Driver 설정
#spring.datasource.hikari.driver-class-name=oracle.jdbc.pool.OracleDataSource
#spring.datasource.hikari.jdbc-url=jdbc:oracle:thin:@localhost:1521:XE

# Oracle Log4jdbc-log4j2 설정
# spring.datasource.hikari.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
#spring.datasource.hikari.jdbc-url=jdbc:log4jdbc:oracle:thin:@localhost:1521:XE

# MySql Driver 설정
# spring.datasource.hikari.driver-class-name=com.mysql.jdbc.Drive
#spring.datasource.hikari.jdbc-url=jdbc:mysql://localhost:3306/[DB명]?serverTimezone=Asia/Seoul

# MySql Log4jdbc-log4j2 설정
# spring.datasource.hikari.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
# # spring.datasource.hikari.connection-test-query=SELECT NOW() FROM dual

spring.datasource.hikari.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
spring.datasource.hikari.jdbc-url=jdbc:log4jdbc:postgresql://localhost:5432/springboot?charSet=UTF-8&prepareThreshold=1
spring.datasource.hikari.username=hermeswing
spring.datasource.hikari.password=pass


# CP Setting
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.maximum-pool-size=50
spring.datasource.hikari.minimum-idle=30
spring.datasource.hikari.idle-timeout=60000

context-datasource.xml

<!-- Oracle -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <!-- 
    <property name="driverClassName" value="oracle.jdbc.pool.OracleDataSource"></property>
    <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:XE"></property>
    --> 
    
    <property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
    <property name="jdbcUrl" value="jdbc:log4jdbc:oracle:thin:@localhost:1521:XE"></property>	
    <!-- Oracle 11 경우>
    <!-- Oracle 19 경우 "jdbc:log4jdbc:oracle:thin:@localhost:1521:orcl" -->
    <property name="username" value="spring"></property>
    <property name="password" value="1234"></property>
</bean>


<!-- MySQL경우 -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <!--
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/SpringDB?serverTimezone=Asia/Seoul"></property>
    -->
    
    <property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
    <property name="jdbcUrl" value="jdbc:log4jdbc:mysql://localhost:3306/SpringDB?serverTimezone=Asia/Seoul"></property>
    <property name="username" value="spring"></property>
    <property name="password" value="1234"></property>
</bean>


<!-- MS SQL경우 -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <!--
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
    <property name="jdbcUrl" value="jdbc:sqlserver://localhost:1433;DatabaseName=SpringDB"></property>
    -->
    
    <property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
    <property name="jdbcUrl" value="jdbc:log4jdbc:sqlserver://localhost:1433;DatabaseName=SpringDB"></property>
    <property name="username" value="spring"></property>
    <property name="password" value="1234"></property>
</bean>


<!-- PostgreSQL 경우 -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <!--
    <property name="driverClassName" value="org.postgresql.Driver"></property>
    <property name="jdbcUrl" value="jdbc:postgresql:localhost:5432/SpringDB?charSet=UTF-8&prepareThreshold=1"></property>
    -->

    <property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
    <property name="jdbcUrl" value="jdbc:log4jdbc:postgresql://localhost:5432/SpringDB?charSet=UTF-8&prepareThreshold=1"></property>
    <property name="username" value="spring"></property>
    <property name="password" value="1234"></property>
</bean>

<!-- Tibero 경우 -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <!--
    <property name="driverClassName" value="com.tmax.tibero.jdbc.TbDriver"></property>
    <property name="jdbcUrl" value="jdbc:tibero:thin:@localhost:1521:SpringDB"></property>
    -->

    <property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
    <property name="jdbcUrl" value="jdbc:log4jdbc:tibero:thin:@localhost:1521:SpringDB"></property>
    <property name="username" value="spring"></property>
    <property name="password" value="1234"></property>
</bean>

 

 


application.yml

##############################################################
# Server port
server:
  servlet:
    context-path: /
    encoding:
      enabled: true
      charset: UTF-8
      force: true
    session:
      timeout: 18000                              # 30분, Default 기본단위 : 초
      
  tomcat:
    uri-encoding: UTF-8                         # Spring Default : UTF-8
##############################################################
# -Dspring.profiles.active=local
spring:
  profiles:
    include: local, dev, datasource

##############################################################
# Spring Message 처리
  messages:
    basename: messages/messages, messages/exception		# 각각 ResourceBundle 규칙을 따르는 쉼표로 구분된 기본 이름 목록
    always-use-message-format: false	# 인수가 없는 메시지도 구문 분석하여 항상 MessageFormat 규칙을 적용할지 여부
    encoding: UTF-8
    fallback-to-system-locale: true		# locale에 해당하는 file이 없을 경우 system locale을 사용 ( default : true )
    use-code-as-default-message: true	# 해당 코드를 찾을 수 없을 경우 Code 를 출력한다. ( default : false )
    cache-duration: 3                 # default는 forever
    #cache-seconds: -1					      # 로드된 자원 번들 파일 캐시 만료(초). -1로 설정하면 번들은 영원히 캐시됩니다.

application-local.yml

# Server port
server:
  port: 9090

##############################################################
# Spring Message 처리
spring:
  config.activate.on-profile: local
  devtools:
    livereload:
      enabled: true
      
# p6spy 를 위한 설정
decorator.datasource.p6spy.enable-logging: true     # false : Disable p6spy
#decorator.datasource.enable: true

application-datasource.yml

##############################################################
# Spring Database 처리
spring:
  datasource:
    # AWS -> Postgresql
    #url: jdbc:postgresql://octopus-database-1.ca56feoc8rlf.ap-northeast-2.rds.amazonaws.com:5432/postgres
    #driver-class-name: org.postgresql.Driver
    #username: octopusAdmin
    #password: octopusDev01
    
    #driver-class-name: org.postgresql.Driver
    #url: jdbc:postgresql://localhost:5432/springboot
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:postgresql://localhost:5432/springboot
    username: hermeswing
    password: pass
    #sql-script-encoding: utf-8            # springboot 1.5.x - Deprecated!
##############################################################
# - 경로 : /src/main/resources 경로 하위
# - schema.sql : 테이블 생성 스크립트
# - data.sql : 데이터 입력 스크립트
# - 파일이름규칙 : schema-${platform}.sql , data-${platform}.sql
#   ex. schema-h2.sql , shcema-postgres.sql
#spring.datasource.initialization-mode=always # Spring Boot <v2.5.x
# DB초기화(schema.sql, data.sql) , [always : 기동 시 매번 동작, never : 기동 시 동작하지 않음]
    #schema: classpath*:initdata/${platform}/schema.sql
    #data: classpath*:initdata/${platform}/data.sql
    #schema: classpath*:initdata/h2db/schema.sql
    #data: classpath*:initdata/h2db/data.sql
    #initialization-mode: always
  sql:
    init:
      #schema-locations: classpath*:initdata/${platform}/schema.sql
      #data: classpath*:initdata/${platform}/data.sql
      schema-locations: classpath*:initdata/schema-postgresql.sql
      data-locations: classpath*:initdata/data-postgresql.sql
      mode: always          # Spring Boot >=v2.5.0

 


spy.properties

appender = com.p6spy.engine.spy.appender.Slf4JLogger
#driverlist = org.h2.Driver, org.postgresql.Driver
driverlist = org.postgresql.Driver
logMessageFormat = com.p6spy.engine.spy.appender.MultiLineFormat

spy.properties 참조

https://p6spy.readthedocs.io/en/latest/configandusage.html

 

Configuration and Usage — p6spy 3.9.2-SNAPSHOT documentation

An example spy.properties file follows (please note default values mentioned as these refer to defaults mentioned in section: Configuration and Usage): modulelist modulelist holds the list of p6spy modules activated. A module contains a group of functional

p6spy.readthedocs.io


https://hermeslog.tistory.com/551

 

[SpringBoot] JPA 개발 시 p6spy를 이용해서 쿼리 로그 남기기

JPA를 공부하다보니, JPA에서는 Logging 시 p6spy 이라는 걸 사용하는 방법도 있다고 합니다. SpringBoot 만을 사용할때에는 log4jdbc-log4j2를 주로 사용했는데, JPA를 보다보니 p6spy라는 것도 있네요. 참조가

hermeslog.tistory.com

 

728x90