Hibernate - Logging By Log4j Using Properties File

Last Updated : 26 Mar, 2026

In this approach, logging is configured using a log4j.properties file placed in the classPath. Hibernate automatically reads this file and applies logging settings like log level, output format, and destination (console/file).

  • Lightweight and easy-to-read configuration
  • No need for complex XML structure
  • Widely used in small to medium projects

Apache Log4j

Apache Log4j is a Java logging framework used to record events and debug applications. In Hibernate, it helps track SQL queries, transactions, and internal operations, and can be configured using a simple log4j.properties file.

  • Helps in debugging Hibernate applications by logging runtime details
  • Supports console and file-based logging
  • Easier configuration compared to XML format

Step-by-Step process to configure Logging using properties File

Follow below steps to set up and enable Log4j logging in Hibernate using the log4j.properties file.

Step 1: Add Required JAR Files

Add Log4j and SLF4J dependencies along with Hibernate

XML
<!-- Log4j dependency: Provides actual logging implementation (writes logs to console/file) -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

<!-- SLF4J API: Acts as a logging abstraction used by Hibernate internally -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.36</version>
</dependency>

<!-- SLF4J Log4j Binding: Connects SLF4J with Log4j (bridge between API and implementation) -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.36</version>
</dependency>

Step 2: Create log4j.properties File

  • Place it inside src/main/resources
  • This file contains all logging configuration settings.
example image of the Log4j.properties file

Step 3: Define Root Logger

Sets the default logging level and output destinations.

log4j.rootLogger=ERROR, stdout, fout

Step 4: Configure Console Appender

Displays logs on the console during runtime.

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n

Step 5: Configure File Appender

Stores logs in a file for later analysis.

log4j.appender.fout=org.apache.log4j.FileAppender
log4j.appender.fout.File=crawl.log
log4j.appender.fout.layout=org.apache.log4j.PatternLayout
log4j.appender.fout.layout.ConversionPattern=%p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n

Step 6:Set Custom Logging Levels

Controls logging level for specific packages/classes.

log4j.logger.com.endeca=INFO
log4j.logger.com.endeca.itl.web.metrics=INFO

Log4j.properties file

XML
# 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


Comment