欢迎来到Hibernate Log4j Logging示例。Hibernate 4在旧的hibernate版本中使用JBoss日志而不是slf4j。今天我们将研究如何在hibernate应用程序中配置log4j。
目录[ 隐藏 ]
在Eclipse或您喜欢的IDE中创建一个maven项目,最终的项目结构将如下图所示。
让我们逐个研究每个组件。
我们的项目中需要hibernate-core,mysql驱动程序和log4j依赖项,我们的最终pom.xml文件如下所示。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.journaldev.hibernategroupId>
<artifactId>HibernateLog4JExampleartifactId>
<version>0.0.1-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-coreartifactId>
<version>4.3.5.Finalversion>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.16version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.0.5version>
dependency>
dependencies>
project>
我使用基于XML的log4j配置,我们也可以使用基于属性文件的配置。
log4j.xml
xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false">
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/system.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true" />
<param name="MaxFileSize" value="200MB" />
<param name="MaxBackupIndex" value="100" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />
layout>
appender>
<appender name="journaldev-hibernate" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/project.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true" />
<param name="MaxFileSize" value="200MB" />
<param name="MaxBackupIndex" value="50" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />
layout>
appender>
<logger name="com.journaldev.hibernate" additivity="false">
<level value="DEBUG" />
<appender-ref ref="journaldev-hibernate" />
logger>
<logger name="org.hibernate" additivity="false">
<level value="INFO" />
<appender-ref ref="FILE" />
logger>
<root>
<priority value="INFO">priority>
<appender-ref ref="FILE" />
root>
log4j:configuration>
此文件需要放在根文件夹中,以便我们的主类可以访问它。注意log4j日志文件的位置,我们的应用程序生成的日志将进入project.log,而hibernate日志将进入system.log文件。
我们的hibernate配置xml如下所示。
hibernate.cfg.xml
xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
<property name="hibernate.connection.password">pankaj123property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/TestDBproperty>
<property name="hibernate.connection.username">pankajproperty>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
<property name="hibernate.current_session_context_class">threadproperty>
<property name="hibernate.show_sql">trueproperty>
<mapping class="com.journaldev.hibernate.model.Employee" />
<mapping class="com.journaldev.hibernate.model.Address" />
session-factory>
hibernate-configuration>
package com.journaldev.hibernate.util;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static Logger logger = Logger.getLogger(HibernateUtil.class);
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
logger.info("Hibernate Configuration loaded");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
logger.info("Hibernate serviceRegistry created");
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
catch (Throwable ex) {
logger.error("Initial SessionFactory creation failed." + ex);
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
if(sessionFactory == null) sessionFactory = buildSessionFactory();
return sessionFactory;
}
}
请注意,我使用log4j Logger进行日志记录,因为此类是我们的应用程序的一部分,日志应该进入project.log文件。
我们有两个模型类 - 员工和地址。我使用与HQL示例中相同的数据库设置,因此您可以检查该教程以查看数据库设置和模型类。这两个类都是简单的java bean,基于hibernate注释的映射。
我们的主要课程如下所示。
package com.journaldev.hibernate.main;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.journaldev.hibernate.model.Employee;
import com.journaldev.hibernate.util.HibernateUtil;
public class HibernateLog4jExample {
static{
System.out.println("Before log4j configuration");
DOMConfigurator.configure("log4j.xml");
System.out.println("After log4j configuration");
}
private static Logger logger = Logger.getLogger(HibernateLog4jExample.class);
@SuppressWarnings("unchecked")
public static void main(String[] args) {
//Prep work
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
//Get All Employees
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Employee");
List empList = query.list();
for(Employee emp : empList){
logger.info("List of Employees::"+emp.getId()+","+emp.getAddress().getCity());
}
tx.commit();
sessionFactory.close();
logger.info("DONE");
}
}
请注意,我们不需要执行任何操作来插入log4j日志记录。显然我们需要在使用之前先配置它,这就是为什么我在开始使用之前使用静态块来确保配置log4j的原因。我正在使用org.apache.log4j.xml.DOMConfigurator
因为我们的log4j配置是基于XML的,如果你有基于属性文件的配置,你应该使用org.apache.log4j.PropertyConfigurator
class。
现在当我们执行上面的程序时,我们在log4j文件中获得以下数据。
project.log
2014-06-05 20:00:11,868 -0700 [main] INFO (HibernateUtil.java:20) - Hibernate Configuration loaded
2014-06-05 20:00:11,936 -0700 [main] INFO (HibernateUtil.java:23) - Hibernate serviceRegistry created
2014-06-05 20:00:12,698 -0700 [main] INFO (HibernateLog4jExample.java:37) - List of Employees::1,San Jose
2014-06-05 20:00:12,698 -0700 [main] INFO (HibernateLog4jExample.java:37) - List of Employees::2,Santa Clara
2014-06-05 20:00:12,698 -0700 [main] INFO (HibernateLog4jExample.java:37) - List of Employees::3,Bangalore
2014-06-05 20:00:12,698 -0700 [main] INFO (HibernateLog4jExample.java:37) - List of Employees::4,New Delhi
2014-06-05 20:00:12,712 -0700 [main] INFO (HibernateLog4jExample.java:42) - DONE
请注意,上面的日志文件只包含我们程序生成的条目,这样我们就可以将应用程序日志与hibernate日志分开。
system.log
2014-06-05 22:49:58,415 -0700 [main] INFO (JavaReflectionManager.java:66) - HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
2014-06-05 22:49:58,423 -0700 [main] INFO (Version.java:54) - HHH000412: Hibernate Core {4.3.5.Final}
2014-06-05 22:49:58,426 -0700 [main] INFO (Environment.java:239) - HHH000206: hibernate.properties not found
2014-06-05 22:49:58,427 -0700 [main] INFO (Environment.java:346) - HHH000021: Bytecode provider name : javassist
2014-06-05 22:49:58,446 -0700 [main] INFO (Configuration.java:2073) - HHH000043: Configuring from resource: hibernate.cfg.xml
2014-06-05 22:49:58,447 -0700 [main] INFO (Configuration.java:2092) - HHH000040: Configuration resource: hibernate.cfg.xml
2014-06-05 22:49:58,491 -0700 [main] INFO (Configuration.java:2214) - HHH000041: Configured SessionFactory: null
2014-06-05 22:49:58,557 -0700 [main] WARN (DriverManagerConnectionProviderImpl.java:93) - HHH000402: Using Hibernate built-in connection pool (not for production use!)
2014-06-05 22:49:58,560 -0700 [main] INFO (DriverManagerConnectionProviderImpl.java:166) - HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/TestDB]
2014-06-05 22:49:58,561 -0700 [main] INFO (DriverManagerConnectionProviderImpl.java:175) - HHH000046: Connection properties: {user=pankaj, password=****}
2014-06-05 22:49:58,561 -0700 [main] INFO (DriverManagerConnectionProviderImpl.java:180) - HHH000006: Autocommit mode: false
2014-06-05 22:49:58,562 -0700 [main] INFO (DriverManagerConnectionProviderImpl.java:102) - HHH000115: Hibernate connection pool size: 20 (min=1)
2014-06-05 22:49:58,699 -0700 [main] INFO (Dialect.java:145) - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2014-06-05 22:49:58,706 -0700 [main] INFO (LobCreatorBuilder.java:97) - HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
2014-06-05 22:49:58,828 -0700 [main] INFO (TransactionFactoryInitiator.java:62) - HHH000399: Using default transaction strategy (direct JDBC transactions)
2014-06-05 22:49:58,832 -0700 [main] INFO (ASTQueryTranslatorFactory.java:47) - HHH000397: Using ASTQueryTranslatorFactory
2014-06-05 22:49:59,207 -0700 [main] INFO (DriverManagerConnectionProviderImpl.java:281) - HHH000030: Cleaning up connection pool [jdbc:mysql://localhost/TestDB]
由于我们的hibernate日志级别设置为INFO模式,因此日志不多。如果您将其更改为调试,即使这个简单的程序也会生成超过1400行的日志。分析这些日志有时候很难理解hibernate如何在内部工作以及调试与hibernate相关的问题。
此外,我们在控制台中生成了以下日志。
Before log4j configuration
After log4j configuration
Hibernate: select employee0_.emp_id as emp_id1_1_, employee0_.emp_name as emp_name2_1_, employee0_.emp_salary as emp_sala3_1_ from EMPLOYEE employee0_
Hibernate: select address0_.emp_id as emp_id1_0_0_, address0_.address_line1 as address_2_0_0_, address0_.city as city3_0_0_, address0_.zipcode as zipcode4_0_0_, employee1_.emp_id as emp_id1_1_1_, employee1_.emp_name as emp_name2_1_1_, employee1_.emp_salary as emp_sala3_1_1_ from ADDRESS address0_ left outer join EMPLOYEE employee1_ on address0_.emp_id=employee1_.emp_id where address0_.emp_id=?
Hibernate: select address0_.emp_id as emp_id1_0_0_, address0_.address_line1 as address_2_0_0_, address0_.city as city3_0_0_, address0_.zipcode as zipcode4_0_0_, employee1_.emp_id as emp_id1_1_1_, employee1_.emp_name as emp_name2_1_1_, employee1_.emp_salary as emp_sala3_1_1_ from ADDRESS address0_ left outer join EMPLOYEE employee1_ on address0_.emp_id=employee1_.emp_id where address0_.emp_id=?
Hibernate: select address0_.emp_id as emp_id1_0_0_, address0_.address_line1 as address_2_0_0_, address0_.city as city3_0_0_, address0_.zipcode as zipcode4_0_0_, employee1_.emp_id as emp_id1_1_1_, employee1_.emp_name as emp_name2_1_1_, employee1_.emp_salary as emp_sala3_1_1_ from ADDRESS address0_ left outer join EMPLOYEE employee1_ on address0_.emp_id=employee1_.emp_id where address0_.emp_id=?
Hibernate: select address0_.emp_id as emp_id1_0_0_, address0_.address_line1 as address_2_0_0_, address0_.city as city3_0_0_, address0_.zipcode as zipcode4_0_0_, employee1_.emp_id as emp_id1_1_1_, employee1_.emp_name as emp_name2_1_1_, employee1_.emp_salary as emp_sala3_1_1_ from ADDRESS address0_ left outer join EMPLOYEE employee1_ on address0_.emp_id=employee1_.emp_id where address0_.emp_id=?
请注意,Hibernate正在向控制台日志打印SQL查询,这是从org.hibernate.engine.jdbc.spi.SqlStatementLogger
类logStatement
方法发生的,代码片段是:
if (this.logToStdout)
System.out.println("Hibernate: " + statement);
这由hibernate.show_sql
hibernate.cfg.xml文件中的属性控制。实际上,如果您使用的是log4j,那么您应该将其关闭,因为如果日志级别设置为DEBUG模式,它们也将成为日志文件的一部分。将它放在日志文件中更有意义,将它打印在控制台上。
这就是hibernate log4j日志示例的全部内容,因为您可以看到它非常容易插件,我们所需要的只是正确配置log4j。您可以从下面的链接下载该项目。
下载Hibernate Log4j项目
转载来源:https://www.journaldev.com/2984/hibernate-log4j-logging