首页 > 代码库 > Hibernate4.3.5搭建Log4j日志环境

Hibernate4.3.5搭建Log4j日志环境

本文记录Hibernate4.3.5搭建Log4j日志环境的过程

1、搞清楚Hibernate4.3.5的日志环境依赖

方法:查看帮助文档

3.5. LoggingImportantCompletely out of date. Hibernate uses JBoss Logging starting in 4.0. This will get documented as we migrate this content to the Developer Guide.
Hibernate utilizes Simple Logging Facade for Java (SLF4J) in order to log various system events. SLF4J can direct your logging output to several logging frameworks (NOP, Simple, log4j version 1.2, JDK 1.4 logging, JCL or logback) depending on your chosen binding. In order to setup logging you will need slf4j-api.jar in your classpath together with the jar file for your preferred binding - slf4j-log4j12.jar in the case of Log4J. See the SLF4J documentation for more detail. To use Log4j you will also need to place a log4j.properties file in your classpath. An example properties file is distributed with Hibernate in the src/ directory.

第1段说hibernate4.0之后用JBoss Logging。具体怎么个用法还没研究过,第2段说Hibernate为了在不同的系统中记录日志利用了SLF4J,SLF4J是一套日志的接口,它可以匹配多种日志框架。为了 使用Log4j需要这两个jar包:slf4j-api.jar、slf4j-log4j12.jar。前者是slf4j的api包,后者是slf4j到log4j的适配器包。

看到这问题就来了,这两个jar包去哪里找呢?

方法:查看一下Hibernate4.3.5的lib目录里有没有,发现hibernate-release-4.3.5.Final\lib\optional\ehcache目录里有slf4j-api-1.6.1.jar,有了slf4j的api包,slf4j到log4j的适配器包去哪里找呢,去把slf4j官网把1.6.1下载下来看一下发现适配器官方发布包里面就有slf4j-1.6.1\slf4j-log4j12-1.6.1.jar。

有了这两个包就OK了吗?肯定不行啊,既然要搭建log4j日志环境,当然还需要log4j核心包,log4j的jar包版本那么多,我应该下载哪个版本呢?当然要下载适配器对应的版本了即:log4j-1.2.16,去官网下载下来。

2、目录结构

我们已经搞清楚了Hibernate4.3.5搭建log4j日志环境所需要的jar包,把他们添加到自己的工程里面,添加log4j的配置文件(示例文件:hibernate-release-4.3.5.Final\project\etc\log4j.properties),如此以后Hibernate4.3.5的log4j日志环境基本搞定。目录结构如下所示:

 

Hibernate4.3.5搭建Log4j日志环境