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

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

问题描述

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

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

如何配置 Hibernate 以登录到 SLF4J?

How can i configure Hibernate to log to SLF4J?

如果这不可能,我该如何配置 Hibernate 的日志记录?

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

Hibernate 4.1 手册 关于日志记录的部分以警告开始......

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

完全过时了.Hibernate 从 4.0 开始使用 JBoss Logging.当我们将此内容迁移到开发人员指南时,这将得到记录.

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.

... 继续讨论 SLF4J,所以没用.入门指南开发者指南 根本谈不上日志.迁移指南也没有.

... 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.

我已经查找了有关 jboss-logging 本身的文档,但我根本找不到任何文档.GitHub 页面是静默的,而 JBoss 的 社区项目页面 甚至没有列出 jboss-logging.我想知道项目的 错误跟踪器 可能有任何与提供文档相关的问题,但它没有.

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.

好消息是,在应用程序服务器(例如 JBoss AS7)中使用 Hibernate 4 时,日志记录主要由您处理.但是如何在独立应用程序中配置它?

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?

推荐答案

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();
}

所以 org.jboss.logging.provider 的可能值是:jbossjdklog4jslf4j.

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

如果您没有设置 org.jboss.logging.provider,它会尝试 jboss,然后是 log4j,然后是 slf4j(仅当使用 logback 时)并回退到 jdk.

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

我将 slf4jlogback-classic 一起使用:

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>

一切正常!

UPDATE 一些用户在非常主要的 App.java 中使用:

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 那些认为他们使用 SLF4J 管理 Log4j 以用于 jboss-logging 的人并不完全如此.jboss-logging 直接使用Log4j,不用SLF4J!

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天全站免登陆