24 Aralık 2020 Perşembe

SpringBoot spring.jpa Hibernate'e Özel Ayarlar - SQL Loglama Ayarları

Giriş
Sadece Spring kullanarak sql loglama biraz yetersiz kaliyor

1. Stdout'a Loglama
show-sql Alanı
Açıklaması şöyle. Yani çıktıyı System.out'a gönderir, ancak System.out yavaştır ve loglanmaz
It’s pretty common for developers to enable the spring.jpa.show-sql setting in the configuration file. By setting this to true, we will see all SQL statements performed by Hibernate printed on the console. This is very helpful for debugging performance issues, as we can see exactly what’s going on in the database.

But it doesn’t log the SQL query. It prints it on the console!
Örnek
Şöyle yaparız.
spring.jpa.show-sql=true
Şuna benzer bir çıktı alırız. Ancak bu çıktı uzun SQL cümleleri için okunaklı değil.
Hibernate: insert into program (id, created_at, title, image_url) values (?, ?, ?, ?)
2. Daha  Detaylı Loglama
1. Daha okunaklı bir SQL isteyebiliriz
2. Ancak yine de bir sorun var. spring.jpa.show-sql=true parametreleri göstermiyor. Parametreleri görmek isteyebiliriz

2.1 Daha Okunaklı SQL
Eğer Hibernate kullanıyorsak daha okunaklı SQL cümleleri için şöyle yaparız
spring.jpa.properties.hibernate.format_sql=true
Şuna benzer bir çıktı alırız. Bu çıktı biraz daha okunaklı ancak halen parametreler görünmüyor
Hibernate: 
    insert 
    into
        program
        (id, created_at, title, image_url) 
    values
        (?, ?, ?, ?)
2.2 Parametreleri görmek
Açıklaması şöyle
By using loggers, we can trace the statement parameters also.
Açıklaması şöyle
Hibernate uses 2 different log categories and log levels to log the executed SQL statements and their bind parameters:

1. The SQL statements are written as DEBUG messages to the category org.hibernate.SQL.
2. The bind parameters are logged to the org.hibernate.type.descriptor.sql category with log level 
TRACE.
Örnek
Örnek
Şöyle yaparız. Burada logger üzerinden ayar yapılıyor
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
Şuna benzer bir çıktı alırız. ? karakteri halen var ancak en azından parametreler de loglanıyor.
....SQL : insert into program (id, created_at, title, image_url) values (?, ?, ?, ?)
...BasicBinder : binding parameter [1] as [BIGINT] — [1]
...BasicBinder : binding parameter [2] as [TIMESTAMP] — [Sat Oct 24 00:28:25 IST 2020]
...BasicBinder : binding parameter [3] as [VARCHAR] — [Hello World]
...BasicBinder : binding parameter [4] as [VARCHAR] — [hello.jpg]
Örnek
Şöyle yaparız. Burada ayarlar Hibernate kütüphanesine geçiliyor.
spring.jpa.properties.hibernate.type=trace

3. Yavaş SQL Cümleleri
Açıklaması şöyle
Once you’ve enabled query logging, you can analyze the logs to identify slow queries. Slow queries are typically defined as queries that take longer than a certain threshold to execute. This threshold can be set based on your application’s performance requirements.

In the logs, you’ll see the SQL statements that were executed along with the time it took to execute each statement. You can use a log analyzers tool, like Log Analyzer or Log Analyzer 2, to analyze the logs and identify slow queries.
Örnek
Şöyle yaparız
# Enables logging
logging.level.org.hibernate.type=trace
logging.level.org.hibernate.stat=debug
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace

# Log slow queries (if query execution time exceeds the specified value in milliseconds)
spring.jpa.properties.hibernate.generate_statistics=true
spring.jpa.properties.hibernate.session_factory.statistics.log_summary=true
spring.jpa.properties.hibernate.session_factory.statistics.log_slow_statements=true
spring.jpa.properties.hibernate.session_factory.statistics.slow_query_threshold_millis=500





Hiç yorum yok:

Yorum Gönder