如何在Hibernate 4中配置日志以使用SLF4J [英] How do you configure logging in Hibernate 4 to use SLF4J

查看:173
本文介绍了如何在Hibernate 4中配置日志以使用SLF4J的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Hibernate 3.x slf4j 进行日志记录。 Hibernate 4.x使用 jboss-logging 。我正在编写一个独立的应用程序,它使用Hibernate 4和SLF4J进行日志记录。



如何配置Hibernate以记录到SLF4J?



如果这是不可能的,我怎么配置Hibernate的日志?



Hibernate 4.1手册有关日志记录的部分开头的警告是...


完全过时。 Hibernate从4.0开始使用JBoss Logging。这将在我们将此内容迁移到开发者指南时得到记录。


...继续讨论SLF4J,无用。 入门指南开发人员指南讨论关于日志记录的问题。 迁移指南也不是。



我已经找到了关于jboss日志本身的文档,但是我一直都找不到。 GitHub页面无提示和JBoss的社区项目页面甚至不会列出jboss日志记录。我想知道,如果个项目的 bug跟踪系统可能与提供文档的任何问题,但它并不



好消息是,在诸如JBoss AS7之类的应用服务器中使用Hibernate 4时,日志很大程度上为您处理。但是,如何在独立的应用程序中配置它? 解决方案

https://github.com/jboss-logging/jboss-logging/blob/master/ src / main / java / org / jboss / logging / LoggerProviders.java

  static final LOGGING_PROVIDER_KEY = org.jboss.logging.provider; 

private static LoggerProvider findProvider(){
//因为impl类直接引用后端框架,所以如果这个类加载器找不到目标
// log类,那么它们是否可以从TCCL获得并不重要,因为我们不会
//能够找到它
final ClassLoader cl = LoggerProviders.class.getClassLoader() ;
尝试{
//检查系统属性
最终字符串loggerProvider = AccessController.doPrivileged(新的PrivilegedAction<字符串>(){
公共字符串的run(){
返回System.getProperty(LOGGING_PROVIDER_KEY);
}
});
if(loggerProvider!= null){
if(jboss.equalsIgnoreCase(loggerProvider)){
return tryJBossLogManager(cl);
} else if(jdk.equalsIgnoreCase(loggerProvider)){
return tryJDK();
} else if(log4j.equalsIgnoreCase(loggerProvider)){
return tryLog4j(cl);
} else if(slf4j.equalsIgnoreCase(loggerProvider)){
return trySlf4j();


catch(Throwable t){
}
try {
return tryJBossLogManager(cl);
} catch(Throwable t){
// nope ...
}
try {
return tryLog4j(cl);
} catch(Throwable t){
// nope ...
}
尝试{
//只有在使用Logback的情况下才使用slf4j
Class.forName(ch.qos.logback.classic.Logger,false,cl);
return trySlf4j();
} catch(Throwable t){
// nope ...
}
return tryJDK();
}

可能的值为 org.jboss.logging。 provider 是: jboss jdk log4j slf4j



如果您没有设置 org.jboss .logging.provider 它会尝试jboss,然后是log4j,然后是slf4j(只有在使用了logback的情况下)以及后备才能使用jdk。

我使用 slf4j with logback-classic

 < dependency> 
< groupId> ch.qos.logback< / groupId>
< artifactId> logback-classic< / artifactId>
< version> 1.0.13< / version>
< scope> $ {logging.scope}< / scope>
< /依赖关系>

并且所有工作都很好!

<

  static {//在主类中运行被加载。 
System.setProperty(org.jboss.logging.provider,slf4j);
}

但是对于基于容器的解决方案,这是行不通的。



UPDATE 2 那些认为他们使用SLF4J为 jboss-logging 管理Log4j的人并不完全如此。 jboss-logging 直接使用Log4j而不使用SLF4J!


Hibernate 3.x used for logging. Hibernate 4.x uses . I am writing a standalone application which uses Hibernate 4, and SLF4J for logging.

How can i configure Hibernate to log to SLF4J?

If that's not possible, how can i configure Hibernate's logging at all?

The Hibernate 4.1 manual section on logging starts with the warning that it is ...

Completely out of date. Hibernate uses JBoss Logging starting in 4.0. This will get documented as we migrate this content to the Developer Guide.

... goes on to talk about SLF4J, and so is useless. Neither the getting started guide nor the developer guide talk about logging at all. Nor does the migration guide.

I have looked for documentation on jboss-logging itself, but i haven't been able to find any at all. The GitHub page is silent, and JBoss's community projects page doesn't even list jboss-logging. I wondered if th project's bug tracker might have any issues relating to providing documentation, but it doesn't.

The good news is that when using Hibernate 4 inside an application server, such as JBoss AS7, logging is largely taken care of for you. But how can i configure it in a standalone application?

解决方案

Look to https://github.com/jboss-logging/jboss-logging/blob/master/src/main/java/org/jboss/logging/LoggerProviders.java:

static final String LOGGING_PROVIDER_KEY = "org.jboss.logging.provider";

private static LoggerProvider findProvider() {
    // Since the impl classes refer to the back-end frameworks directly, if this classloader can't find the target
    // log classes, then it doesn't really matter if they're possibly available from the TCCL because we won't be
    // able to find it anyway
    final ClassLoader cl = LoggerProviders.class.getClassLoader();
    try {
        // Check the system property
        final String loggerProvider = AccessController.doPrivileged(new PrivilegedAction<String>() {
            public String run() {
                return System.getProperty(LOGGING_PROVIDER_KEY);
            }
        });
        if (loggerProvider != null) {
            if ("jboss".equalsIgnoreCase(loggerProvider)) {
                return tryJBossLogManager(cl);
            } else if ("jdk".equalsIgnoreCase(loggerProvider)) {
                return tryJDK();
            } else if ("log4j".equalsIgnoreCase(loggerProvider)) {
                return tryLog4j(cl);
            } else if ("slf4j".equalsIgnoreCase(loggerProvider)) {
                return trySlf4j();
            }
        }
    } catch (Throwable t) {
    }
    try {
        return tryJBossLogManager(cl);
    } catch (Throwable t) {
        // nope...
    }
    try {
        return tryLog4j(cl);
    } catch (Throwable t) {
        // nope...
    }
    try {
        // only use slf4j if Logback is in use
        Class.forName("ch.qos.logback.classic.Logger", false, cl);
        return trySlf4j();
    } catch (Throwable t) {
        // nope...
    }
    return tryJDK();
}

So possible values for org.jboss.logging.provider are: jboss, jdk, log4j, slf4j.

If you don't set org.jboss.logging.provider it tries jboss, then log4j, then slf4j (only if logback used) and fallback to jdk.

I use slf4j with logback-classic:

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.13</version>
        <scope>${logging.scope}</scope>
    </dependency>

and all work fine!

UPDATE Some users uses in very main App.java:

static { //runs when the main class is loaded.
    System.setProperty("org.jboss.logging.provider", "slf4j");
}

but for container based solutions this is not worked.

UPDATE 2 Those who think that they manage Log4j with SLF4J for jboss-logging it is not exactly thus. jboss-logging directly uses Log4j without SLF4J!

这篇关于如何在Hibernate 4中配置日志以使用SLF4J的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