log4jdbc

1 분 소요

log4jdbc?

Log4jdbc is a Java library that provides JDBC (Java Database Connectivity) logging and debugging capabilities. It is used to log SQL statements, parameters, and results generated by JDBC calls, making it a helpful tool for debugging and performance monitoring of database interactions in Java applications.

Log4jdbc == library
JDBC 로깅하기위한 library
SQL구문이나 파라미터, 결과값까지 출력해주는 유용한 라이브러리

features

구문 디버깅

SQL Statement Logging: log4jdbc allows you to log all SQL statements sent to a database, along with their parameters and results. This is particularly useful for debugging database-related issues, understanding how an application interacts with the database, and identifying potential performance bottlenecks.

Debugging: By inspecting the logged SQL statements, developers can analyze and troubleshoot issues related to incorrect SQL queries, unexpected data, or database connectivity problems.

디테일한 SQL문법을보면서 디버깅가능

성능 모니터링

Profiling: log4jdbc can help in profiling database performance by providing insights into how long each SQL statement takes to execute. This information can be crucial for optimizing database queries and improving application performance.

SQL구문이 얼마나오래걸리는지도 확인 가능

흐름 및 동작방식

Principle of Operation: log4jdbc operates by acting as a proxy between the application’s JDBC calls and the actual database driver. It wraps the original JDBC driver using Java’s Dynamic Proxy mechanism and intercepts the SQL statements and method calls made by the application to the JDBC driver.

log4jdbc는 애플리케이션의 JDBC의 호출과 실제 database driver 사이에서 작동.

graph LR;
    ApplicationJDBC --> Log4jdbc;
    Log4jdbc --> DBDriver;

    style Log4jdbc fill:#ffcc00

When the application makes JDBC calls using this special URL, log4jdbc intercepts these calls, logs the relevant information (SQL statements, parameters, etc.), and then forwards the calls to the actual database driver for execution. The logged information is output to the logging framework (e.g., log4j or logback), which you can configure to store the logs in files, the console, or any other logging destination.

  1. Application JDBC의 Call 요청
  2. Log4jdbc가 해당요청을 훅킹해 관련정보를 기록(구문, 인자, …)
  3. 요청을 전달 to 실제 DatabaseDriver한테
  4. 기록된정보는 logging framework에 출력됨 + 어디에 출력할것인지 설정가능 (파일 or 콘솔)

설정방법

When you use log4jdbc, you typically configure it to replace the standard JDBC driver URL with its own special URL. For example, instead of using the JDBC URL like jdbc:mysql://localhost:3306/mydatabase, you would use jdbc:log4jdbc:mysql://localhost:3306/mydatabase.

yaml 설정방식(boot기준)

JDBC
jdbc:mysql://localhost:3306/mydatabase
Logj4dbc
jdbc:log4jdbc:mysql://localhost:3306/mydatabase.

요약

그 동안 유용하게 사용했었지만 정리해보고싶어서 정리