以编程方式配置JPA Hibernate调试日志记录

时间:2019-05-14 07:41:37

标签: java hibernate jpa logging log4j

使用特定系统属性运行CLI应用程序时,我将激活调试日志,例如:

boolean verboseLogging = Boolean.getBoolean("verbose");
if (verboseLogging) {
   org.apache.log4j.Logger.getRootLogger().setLevel(Level.Debug);
}

我正在使用旧版log4j v1.2.17

然后是由JPA persistence.xml配置的Hibernate EntityManager v4.2.2.Final部分:

<persistence ...>
   <persistence-unit name="dt">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <class>...</class>
   </persistence-unit>
</persistence>

类似地,我想将Hibernate记录器设置为调试级别,并在该标志verboseLogging为true时激活Hibernate属性show_sql和hbm2ddl。所以我有这点:

if (verboseLogging) {
   Logger.getLogger("org.hibernate").setLevel(Level.DEBUG);
   Logger.getLogger("org.hibernate.sql").setLevel(Level.DEBUG);
   Logger.getLogger("org.hibernate.type").setLevel(Level.DEBUG);
   Logger.getLogger("org.hibernate.tool.hbm2ddl").setLevel(Level.DEBUG);

   Map<String, Object> properties = new HashMap<>();
   properties.put("hibernate.show_sql", true);
   return Persistence.createEntityManagerFactory("dt", properties);
}
return Persistence.createEntityManagerFactory("dt");

我一直在努力地激活Hibernate调试日志,但是它不起作用!

我想念什么?它是缺少的依赖项吗? 请注意,Hibernate实际上正在记录以下内容:

INFO  Version - HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
INFO  Version - HHH000412: Hibernate Core {4.2.2.Final}
INFO  Environment - HHH000206: hibernate.properties not found
INFO  Environment - HHH000021: Bytecode provider name : javassist
INFO  Dialect - HHH000400: Using dialect: org.hibernate.dialect.SybaseASE15Dialect
INFO  TransactionFactoryInitiator - HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
INFO  ASTQueryTranslatorFactory - HHH000397: Using ASTQueryTranslatorFactory
INFO  Version - Hibernate Validator 4.2.0.Final
WARN  TemplateRenderer - HHH000174: Function template anticipated 1 arguments, but 2 arguments encountered

我应该担心hibernate.properties not found吗?

0 个答案:

没有答案