Reputation: 2953
In hibernate configuration I have mentioned show_sql=true.
In the logs I can see that queries are printing which is executed by hibernate.
Like below
Hibernate : <insert query>
Hibernate : <select query>
I want to print the current timestamp as well along with query. Like
<current time > Hibernate : <insert query>
<current time > Hibernate : <select query>
I am using Hibernate 4.
I have used hibernate.generate_statistics=true
but this does not give time for insertion. Moreover, I just need current time.
Upvotes: 2
Views: 2878
Reputation: 16051
The show_sql=true
option only sets up writing to the system console, it is not a true logger, so you cannot configure it. If you want to get meaningful logging from Hibernate you should configure a logging framework, and use it to control the logging of Hibernate.
I have used the following logging frameworks already, so I can tell you, that these are working well with Hibernate: log4j, slf4j, logback, log4j2.
For example, this line in a log4j.properties
file configures log4j to keep the same info, which is put out with show_sql=true
:
log4j.category.org.hibernate.SQL=DEBUG
But in this case you can configure the output. In my case, the format is defined in the console
appender:
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p %F:%L - %m%n
This way, the output is:
<current time> DEBUG SqlStatementLogger.java:104 - <insert query>
log4j.properties
fileFor the sake of completeness, I have included my configuration file:
# root logger
log4j.rootLogger = INFO, stdout
# category filters
log4j.category.org.hibernate.SQL=TRACE
# appenders
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p %F:%L - %m%n
Upvotes: 2
Reputation: 1
Hibernate maintains statistics on which objects were queried, and how often
– Enable statistics in the configuration file
• hibernate.generate_statistics=true
Hibernate Interfaces
– Statistics for global information
– EntityStatistics for info on Object Type
– QueryStatistics for SQL and HQL queries
I think you require logging for timestamp not stats, but you can use any of the below options as per your requirements.
You can use log4jdbc JDBC proxy driver for logging SQL and other interesting information.
You can use slf4j-api.jar in your classpath together with the jar file for your preferred binding - slf4j-log4j12.jar
To use Log4j you will also need to place a log4j.properties file in your classpath. An example properties file is distributed with Hibernate in the src/ directory.
Hibernate Log Categories
org.hibernate.SQL - Log all SQL DML statements as they are executed
org.hibernate.type - Log all JDBC parameters
org.hibernate.tool.hbm2ddl - Log all SQL DDL statements as they are executed
org.hibernate.pretty - Log the state of all entities (max 20 entities) associated with the session at flush time
org.hibernate - Log everything. This is a lot of information but it is useful for troubleshooting
with Hibernate, you should almost always work with debug enabled for the category org.hibernate.SQL, or, alternatively, the property hibernate.show_sql enabled
<attribute name="ShowSqlEnabled">true</attribute>
EntityStatistics entityStats =
stats.getEntityStatistics( Cat.class.getName() );
long changes =
entityStats.getInsertCount()
+ entityStats.getUpdateCount()
+ entityStats.getDeleteCount();
log.info(Cat.class.getName() + " changed " + changes + "times" );
` Please check Hibernate Documentation for more details.
Upvotes: 0