Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaHibernate – Logging By Log4j Using Properties File

Hibernate – Logging By Log4j Using Properties File

Apache log4j is a java-based logging utility. Apache log4j role is to log information to help applications run smoothly, determine what’s happening, and debug processes when errors occur. log4j may logs login attempts (username, password), submission form, and HTTP headers (user-agent, x-forwarded-host, etc.) into the log file or database. Apache log4j 2.0 introduces

  • New plugin system
  • Support for properties
  • Support JSON base configuration
  • Automatic reloading its configuration

Logging in hibernate is done by using log4j. And we have two ways to retrieve logging using log4j. They are as follows 

  1. Logging using log4j by log4j.xml
  2. Logging using log4j by log4j.properties file

In this article, we are going to enable logging using log4j through the properties file.

Logging using log4j by the log4j.properties file

Follow these steps to enable logging using log4j through the properties file. 

  1. You can load the required jar files of log4j with hibernate jar files. 
  2. You need to load the slf4j.jar and log4j.jar files with hibernate jar files.

or the other way is to create a log4j.properties file

  1. You need to create log4j.properties file. 

This is an example image of the Log4j.properties file

example image of the Log4j.properties file

 

Example

# initialize root logger with level ERROR for stdout and fout
log4j.rootLogger=ERROR,stdout,fout

# set the log level for these components
log4j.logger.com.endeca=INFO
log4j.logger.com.endeca.itl.web.metrics=INFO

# add a ConsoleAppender to the logger stdout to write to the console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# use a simple message format
log4j.appender.stdout.layout.ConversionPattern=%m%n
# add a FileAppender to the logger fout
log4j.appender.fout=org.apache.log4j.FileAppender

# create a log file
log4j.appender.fout.File=crawl.log
log4j.appender.fout.layout=org.apache.log4j.PatternLayout

# use a more detailed message pattern
log4j.appender.fout.layout.ConversionPattern=%p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n

Both are good ways of practice to have logs by log4j. Mostly log4j.properties file along with hibernate-config.xml files are used to create user wanted logs. These logs are very helpful for tracing the errors and finding the process and endpoints that the code is touching while the process of execution.

RELATED ARTICLES

Most Popular

Recent Comments