如何将java.util.logging发送到log4j2? [英] How to send java.util.logging to log4j2?

查看:56
本文介绍了如何将java.util.logging发送到log4j2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个使用 NetBeans 平台的项目.在底层,NetBeans使用 java.util.logging 框架.

I'm working on a project that uses the NetBeans Platform. Under the hood, NetBeans uses the java.util.logging framework.

我想通过将 java.util.logging 绑定到 log4j2 来拦截这些日志调用.这可能吗?

I would like to intercept these log calls by binding java.util.logging to log4j2. Is this possible?

已经问过类似的先前问题,但特定于 log4j 而不是 log4j2

Previous questions have been asked that are similar, but were specific to log4j and not log4j2!

推荐答案

我找到了一个解决方案:

I've found a solution:

NetBeans平台以 org.netbeans.* 命名空间命名记录器.要路由NetBeans日志调用,我只需创建一个 Handler 并将其注册到 org.netbeans 记录器中即可.

The NetBeans Platform names logger after the org.netbeans.* namespace. To route NetBeans log calls, I simply created a Handler and registered it with the org.netbeans logger:

public class CustomHandler extends Handler
{
    @Override
    public void publish(LogRecord record)
    {
        // Re-direct log calls here (e.g. send record to Log4j2 logger).
    }

    @Override
    public void flush() 
    {
    }

    @Override
    public void close() throws SecurityException
    {
    }
}

请确保注册新的 Handler 并在必要时禁用父记录器:

Be sure to register the new Handler and disable parent loggers if necessary:

Logger logger = Logger.getLogger("org.netbeans");
logger.addHandler(new CustomerHandler());
logger.setUseParentHandlers(false);

这篇关于如何将java.util.logging发送到log4j2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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