当您不需要本地化时,是否有一种简约的方法来获取默认的 ResourceBundle?

发布于 2024-12-13 10:19:03 字数 1190 浏览 1 评论 0原文

我想继承并扩展java.util.logging.Logger。唯一可见的构造函数是

protected Logger(String name, String resourceBundleName)

尽管事实上,当您想要获取实例时,它们提供了两种方法:

static Logger   getLogger(String name)
static Logger   getLogger(String name, String resourceBundleName)  

是否有方法获取某种默认的ResourceBundle或默认名称的ResourceBundle 以便我可以使用构造函数而无需创建国际化属性文件和关联对象?

当然,我可以简单地生成一个包装类并使用 getLogger(String name) 来避免需要 ResourceBundle 名称。不幸的是,这意味着我必须为我想要使用的每个 Logger 方法实现一个包装器方法。 (打字太多)。

编辑:以下从 Logger 获取默认本地化数据的尝试不起作用。

import java.util.logging.Logger;

class JLogger extends Logger
{
    static Logger dummyLogger = Logger.getLogger("com.dummy.utilities");

    private JLogger(String name)
    {
        super(name, JLogger.dummyLogger.getResourceBundleName());
    }

    static JLogger getJLogger(String name)
    {
        return new JLogger(name);
    }

    public void severe() // a zero arguments version of severe
    {
        severe("");
    }
}

getResourceBundleName() 仅返回 null。如果第二个参数为 null,则 super(name, resourceName) 构造函数会构建不执行任何日志记录的内容。

I want to inherit to extend java.util.logging.Logger. The only visible constructor is

protected Logger(String name, String resourceBundleName)

This is despite the fact that when you want to get an instance, they offer two ways:

static Logger   getLogger(String name)
static Logger   getLogger(String name, String resourceBundleName)  

Is there a way to obtain some sort of default ResourceBundle or default name of a ResourceBundle so that I can use the constructor without creating an internationalization properties file and associated object?

Of course I could simply produce a wrapper class and use getLogger(String name) to avoid the need for the ResourceBundle name. Unfortunately that means I would have to implement a wrapper method for every Logger method I want to use. (Too much typing).

Edit: The following attempt to get the default localization data from Logger does not work.

import java.util.logging.Logger;

class JLogger extends Logger
{
    static Logger dummyLogger = Logger.getLogger("com.dummy.utilities");

    private JLogger(String name)
    {
        super(name, JLogger.dummyLogger.getResourceBundleName());
    }

    static JLogger getJLogger(String name)
    {
        return new JLogger(name);
    }

    public void severe() // a zero arguments version of severe
    {
        severe("");
    }
}

The getResourceBundleName() just returns a null. The super(name, resourceName) constructor builds something which does not do any logging if the second argument is null.

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

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

发布评论

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

评论(1

西瓜 2024-12-20 10:19:03

不幸的是,这意味着我必须为我想要使用的每个 Logger 方法实现一个包装器方法。 (打字太多)。

不需要这样做,它已经完成了。看看SLF4JCommons Logging,它们为 java 提供了合理的包装器。 util.logging,隐藏了该 API 的糟糕之处。

这两个 API 还可以委托给适当的底层日志实现,例如 Logback 或 Log4J,完全绕过 java.util.logging。

Unfortunately that means I would have to implement a wrapper method for every Logger method I want to use. (Too much typing).

No need to do that, it's been done already. Have a look at SLF4J and Commons Logging, they provide sensible wrappers around java.util.logging, hiding the awfulness of that API.

Those two APIs can also delegate to a proper underlying log implementation such as Logback or Log4J, bypassing java.util.logging altogether.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文