Jak logować wyrażenia SQL w Spring Boot?
Chcę rejestrować wyrażenia SQL w pliku.
Mam następujące właściwości w application.properties
spring.datasource.url=...
spring.datasource.username=user
spring.datasource.password=1234
spring.datasource.driver-class-name=net.sourceforge.jtds.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
security.ignored=true
security.basic.enabled=false
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=INFO
logging.file=c:/temp/my-log/app.log
Kiedy uruchamiam moją aplikację
cmd>mvn spring-boot:run
Mogę zobaczyć polecenia sql w konsoli, ale nie pojawiają się one w aplikacji plików.log. Plik zawiera tylko podstawowe logi z springa.
Co zrobić, aby w pliku dziennika zobaczyć polecenia sql?
17 answers
Spróbuj użyć tego w pliku właściwości:
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-06 15:39:30
To działa również na stdout:
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
Do logowania wartości:
logging.level.org.hibernate.type=trace
Po prostu dodaj to do application.properties
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-02-06 16:00:06
To działa dla mnie (YAML):
spring:
jpa:
properties:
hibernate:
show_sql: true
format_sql: true
logging:
level:
org:
hibernate:
type: trace
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-09 12:09:21
Ustawienia, aby uniknąć
Nie należy używać tego ustawienia:
spring.jpa.show-sql=true
Problem z show-sql
polega na tym, że polecenia SQL są drukowane w konsoli, więc nie ma sposobu na ich filtrowanie, jak zwykle robi się to w przypadku frameworka logowania.
Używanie logowania Hibernate
W pliku konfiguracyjnym dziennika, jeśli dodasz następujący logger:
<logger name="org.hibernate.SQL" level="debug"/>
Następnie, Hibernate wyświetli polecenia SQL po utworzeniu JDBC PreparedStatement
. Dlatego oświadczenie będzie rejestrowane użycie symboli zastępczych parametrów:
INSERT INTO post (title, version, id) VALUES (?, ?, ?)
Jeśli chcesz zapisać wartości parametrów bind, po prostu dodaj następujący logger:
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="trace"/>
Po ustawieniu rejestratora BasicBinder
, zobaczysz, że wartości parametrów bind są również rejestrowane:
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
Framework datasource-proxy OSS pozwala na zastępowanie rzeczywistego JDBC DataSource
, Jak pokazano na poniższym diagramie:
Możesz zdefiniować dataSource
bean, który będzie używany przez Hibernate w następujący sposób:
@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();
}
Zauważ, że {[13] } musi być DataSource
zdefiniowany przez [connection pool] [2], którego używasz w swojej aplikacji.
Następnie należy ustawić poziom dziennika net.ttddyy.dsproxy.listener
na debug
w pliku konfiguracyjnym logging framework. Na przykład, jeśli używasz Logback, możesz dodać następujący logger:
<logger name="net.ttddyy.dsproxy.listener" level="debug"/>
Po włączeniu datasource-proxy
, polecenie SQl zostanie zalogowane w następujący sposób:
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)]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2021-01-09 23:04:14
Proszę użyć:
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
spring.jpa.show-sql=true
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-01 15:39:32
Jeśli masz logback-wiosna.xml lub coś w tym stylu, dodaj do niego następujący kod
<logger name="org.hibernate.SQL" level="trace" additivity="false">
<appender-ref ref="file" />
</logger>
Mi pasuje.
Aby uzyskać również zmienne bind:
<logger name="org.hibernate.type.descriptor.sql" level="trace">
<appender-ref ref="file" />
</logger>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-13 13:30:35
Dla sterownika MS-SQL server (Microsoft SQL Server JDBC Driver).
Spróbuj użyć:
logging.level.com.microsoft.sqlserver.jdbc=debug
W twoim zgłoszeniu.Plik Właściwości.
Moje osobiste preferencje to ustawienie:
logging.level.com.microsoft.sqlserver.jdbc=info
logging.level.com.microsoft.sqlserver.jdbc.internals=debug
Możesz spojrzeć na te linki w celach informacyjnych:
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-21 19:29:21
Według dokumentacji jest to:
spring.jpa.show-sql=true # Enable logging of SQL statements.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-21 20:54:26
Przetłumaczona zaakceptowana odpowiedź na YAML działa dla mnie
logging:
level:
org:
hibernate:
SQL:
TRACE
type:
descriptor:
sql:
BasicBinder:
TRACE
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-03-12 21:39:54
Jeśli chcesz wyświetlić rzeczywiste parametry używane do zapytania, możesz użyć
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql=TRACE
Następnie zauważ, że rzeczywista wartość parametru jest pokazana jako binding parameter......
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]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-07 06:17:04
Możemy użyć dowolnego z tych w aplikacji .właściwości Plik:
spring.jpa.show-sql=true
example :
//Hibernate: select country0_.id as id1_0_, country0_.name as name2_0_ from country country0_
Lub
logging.level.org.hibernate.SQL=debug
example :
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_
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-11-23 06:58:45
Zaloguj się na standardowe wyjście
Dodaj do application.properties
### to enable
spring.jpa.show-sql=true
### to make the printing SQL beautify
spring.jpa.properties.hibernate.format_sql=true
To najprostszy sposób drukowania zapytań SQL, choć nie rejestruje parametrów przygotowanych poleceń. I jego nie jest zalecany, ponieważ nie jest taki jak zoptymalizowany framework rejestrowania.
Using Logging Framework
Dodaj do 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
Poprzez podanie powyższych właściwości, wpisy logów będą wysyłane do skonfigurowanego aplikacji log, takiej jak log-back lub log4j.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-20 09:12:55
Wystarczy ustawić
spring.jpa.show-sql=true
w aplikacji.właściwości
na przykład możesz zmienić to https://github.com/007anwar/ConfigServerRepo/blob/master/application.yaml
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-07-21 08:13:27
Użyj tego kodu w aplikacji file.właściwości:
#Enable logging for config troubeshooting
logging.level.org.hibernate.SQL=DEBUG
logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-08-03 12:57:56
Jeśli masz problem z tym ustawieniem i wydaje się, że czasami działa, a nie innym razem - zastanów się, czy czasy, w których nie działa, są podczas testów jednostkowych.
Wiele osób deklaruje własne właściwości czasu testowego poprzez adnotację @TestPropertySources
zadeklarowaną gdzieś w Twojej hierarchii dziedziczenia testowego. Spowoduje to nadpisanie ustawień application.properties
lub innych właściwości produkcyjnych, aby wartości, które ustawiasz, były skutecznie ignorowane w czasie testu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-09 02:39:15
Wprowadzenie spring.jpa.properties.hibernate.show_sql=true
do aplikacji.właściwości nie zawsze pomagały.
Możesz spróbować dodać properties.put("hibernate.show_sql", "true");
do Właściwości konfiguracji bazy danych.
public class DbConfig {
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource
) {
Map<String, Object> properties = new HashMap();
properties.put("hibernate.hbm2ddl.auto", "validate");
properties.put("hibernate.show_sql", "true");
return builder
.dataSource(dataSource)
.packages("com.test.dbsource.domain")
.persistenceUnit("dbsource").properties(properties)
.build();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-16 14:00:49
Ta strona wyjaśnia ten temat bardzo jasno. Polecam lekturę tej strony.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-12-11 00:45:10