❮ Previous Next ❯

Hibernate Log4j

Log4j allows output to be logged anywhere from console to file to network ports to databases. But the simple configuration that you have is supposed to print on the console as well. So first remove the show_sql property to see if Log4j puts anything on the console at all.


If this doesn't work, it shows that Log4j is not configured correctly. If you're using hibernate > 3.5, it uses the slf4j api, which uses logback by default instead of log4j. You can easily switch to log4j by removing logback jar(s) from your classpath, and adding slf4j-log4j12.jar and log4j.jar instead.


The Log4j tracing also prints the queries using '?', but it also prints the parameter bindings, i.e. what the '?' will be replaced with by the database driver or server.


In order to setup logging you will need slf4j-api.jar in your classpath together with the jar file for your preferred binding - slf4j-log4j12.jar in the case of Log4J.


Put log4j.properties file in your classpath. An example properties file is distributed with Hibernate in the src/ directory.


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. Here we have to load the slf4j.jar and log4j.jar files with hibernate jar files.


log4j.xml file

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} [%t] %-5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="rolling-file" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="Program-Name.log"/>
<param name="MaxFileSize" value="1000KB"/>
<!-- Keep one backup file -->
<param name="MaxBackupIndex" value="4"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %l - %m%n"/>
</layout>
</appender>
<root>
<priority value ="debug" />
<appender-ref ref="console" />
<appender-ref ref="rolling-file" />
</root>
</log4j:configuration>

❮ Previous Next ❯

Follow Us





  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved