Enabling Hibernate query logging through Java code - java

I want to print a particular hibernate query in UAT environment. However, I do not want to enable hibernate logging for all the queries since it will start generating huge logs.
Is there a way I can enable hibernate query logging through java code just before the query I want to print and then disable it immediately? Please note that I am using Spring data Jpa repository.

It depends on the logging library that you use.
I think Spring uses SLF4J and logback so something like the following should work:
LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
loggerContext.getLogger("org.hibernate.SQL").setLevel(Level.DEBUG);
and afterwards set
loggerContext.getLogger("org.hibernate.SQL").setLevel(Level.ERROR);

There is away to do customized logging with help of logback but not sure it will help to log the particular SQL logs.
Use this link -https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.logging

Related

Whitelist specific query from hibernate slow query logging

I am using below hibernate settings to log slow queries for my application
hibernate.session.events.log.LOG_QUERIES_SLOWER_THAN_MS : <time>
org.hibernate.SQL_SLOW: warn
It is working fine. But I want to whitelist a few queries so that even if they run beyond 'LOG_QUERIES_SLOWER_THAN_MS' will not be logged as a warning. I want to do that as I know there are a few queries which supposed to take a longer time to run and I don't want to generate any alerts for those.
Is there any way I can do that?
I have tried to find any settings I can provide during the query to override the behaviour for that specific query.
Are there any settings and API hibernate provide to achieve it?
There is no way to do that with plain Hibernate. You can just disable the org.hibernate.SQL_SLOW logger temporarily with your logger API by setting the level programatically to e.g. WARN and then reset it again to INFO.

Enable Debug Logs for Couchbase SDK with spring boot

I am using Couchbase SDK along with Spring boot 2.6.x version. I am using the spring spring-data-couchbase:jar:4.4.0 which in turn has com.couchbase.client:java-client:jar:3.3.0 dependency. The issue, is when trying to execute cluster.query() methods, I need to see what query is getting executed. I need to enable debug logs. However, I have tried configuring it under properties for logging.level for the package com.couchbase.client to DEBUG level, nothing is showing up. I tried the similar config in log4j.xml as well and no luck either. Does couchbase uses any wierd property or is it not reading the properties specified from either log4j or spring's properties?
You may ask if raw query is passed then why log specifically, but this is required if using parameterised queries and to debug it.
How does one enable logging for couchbase if using plain sdk methods under spring's context ?
Can you enable couchbase specific properties of spring-data and not package within the application?
logging:
level:
org.springframework.data.couchbase.repository.query: DEBUG
This should help ! It won't require any external dependency or logging solutions
For the cluster, add ServiceType.QUERY to captureTraffic...
env.ioConfig(it -> it.captureTraffic(ServiceType.QUERY))
and also set the log level for the com.couchbase category to TRACE

How does logging component work

I use HSQLDB on OSGI framework. And it is common solution to use pax-logging that support many logging frameworks (java logging, slf4j, jboss logging etc).
I don't have problems with pax-logging, however, I have problems with HSQLDB logging messages. HSQLDB logging component is very tricky - some messages go to pax-logging system, some go to console.
Could anyone explain what messages where must go and why.
There are separate logging components in HSQLDB.
The Server uses separate writers for log and error messages. The logs default to stdout and stderr but you can set each one to use a custom PrintWriter.
The optional SQL log is always a file. It can be turned on and off live for checking the SQL statements being executed.
The optional event log is a file or an external logging framework. The latter is used when the database is in-process in an application. In both configurations, it reports general persistence events at different levels of detail selected by the user.

How to enable debug logging in PostgreSQL JDBC/HikariCP?

I have Spring Boot 1.4.0, HikariCP 2.4.7, slf4j-api 1.7.21 and PostgreSQL JDBC 9.4.1208.
I want to see debug logs from PostgreSQL JDBC because I have some problems with HikariCP:
HikariPool-1 - Connection is not available, request timed out after 42734ms.
How can I enable debug logging to see what's going on?
I've tried:
-Adding:
org.postgresql.Driver.setLogLevel(Driver.DEBUG);
hikariDataSource = new HikariDataSource();
hikariDataSource.setLogWriter(new PrintWriter(System.out));
-Adding to VM options:
-Dorg.slf4j.simpleLogger.defaultLogLevel=debug
However, logs are the same as they were.
Few things to consider:
Consider logging to files
1) logging to sysout may cause you to miss updates
2) it's also slow and slows your app down
Consider keeping log config in slf4j config file instead.
Consider specific Hikari config:
-Dorg.slf4j.simpleLogger.log.com.zaxxer.hikari=error
Also, by default the log level for slf4j simple logger is info. So, if you're NOT seeing anything, I don't think it's config. It may be lack of some dependency (are you just using slf4j, or something else, like log4j as well?).
Finally, are you certain that your changes are picked up at all?
Since it's hard to say on this side, let me offer another side where logging can take place: Postgres.
Try changing PG logging config:
logging_collector = on # may be 'true' for older versions as well?
log_statement = all # 'true' for older versions
log_min_error_statement = error
First turns log collector on and makes logging not lose messages.
Second says everything interests you, even simple queries with syntax errors.
Third gives your error level.
Docs for Postgres will tell you more.

view SQL executed by Jasper report

When running a Jasper report in which the SQL is embedded in the report file (.jrxml), is it possible to see the SQL that is executed? Ideally, I'd also like to see the values that are substituted for each of the $P{} placeholders.
Cheers,
Don
JasperReports uses the Jakarta Commons Logging API.
Commons Logging has a discovery mechanism that connects to logging API you are using in your project.
You need to configure logger named "net.sf.jasperreports" in your logging configuration file to control the logging level of JasperReports.
If you are using Log4j, you can read this section of there documentation for exact details.
For example you may write something like this in log4j.properties file
log4j.logger.net.sf.jasperreports=INFO, Daily
Where "Daily" is name of an appender configured in same properties file.
Another option is to use p6spy. P6Spy is sort of a "proxy JDBC driver" that sits between the app and the real JDBC driver, and it can log everything that it sees. You should be able to download a copy here: http://www.p6spy.com/
If you're using Ms SQL you can use sql profiler, to see every query executed on the server.
EDIT: Here is an article on enabling sql query logging on MySql server: http://www.howtogeek.com/howto/database/monitor-all-sql-queries-in-mysql/
You can adjust your log4j settings to log the running SQL...

Categories