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
- Logging using log4j by log4j.xml
- 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.
- You can load the required jar files of log4j with hibernate jar files.
- 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
- You need to create log4j.properties file.
This is an example image of the Log4j.properties file
# initialize root logger with level ERROR for stdout and fout
# set the log level for these components
# add a ConsoleAppender to the logger stdout to write to the console
# use a simple message format
# add a FileAppender to the logger fout
# create a log file
# use a more detailed message pattern
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!