> For the complete documentation index, see [llms.txt](https://tech.x2bee.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tech.x2bee.com/dev-guide/developer-guide-en/pjt-prepare/framework-guide/query-logging.md).

# Query Logging

This document describes how to perform query logging using log4jdbc.

## Adding the log4jdbc Library

* Add the log4jdbc library dependency to the pom.xml file.

{% code title="pom.xml" %}

```xml
<dependencies>
    ....
    <dependency>
        <groupId>org.bgee.log4jdbc-log4j2</groupId>
        <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
        <version>1.16</version>
    </dependency>
</dependencies>
```

{% endcode %}

* When using Gradle, add the dependency to build.gradle:

{% code title="build.gradle" %}

```gradle
dependencies {
    ....
    implementation 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16'
}
```

{% endcode %}

## Adding the log4jdbc Configuration File

* Add a configuration file named **log4jdbc.log4j2.properties** under the resources folder.

{% code title="log4jdbc.log4j2.properties" %}

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

{% endcode %}

## Modifying DB Connection Information

* Add the following to driver-class-name:
  * net.sf.log4jdbc.sql.jdbcapi.DriverSpy
* Add log4jdbc to the URL.

Example application.yml:

{% code title="application.yml" %}

```yaml
spring:
  config:
    datasource:
      testdb:
        url: dbc:log4jdbc:postgresql://xxx.xx.xx.xx:xxxx:xxxx
        driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
```

{% endcode %}

## Adding Related Loggers to the logback Configuration

To control log4jdbc logs, add the loggers below to the logback configuration file. (The file name is either logback.xml or logback.yml, depending on the environment.)

{% code title="logback.yml / logback.xml (example)" %}

```xml
<!-- log4jdbc 옵션 설정 -->
<logger name="jdbc" level="off"/>

<!-- 커넥션 open close 이벤트 로그로 남김 -->
<logger name="jdbc.connection" level="off"/>

<!-- SQL문만을 로그로 남기며, PreparedStatement일 경우 관련된 argument 값으로 대체된 SQL문이 보여짐 -->
<logger name="jdbc.sqlonly" level="off"/>

<!-- SQL문과 해당 SQL을 실행시키는데 수행된 시간 정보(milliseconds)를 포함 -->
<logger name="jdbc.sqltiming" level="debug"/>

<!-- ResultSet을 제외한 모든 JDBC 호출 정보를 로그로 남김. 방대한 양의 로그가 생성되므로 특별히 JDBC 문제를 추적해야 할 필요가 있는 경우를 제외하고는 사용을 권장하지 않음-->
<logger name="jdbc.audit" level="off"/>

<!-- ResultSet을 포함한 모든 JDBC 호출 정보를 로그로 남기므로 방대한 양의 로그가 생성됨 -->
<logger name="jdbc.resultset" level="off"/>

<!-- SQL 결과 조회된 데이터의 table을 로그로 남김 -->
<logger name="jdbc.resultsettable" level="debug"/>
```

{% endcode %}

(Adjust the level values above as needed. Some loggers generate a large volume of logs, so caution is needed in production environments.)
