如何将 HornetQ 日志记录委托给 Slf4j

发布于 2024-11-20 00:38:22 字数 977 浏览 5 评论 0原文

我希望将嵌入在我的应用程序中运行的 HornetQ 的所有日志记录委托给 slf4j。不幸的是任何尝试都失败了。我已经尝试

  • 添加 jul-to-slf4j
  • 安装桥 SLF4JBridgeHandler.install();
  • 尝试了 Log4jLogDelegateFactory (“hornetq-configuration.xml”通过org.hornetq.integration.logging.Log4jLogDelegateFactory)
  • 编写了我自己的LogDelegate 和 LogDelegateFactory

但无论我做什么,以下几行都不会被捕获:

13.07.2011 17:42:11 org.hornetq.core.logging.impl.JULLogDelegate warn
WARNUNG: AIO wasn't located on this platform, it will fall back to using pure Java NIO. If your platform is Linux, install LibAIO to enable the AIO journal
13.07.2011 17:42:11 org.hornetq.core.logging.impl.JULLogDelegate info
INFO: Using NIO Journal
13.07.2011 17:42:11 org.hornetq.core.logging.impl.JULLogDelegate info
INFO: Started Netty Acceptor version 3.1.5.GA-r1772

我错过了什么?

I want that all logging from HornetQ, which runs embedded in my application, is delegated to slf4j. Unfortunately any attempt failed. I allready tried

  • added jul-to-slf4j
  • installed the bridge SLF4JBridgeHandler.install();
  • Tried the Log4jLogDelegateFactory ("hornetq-configuration.xml" via <log-delegate-factory-class-name>org.hornetq.integration.logging.Log4jLogDelegateFactory</log-delegate-factory-class-name>)
  • Wrote my own LogDelegate and LogDelegateFactory

But whatever I do, the folloing lines are not catched:

13.07.2011 17:42:11 org.hornetq.core.logging.impl.JULLogDelegate warn
WARNUNG: AIO wasn't located on this platform, it will fall back to using pure Java NIO. If your platform is Linux, install LibAIO to enable the AIO journal
13.07.2011 17:42:11 org.hornetq.core.logging.impl.JULLogDelegate info
INFO: Using NIO Journal
13.07.2011 17:42:11 org.hornetq.core.logging.impl.JULLogDelegate info
INFO: Started Netty Acceptor version 3.1.5.GA-r1772

Anything I did miss?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最终幸福 2024-11-27 00:38:22

我找到了一个完整的解决方案,我想与您分享。
我之前已经单独尝试过其中每一个,但是您必须结合其他答案中提到的一些技术。

  • 您需要编写自己的 LogDelegate:
import org.hornetq.spi.core.logging.LogDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Slf4jLogDelegate implements LogDelegate {
    private final Logger logger;
    public Slf4jLogDelegate(Class clazz) {
        logger = LoggerFactory.getLogger(clazz);
    }
    @Override
    public void debug(Object message) {
        logger.debug(message.toString());
    }
    @Override
    public void debug(Object message, Throwable t) {
        logger.debug(message.toString(), t);
    }
    @Override
    public void error(Object message) {
        logger.error(message.toString());
    }
    @Override
    public void error(Object message, Throwable t) {
        logger.error(message.toString(), t);
    }
    @Override
    public void fatal(Object message) {
        logger.error(message.toString());
    }
    @Override
    public void fatal(Object message, Throwable t) {
        logger.error(message.toString(), t);
    }
    @Override
    public void info(Object message) {
        logger.info(message.toString());
    }
    @Override
    public void info(Object message, Throwable t) {
        logger.info(message.toString(), t);
    }
    @Override
    public boolean isDebugEnabled() {
        return logger.isDebugEnabled();
    }
    @Override
    public boolean isInfoEnabled() {
        return logger.isInfoEnabled();
    }
    @Override
    public boolean isTraceEnabled() {
        return logger.isTraceEnabled();
    }
    @Override
    public void trace(Object message) {
        logger.trace(message.toString());
    }
    @Override
    public void trace(Object message, Throwable t) {
        logger.trace(message.toString(), t);
    }
    @Override
    public void warn(Object message) {
        logger.warn(message.toString());
    }
    @Override
    public void warn(Object message, Throwable t) {
        logger.warn(message.toString(), t);
    }
}
  • 然后您需要编写工厂:
import org.hornetq.spi.core.logging.LogDelegate;
import org.hornetq.spi.core.logging.LogDelegateFactory;
public class Slf4jLogDelegateFactory implements LogDelegateFactory {
    // Maybe caching of delegates makes sense?
    @Override
    public LogDelegate createDelegate(Class clazz) {
        return new Slf4jLogDelegate(clazz);
    }
}
  • 然后将以下行添加到您的 hornetq-configuration.xml 中:
    yourpackage.Slf4jLogDelegateFactory
  • 最后,在启动嵌入式服务器之前添加以下行:
    org.hornetq.core.logging.Logger.setDelegateFactory(new Slf4jLogDelegateFactory());

我只是想知道为什么他们不能使用经过验证的日志机制,这样可以轻松交换记录器。

