Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 232 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I log SQL statements in Spring Boot?

#11
Please use:

```lang-none
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
spring.jpa.show-sql=true
```

Reply

#12
If you want to view the actual parameters used to query you can use

```lang-none
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql=TRACE
```

Then notice that actual parameter value is shown as `binding parameter`...:

```lang-none
2018-08-07 14:14:36.079 DEBUG 44804 --- [ main] org.hibernate.SQL : select employee0_.id as id1_0_, employee0_.department as departme2_0_, employee0_.joining_date as joining_3_0_, employee0_.name as name4_0_ from employee employee0_ where employee0_.joining_date=?
2018-08-07 14:14:36.079 TRACE 44804 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [TIMESTAMP] - [Tue Aug 07 00:00:00 SGT 2018]
```


Reply

#13
This works for standard output too:

```lang-none
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
```

To log values:

```lang-none
logging.level.org.hibernate.type=trace
```

Just add this to `application.properties`.

Reply

#14
We can use any one of these in the *application.properties* file:

```lang-none
spring.jpa.show-sql=true
```

Example:

```lang-none
//Hibernate: select country0_.id as id1_0_, country0_.name as name2_0_ from country country0_
```

or

```lang-none
logging.level.org.hibernate.SQL=debug
```

Example:

```lang-none
2018-11-23 12:28:02.990 DEBUG 12972 --- [nio-8086-exec-2] org.hibernate.SQL : select country0_.id as id1_0_, country0_.name as name2_0_ from country country0_
```




Reply

#15
# **Log in to standard output**

Add to `application.properties`

### To enable
spring.jpa.show-sql=true

### To make the printing SQL beautify
spring.jpa.properties.hibernate.format_sql=true

This is the simplest way to print the SQL queries, though it doesn't log the parameters of prepared statements.

And it’s not recommended since it’s not such as optimized logging framework.

### Using a logging framework

Add to `application.properties`

### Logs the SQL queries
logging.level.org.hibernate.SQL=DEBUG
### Logs the prepared statement parameters
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

### To make the printing SQL beautify
spring.jpa.properties.hibernate.format_sql=true

By specifying the above properties, log entries will be sent to the configured log appender, such as log-back or [Log4j][1].

[1]:

[To see links please register here]




Reply

#16
Settings to avoid
-----------------

You should not use this setting:

```lang-none
spring.jpa.show-sql=true
```

The problem with `show-sql` is that the SQL statements are printed in the console, so there is no way to filter them, as you'd normally do with a Logging framework.

Using Hibernate logging
-----------------------

In your log configuration file, if you add the following logger:

```lang-xml
<logger name="org.hibernate.SQL" level="debug"/>
```

Then, Hibernate will print the SQL statements when the JDBC `PreparedStatement` is created. That's why the statement will be logged using parameter placeholders:

```lang-sql
INSERT INTO post (title, version, id) VALUES (?, ?, ?)
```

If you want to log the bind parameter values, just add the following logger as well:

```lang-xml
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="trace"/>
```

Once you set the `BasicBinder` logger, you will see that the bind parameter values are logged as well:

```lang-none
DEBUG [main]: o.h.SQL - insert into post (title, version, id) values (?, ?, ?)
TRACE [main]: o.h.t.d.s.BasicBinder - binding parameter [1] as [VARCHAR] - [High-Performance Java Persistence, part 1]
TRACE [main]: o.h.t.d.s.BasicBinder - binding parameter [2] as [INTEGER] - [0]
TRACE [main]: o.h.t.d.s.BasicBinder - binding parameter [3] as [BIGINT] - [1]
```

Using datasource-proxy
----------------------

The [datasource-proxy][1] OSS framework allows you to proxy the actual JDBC `DataSource`, as illustrated by the following diagram:

[![DataSource-Proxy][2]](

[To see links please register here]

)

You can define the `dataSource` bean that will be used by Hibernate as follows:

@Bean
public DataSource dataSource(DataSource actualDataSource) {
SLF4JQueryLoggingListener loggingListener = new SLF4JQueryLoggingListener();
loggingListener.setQueryLogEntryCreator(new InlineQueryLogEntryCreator());
return ProxyDataSourceBuilder
.create(actualDataSource)
.name(DATA_SOURCE_PROXY_NAME)
.listener(loggingListener)
.build();
}

Notice that the `actualDataSource` must be the `DataSource` defined by the [connection pool][2] you are using in your application.

Next, you need to set the `net.ttddyy.dsproxy.listener` log level to `debug` in your logging framework configuration file. For instance, if you're using Logback, you can add the following logger:

```lang-xml
<logger name="net.ttddyy.dsproxy.listener" level="debug"/>
```

Once you enable `datasource-proxy`, the SQL statement are going to be logged as follows:

```lang-none
Name:DATA_SOURCE_PROXY, Time:6, Success:True,
Type:Prepared, Batch:True, QuerySize:1, BatchSize:3,
Query:["insert into post (title, version, id) values (?, ?, ?)"],
Params:[(Post no. 0, 0, 0), (Post no. 1, 0, 1), (Post no. 2, 0, 2)]
```

[1]:

[To see links please register here]

[2]:


Reply

#17
You just need to set `spring.jpa.show-sql=true` in *application.properties*.

For example, you may refer to *[ConfigServerRepo/application.yaml][1]*.

[1]:

[To see links please register here]



Reply

#18
In my YAML file:

logging:
level:
org.hibernate.SQL: DEBUG
org.hibernate.type.descriptor.sql: TRACE

Spring Boot version: 2.3.5.RELEASE


Reply

#19
Add these in the properties. Quoting [Hibernate Show SQL](

[To see links please register here]

):

```
# Show SQL statement
logging.level.org.hibernate.SQL=debug

# Show SQL values
logging.level.org.hibernate.type.descriptor.sql=trace
```
Reply

#20
If you are using JdbcTemplate, add the below in `application.properties` file to log SQL and parameter values.

logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
logging.level.org.springframework.jdbc.core.StatementCreatorUtils=TRACE



Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through