Hibernate - log4j Integration



Log4j is a logging framework, written entirely in Java. It is an open-source project from Apache Software Foundation.

In order to use Log4j, you need to put log4j library to your project's CLASSPATH. Then you need to set up a configuration file, which can be one of the following −

  • An XML file called log4j.xml

  • A properties file called log4j.properties

Different log levels of log4j

Following is the list of log levels supported by log4j framework.

Sr.No. Level & Description
1

ALL

Highest Debug level. Outputs everything that is possible.

2

DEBUG

Logs detailed information that is useful for debugging.

3

INFO

Logs informational messages that highlight the progress of the application at a high level.

4

WARN

Logs potentially harmful situations that are not necessarily errors.

5

ERROR

Designates error events that might still allow the application to continue running.

6

FATAL

Designates very severe error events that will presumably lead the application to abort.

7

OFF

Disables logging. Used to turn off logging completely.

8

TRACE

Designates finer-grained informational events than the DEBUG.

Configuration using log4j.xml

For xml file configuration, you need log4j.jar in your CLASSPATH. Remember to save log4j.xml in the same directory (src/) as hibernate.cfg.xml.

log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
   <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{MM/dd HH:mm:ss} %-5p
            %30.30c %x - %m\n"/>
      </layout>
   </appender>
   <appender name="FileAppender" class="org.apache.log4j.RollingFileAppender">
      <param name="File" value="C:/hibernate-log4j.log"/>
      <param name="MaxFileSize" value="10MB"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{MM/dd HH:mm:ss} %-5p
            %30.30c %x - %m\n"/>
      </layout>
   </appender>
   <root>
      <level value="info"/>
      <appender-ref ref="ConsoleAppender"/>
      <appender-ref ref="FileAppender"/>
   </root>
</log4j:configuration>

In, this configuration debug messages will be output to console (because of ConsoleAppender) and also to file (because of FileAppender) in C:/ > hibernate-log4j.log. You can change the file name as per your wish, but the extension needs to be log.

Configuration using log4j.properties

Similar to the XML file, the log4j.properties file must be put in under src/ folder (same as hibernate.cfg.xml).

log4j.properties

#Definetherootloggerwithappenderfile
log4j.rootLogger=DEBUG,FILE, stdout

#Output logmessagesto file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\hibernate.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE}%5p%c{1}:%L-
%m%n

#Outputlogmessagestostdout
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{yyyy-MM-ddHH:mm:ss}%-
5p%c{1}:%L-%m%n

# Hibernate logging
-------------------------------------------------
# General Hibernate logging
log4j.logger.org.hibernate=INFO
# Logs all SQL statements generated by Hibernate
log4j.logger.org.hibernate.SQL=DEBUG
Advertisements