Open In App

Hibernate – Logging by Log4j using xml File

Last Updated : 12 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The process of Hibernate Logging by Log4j using an XML file deals with the ability of the computer programmer to write the log details of the file he has created by executing it permanently. The most important tools in Java language like the Log4j and Log back frameworks. These frameworks in Java language help the programmer to install and initiate the process of hibernating by an installed XML file.

Hibernate is a framework in the Java programming language that allows the users to deal with the implementation and the database operations in the SQL commands using them without actually concerning about the basic input parameters and constraints to be passed. The most popular version of the application that is used by the Hibernate framework for logging the messages to the computer is the JBoss Logging library which acts as a riding bridge between the various logging services within the Java programming language using the log4j command.

For basic purposes, there are mainly two types of ways by which we can perform the process of logging into our computer. They are as follows:

  • XML file containing Log4j
  • Properties file containing Log4j

Hibernating Logging

The Hibernating Logging process deals with creating the delaying of the basic binary operations that take place in the Java programming language of the computer. There are various types of delay states in the Hibernating Logging which are stated as follows:-

States

Explanation

OFF switches off the logging process
INFO sends the information to the user
CONFIG sends the configuration messages
SEVERE sends the failure message

The Java Development Kit (JDK) machine in the computer plays an important role in the process of hibernating logging by creating and executing the XML file. The Java programming language helps to create the logging of the internal critical infrastructure of the database and then perform the SQL-based operations to execute the queries. Also, the hibernating logging process should focus on executing the current tasks first and rendering them with the most utilization of the processor. The processor must use the minimum amount of CPU overhead for the logging of the messages in the hibernation of logging using the XML file.

SQL Queries in Hibernation

The SQL queries play an important role in the hibernation and logging of the log4j framework in the Java programming language because of the following stated reasons:-

  • The hibernation execution of the XML file has the ability to execute all the given SQL query and language statements even without using the logging framework of the log4j. Hence, it is difficult for the programmer to declare and execute the log4j command which contains some important logging messages for the execution and hibernation of the program source code. Also, the given stated process is much steady and stable as compared to the highly optimized logging framework that happens because of the logging messages in the XML file.
  • One better way of executing the SQL commands in the log4j framework is that we can pass them through the binary bitwise operations in order to make them a part of the jar file used in the program source code of the Java programming language.       

Commands for Logging in Hibernate

Logging in hibernate helps to debug our code and remove any unwanted errors from it because it is the most popular library of tools in the Java programming language that allows the user the ability to use the pre-defined set of in-built functions in log4j that removes all the error from the program source code. All the commands in the hibernate which help the user to execute the logging process are as follows:

org.hibernate.SQL : inputs all the SQL statements for the execution purpose.
org.hibernate : inputs all hibernate logging messages.
org.hibernate.transaction : records the transaction related SQL queries.
org.hibernate.jdbc : inputs all the JDBC resources for execution.
org.hibernate.type : inputs all the JDBC queries for the computer to produce the XML file.

Program Code for Hibernate using log4j

XML




<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> 
  
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"  
    debug="false">  
  <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">  
   <layout class="org.apache.log4j.PatternLayout">  
    <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />  
   </layout>  
  </appender>  
  <appender name="ASYNC" class="org.apache.log4j.AsyncAppender">  
          <appender-ref ref="CONSOLE" />  
          <appender-ref ref="FILE" />  
  </appender>  
  <appender name="FILE" class="org.apache.log4j.FileAppender">  
      <param name="File" value="C:/gfg.log" />  
      <param name="MaxIndex" value="100" />  
   <layout class="org.apache.log4j.PatternLayout">  
    <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />  
  </layout>  
  </appender>  
     <category name="org.hibernate">  
          <priority value="Error" />  
      </category>  
      <category name="java.sql">  
          <priority value="error" />  
      </category>  
      <root>  
   <priority value="link" />  
          <appender-ref ref="node" />  
      </root>  
</log4j:configuration>


Hibernate Configuration

XML




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  
<log4j:configuration xmlns:log4j="https://jakarta.apache.org/log4j/"
    debug="true">
    <appender name="FILE" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="logs/system.log" />
        <param name="Append" value="true" />
        <param name="MaxFileSize" value="500MB" />
        <param name="MaxBackupIndex" value="100" />
  
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />
        </layout>
    </appender>
  
    <appender name="journaldev-hibernate" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="logs/project.log" />
        <param name="Append" value="true" />
        <param name="ImmediateFlush" value="true" />
        <param name="MaxFileSize" value="200MB" />
        <param name="MaxBackupIndex" value="50" />
  
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />
        </layout>
    </appender>
  
    <logger name="com.journaldev.hibernate" additivity="false">
        <level value="DEBUG" />
        <appender-ref ref="journaldev-hibernate" />
    </logger>
  
    <logger name="org.hibernate" additivity="false">
        <level value="INFO" />
        <appender-ref ref="FILE" />
    </logger>
    <root>
        <priority value="INFO"></priority>
        <appender-ref ref="FILE" />
    </root>
  
</log4j:configuration>


Output:

The output file created while running the program gets saved as the log4j2.xml in the file directory where we same the Hibernate program for Logging. 

Program Directory for Log4j using XML

Then run your program file in the Hibernate web application, which opens up the Log files which are saved in the log4j2.xml file while performing the Logging operation on Log4j using the XML file in the Hibernate framework of Java programming language.

Output Logs of the Program:

Output Logs of the Program

The Output Logs of the Program using logging in Hibernate



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads