Spring boot 3 RestClient and RestTemplate logging http requests and response

In Spring boot(SB) 2 the configuration was different, now in SB3 we need to configure the rest client differently. Before SB3 there was RestTemplate now there is new Rest api coming in spring world. Find more details here. Adding the required dependency which will do the real http logging here. implementation 'org.apache.httpcomponents.client5:httpclient5:5.3.1' The big difference is properties configuration, it is changed and not documented on spring site logging.level.org.apache.hc.client5.http.wire=DEBUG logging.level.org.apache.hc.client5.http=DEBUG Using only “wire” will give request/response dump. And that extra http=DEBUG will give connection and more debug log, find it at the end of the page. ...

May 18, 2024 · 8 min · Özkan Pakdil

Using sqlite with h2 console

Sometimes we(developers/programmers) may need to see database tables in tree and run some select, insert quickly on the db, H2 console is the best tool in spring boot to reach database on dev environment. And I saw a question about “Using Flyway with SQLite database in spring boot”, I have been using liquibase around 5 years but I have not tried flyway before, learning opportunity. So I preapred an example with gradle spring boot, sqlite and h2 console and answered the question. ...

October 10, 2023 · 1 min · Özkan Pakdil

Printing running sqls in logs with spring boot 3 and hibernate 6

In application.properties logging.level.org.hibernate=info logging.level.org.hibernate.SQL=debug logging.level.org.hibernate.orm.jdbc.bind=trace logging.level.org.hibernate.stat=debug logging.level.org.hibernate.SQL_SLOW=info logging.level.org.hibernate.cache=debug will make print all sqls and bindings with it like below 2023-04-08T09:31:54.232+01:00 DEBUG 164224 --- [ main] org.hibernate.SQL : insert into "address" ("city", "line1", "post_code", "id") values (?, ?, ?, ?) 2023-04-08T09:31:54.232+01:00 TRACE 164224 --- [ main] org.hibernate.orm.jdbc.bind : binding parameter [1] as [VARCHAR] - [Glasgow] 2023-04-08T09:31:54.232+01:00 TRACE 164224 --- [ main] org.hibernate.orm.jdbc.bind : binding parameter [2] as [VARCHAR] - [apt:0] 2023-04-08T09:31:54.232+01:00 TRACE 164224 --- [ main] org.hibernate.orm.jdbc.bind : binding parameter [3] as [VARCHAR] - [G0] 2023-04-08T09:31:54.232+01:00 TRACE 164224 --- [ main] org.hibernate.orm.jdbc.bind : binding parameter [4] as [BIGINT] - [28]

April 8, 2023 · 1 min · Özkan Pakdil

Printing running sqls with P6spy in spring boot 3 and hibernate 6

in build.gradle implementation 'p6spy:p6spy:3.9.1' In application.properties spring.datasource.url=jdbc:p6spy:h2:mem:testdb spring.datasource.driverClassName=com.p6spy.engine.spy.P6SpyDriver and in spy.properties driverlist=org.h2.Driver appender=com.p6spy.engine.spy.appender.StdoutLogger logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat customLogMessageFormat=%(currentTime)|%(executionTime)|%(sqlSingleLine) With this configuration application logs will appear in console. like below 2023-04-08T12:14:54.237+01:00 DEBUG 178209 --- [nio-8080-exec-4] o.s.w.f.CommonsRequestLoggingFilter : Before request [GET /byname/name1] 1680952494240|0|select c1_0."id",c1_0."last_name",c1_0."name" from "customer" c1_0 where c1_0."name"='name1' 1680952494242|0|select a1_0."customer_id",a1_1."id",a1_1."city",a1_1."line1",a1_1."post_code" from "customer_addresses" a1_0 join "address" a1_1 on a1_1."id"=a1_0."addresses_id" where a1_0."customer_id"=2 1680952494244|0|select o1_0."customer_id",o1_1."id",o1_1."create_time",o1_1."full_price",o1_1."items",o1_1."update_time",o1_1."version" from "customer_orders" o1_0 join "order" o1_1 on o1_1."id"=o1_0."orders_id" where o1_0."customer_id"=2 1680952494246|0|select b1_0."customer_id",b1_1."id",b1_1."create_time",b1_1."items",b1_1."update_time",b1_1."version" from "customer_baskets" b1_0 join "basket" b1_1 on b1_1."id"=b1_0."baskets_id" where b1_0."customer_id"=2 If required user can change the log format like described here, here is working example project. P6spy is good for development but should not be used in production, for production every database has their own monitoring logging solution which will show what sql is running from which user. ...

April 8, 2023 · 1 min · Özkan Pakdil