Hibernate Logging By Log4j Using Properties File (original) (raw)

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).

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.

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 log4j 1.2.17 org.slf4j slf4j-api 1.7.36 org.slf4j slf4j-log4j12 1.7.36

`

Step 2: Create log4j.properties File

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

`