Configure Logging Hibernate
Summary: To configure logging in hibernate you need to have log4j.properties available in class path and below dependency in your pom.xml file.
- log4j.properties file:
# Log messages to log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=C\:\\JavaHonk\\log.log log4j.appender.file.MaxFileSize=2MB log4j.appender.file.MaxBackupIndex=5 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n # Write output to console as stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n # Root logger option log4j.rootLogger=INFO, file, stdout # Log DEBUG level log4j.logger.org.hibernate=DEBUG # Log JDBC parameters log4j.logger.org.hibernate.type=ALL
- pom.xml file dependency:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.0</version> </dependency>
- If you are using eclipse project include slf4j-log4j12.jar in your class path.
Step 1: This demo is extension of this tutorial just you need log4j.properties file in src/main/resources folder and run it.
Step 2: Below is final project structure:
Step 3: To run test class right click –> Run As –> Java Application . You will see below full log
details on console:
Step 4: Now open log file you will see details as below:
Download project: HibernateAnnotationExample