Hibernate Log4j Logging With Examples

Welcome to Hibernate Log4j Logging example. Hibernate 4 uses JBoss logging rather than slf4j in older hibernate versions. Today we will look into how to configure log4j in a hibernate application.

Hibernate Log4j Logging

Create a maven project in the Eclipse or your favorite IDE, final project structure will look like below image.

Hibernate-Log4j-Example-450x285

Let’s look into each of the components one by one.

Hibernate Log4j Maven Dependencies

We need hibernate-core, mysql driver and log4j dependencies in our project, our final pom.xml file looks like below.

Log4j Configuration File

I am using XML based log4j configuration, we can also use property file based configuration.

log4j.xml

This file needs to be placed at the root folder, so that our main class can access it. Notice the location of log4j log files, our application generated logs will go into project.log whereas hibernate logs will go into system.log file.

Hibernate Configuration File

Our hibernate configuration xml looks like below.

hibernate.cfg.xml

Hibernate SessionFactory utility class

Notice that I am using log4j Logger for logging purpose, since this class is part of our application, the logs should go into project.log file.

Model Classes

We have two model classes – Employee and Address. I am using the same database setup as in HQL Example, so you can check that tutorial to look into database setup and model classes. Both of these classes are simple java beans with hibernate annotation based mapping.

Hibernate Log4j Logging Test Class

Our main class looks like below.

Notice that we don’t need to do anything to plugin log4j logging. Obviously we would need to configure it first before using it, that’s why I am using static block to make sure log4j is configured before we start using it. I am using org.apache.log4j.xml.DOMConfigurator because our log4j configuration is XML based, if you have property file based configuration, you should use org.apache.log4j.PropertyConfigurator class.

Now when we execute above program, we get following data in log4j files.

project.log

Notice that above log file contains only entries generated by our program, this way we can separate our application logs from hibernate logs.

system.log

Since our hibernate logs level is set to INFO mode, there are not many logs. If you will change it to debug, even this simple program generates more than 1400 lines of log. Analyzing these logs are sometime crucible in understanding how hibernate works internally and to debug issues related to hibernate.

Also, we got following logs generated in console.

Notice that Hibernate is printing SQL queries to the console log, this is happening from org.hibernate.engine.jdbc.spi.SqlStatementLogger class logStatement method, code snippet is:

This is controlled by hibernate.show_sql property in hibernate.cfg.xml file. Actually if you are using log4j, then you should turn it off because they will also be part of log files if logging level is set to DEBUG mode. Having it in log files make more sense that getting it printed on console.

That’s all for hibernate log4j logging example, as you can see it’s very easy to plugin and all we need is to configure log4j properly. You can download the project from below link.

By admin

Leave a Reply