Hibernate logging by Log4j using xml file

logging enables the developer to compose the log details into a file for all time. Log4j and Logback frameworks can be utilized in rest system to help logging.

There are two different ways to perform logging utilizing log4j:

  1. By log4j.xml file (or)
  2. By log4j.properties file

Levels of Logging

Following are the regular logging levels.

Levels Description
OFF This level is utilized to kill logging.
WARNING This is a message level that demonstrates a problem.
SEVERE This is a message level that shows a failure.
INFO This level is utilized for informational messages.
CONFIG This level is utilized for static configuration messages.

Steps to perform Hibernate logging by Log4j utilizing xml file

There are two different ways to perform logging utilizing log4j utilizing xml file:

  1. Load the log4j container files with hibernate
  2. Create the log4j.xml file inside the src envelope (parallel with hibernate.cfg.xml file)

Example of Hibernate logging by Log4j using xml file

You can enable logging in hibernate by following only two steps in any hibernate example. This is the first example of hibernate application with logging support using log4j.

Load the required jar files

You need to load the slf4j.jar and log4j.jar files with hibernate jar files.

Create log4j.xml file

Now you need to create log4j.xml file. In this example, all the log details will be written in the C:/javatpointlog.log file.

log4j.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">  
  3. <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"  
  4.     debug="false">  
  5. <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">  
  6.  <layout class="org.apache.log4j.PatternLayout">  
  7.   <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />  
  8.  </layout>  
  9. </appender>  
  10.     <appender name="ASYNC" class="org.apache.log4j.AsyncAppender">  
  11.         <appender-ref ref="CONSOLE" />  
  12.         <appender-ref ref="FILE" />  
  13. </appender>  
  14. <appender name="FILE" class="org.apache.log4j.RollingFileAppender">  
  15.     <param name="File" value="C:/javatpointlog.log" />  
  16.     <param name="MaxBackupIndex" value="100" />  
  17.  <layout class="org.apache.log4j.PatternLayout">  
  18.   <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />  
  19. </layout>  
  20. </appender>  
  21.     <category name="org.hibernate">  
  22.         <priority value="DEBUG" />  
  23.     </category>  
  24.     <category name="java.sql">  
  25.         <priority value="debug" />  
  26.     </category>  
  27.     <root>  
  28.         <priority value="INFO" />  
  29.         <appender-ref ref="FILE" />  
  30.     </root>  
  31. </log4j:configuration>  





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition


python