I found a complete solution that I want to share with you.
I have previously tried each of these on their own, but you have to combine some of the techniques mentioned in other answers.

  • You need to write your own LogDelegate:
import org.hornetq.spi.core.logging.LogDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Slf4jLogDelegate implements LogDelegate {
    private final Logger logger;
    public Slf4jLogDelegate(Class clazz) {
        logger = LoggerFactory.getLogger(clazz);
    }
    @Override
    public void debug(Object message) {
        logger.debug(message.toString());
    }
    @Override
    public void debug(Object message, Throwable t) {
        logger.debug(message.toString(), t);
    }
    @Override
    public void error(Object message) {
        logger.error(message.toString());
    }
    @Override
    public void error(Object message, Throwable t) {
        logger.error(message.toString(), t);
    }
    @Override
    public void fatal(Object message) {
        logger.error(message.toString());
    }
    @Override
    public void fatal(Object message, Throwable t) {
        logger.error(message.toString(), t);
    }
    @Override
    public void info(Object message) {
        logger.info(message.toString());
    }
    @Override
    public void info(Object message, Throwable t) {
        logger.info(message.toString(), t);
    }
    @Override
    public boolean isDebugEnabled() {
        return logger.isDebugEnabled();
    }
    @Override
    public boolean isInfoEnabled() {
        return logger.isInfoEnabled();
    }
    @Override
    public boolean isTraceEnabled() {
        return logger.isTraceEnabled();
    }
    @Override
    public void trace(Object message) {
        logger.trace(message.toString());
    }
    @Override
    public void trace(Object message, Throwable t) {
        logger.trace(message.toString(), t);
    }
    @Override
    public void warn(Object message) {
        logger.warn(message.toString());
    }
    @Override
    public void warn(Object message, Throwable t) {
        logger.warn(message.toString(), t);
    }
}
  • Then you need to write the factory:
import org.hornetq.spi.core.logging.LogDelegate;
import org.hornetq.spi.core.logging.LogDelegateFactory;
public class Slf4jLogDelegateFactory implements LogDelegateFactory {
    // Maybe caching of delegates makes sense?
    @Override
    public LogDelegate createDelegate(Class clazz) {
        return new Slf4jLogDelegate(clazz);
    }
}
  • Then add to your hornetq-configuration.xml the following line:
    <log-delegate-factory-class-name>yourpackage.Slf4jLogDelegateFactory</log-delegate-factory-class-name>
  • Finally, add the following line before you start the embedded server:
    org.hornetq.core.logging.Logger.setDelegateFactory(new Slf4jLogDelegateFactory());

I'm just wondering why they can't use proven logging mechanisms, that make it easy to exchange a logger.

痴情 2024-11-27 00:38:22

由于您正在运行嵌入式,请尝试在启动服务器之前添加以下代码:

org.hornetq.core.logging.Logger.setDelegateFactory(new YourOwnDelegateFactory())

您可能会收到一些使用旧委托的消息,直到服务器从配置。

Since you are running Embedded, try adding this code before you start the server:

org.hornetq.core.logging.Logger.setDelegateFactory(new YourOwnDelegateFactory())

You had probably a few messages using the old delegate until the server got it from the config.

街道布景 2024-11-27 00:38:22

在尝试此操作时,我还发现,当 HornetQServerImpl 初始化其静态日志时,它会在加载新的 LogDelegateFactory 之前执行此操作,因此不要使用我在 hornetq 上指定的日志-configuration.xml 它使用 JULLogDelegateFactory 因此其日志直接输出到控制台。

深入研究 HornetQ 的代码,我发现它在 Logger.initialise() 上初始化它,并且它还在其中查找名为“org.hornetq.logger-delegate-factory-class-”的系统属性name" 设置默认工厂,所以我用它来解决我的问题。

我已经使用 Spring 修复了这个问题:

<!-- This property must be set so HornetQServerImpl logger uses our factory -->
<bean id="systemPrereqs" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="java.lang.System" />
    <property name="targetMethod" value="setProperty" />
    <property name="arguments">
        <list>
            <value>org.hornetq.logger-delegate-factory-class-name</value>
            <value>my.Slf4jLogDelegateFactory</value>
        </list>
    </property>
</bean>

When trying this, I also found that when HornetQServerImpl initialized its static log, it does so before loading the new LogDelegateFactory so instead of using the one I'm specifying on the hornetq-configuration.xml it uses JULLogDelegateFactory so its logs are directly output to the console.

Diving on HornetQ's code I found that it initializes this on Logger.initialise() and that also there it looks for a System property named "org.hornetq.logger-delegate-factory-class-name" to set the default factory, so I used that to fix my problem.

I've fixed this using Spring:

<!-- This property must be set so HornetQServerImpl logger uses our factory -->
<bean id="systemPrereqs" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="java.lang.System" />
    <property name="targetMethod" value="setProperty" />
    <property name="arguments">
        <list>
            <value>org.hornetq.logger-delegate-factory-class-name</value>
            <value>my.Slf4jLogDelegateFactory</value>
        </list>
    </property>
</bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文