log4j2.xml RollingFile配置的正确格式 [英] Proper format for log4j2.xml RollingFile configuration

查看:1549
本文介绍了log4j2.xml RollingFile配置的正确格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用log4j2的glassfish 4应用程序中遇到以下异常:

I am getting the following exception in my glassfish 4 application that uses log4j2:

SEVERE: ERROR StatusLogger Invalid URL C:/glassfish4/glassfish/domains/domain1/config/log4j2.xml java.net.MalformedURLException: Unknown protocol: c

我的log4j2.xml中包含以下部分:

I have the following section in my log4j2.xml:

<RollingFile name="RollingFile" fileName="C:/glassfish4/glassfish/domains/domain1/logs/ucsvc.log"
             filePattern="C:/glassfish4/glassfish/domains/domain1/logs/$${date:yyyy-MM}/ucsvc-%d{MM-dd-yyyy}-%i.log">

我知道,如果要查找URL,则"C:/glassfish4/..."的格式不正确.

I understand that if it's looking for a URL, then "C:/glassfish4/..." is not the correct format.

但是,滚动文件部分实际上可以正常工作:我在期望的位置看到一个日志文件和滚动日志文件.

However, the rolling file part actually works: I see a log file and the rolled log files where I expect them.

如果我更改为根本无法使用的网址(例如file:///C/glassfish4/...).

If I change to a URL (e.g. file:///C/glassfish4/...) that doesn't work at all.

那么我应该忽略该异常吗? (一切似乎都正常).还是有人可以解释此部分配置的正确格式?

So should I ignore the exception? (everything seems to be working ok). Or can someone explain the correct format for this section of the configuration?

推荐答案

我尚未完全确定为什么这是因为配置文件对我以及OP都适用,但是,我可以确认将路径引用更改为file://url可以解决问题(即:摆脱错误/警告/刺激性).

I have not yet fully determined why it is that the config file works for me as well as the OP, but, I can confirm that changing the path reference to a file:// url solves the problem (ie: gets rid of the error/warning/irritant).

在我的IntelliJ Run/Debug配置中,对于 VM选项,我具有:

In my IntelliJ Run/Debug configurations, for VM options, I have:

-Dlog4j.configurationFile=file://C:\dev\path\to\log4j2.xml

我可以确认将'\'转换为'/',所以不用担心.

I can confirm that '\' are translated to '/' so, no worries there.

编辑:

好的,因为他们(apache家伙)非常努力地加载配置,并且事实上他们确实是通过c:\...表示法从文件中加载的,所以整个过程都可以正常工作.在继续尝试之前,他们只是抛出了一个颇具误导性的例外.

Okay, the whole thing works because they (the apache guys) try really hard to load the configuration and they do, in fact, load from the file as specified via the c:\... notation. They just throw up a rather misleading exception before continuing to try.

在ConfigurationFactory :: getConfiguration:

In ConfigurationFactory::getConfiguration:

    **source = getInputFromURI(FileUtils.getCorrectedFilePathUri(config));**
} catch (Exception ex) {
    // Ignore the error and try as a String.
}
if (source == null) {
    final ClassLoader loader = this.getClass().getClassLoader();
    **source = getInputFromString(config, loader);**

第一行加粗的行尝试从URL加载并失败,并引发异常.然后,代码继续,并弹出到getInputFromString中:

The first bolded line tries to load from a URL and fails, throwing the exception. The code then continues, pops into getInputFromString:

try {
    final URL url = new URL(config);
    return new ConfigurationSource(url.openStream(), FileUtils.fileFromURI(url.toURI()));
} catch (final Exception ex) {
    final ConfigurationSource source = getInputFromResource(config, loader);
    if (source == null) {
        try {
            **final File file = new File(config);
            return new ConfigurationSource(new FileInputStream(file), file);**

在尝试再次加载配置的地方,失败并陷入困境,再次尝试,失败并最终成功,以粗体显示(使用File进行处理).

Where it tries to load the config again, fails and falls into the catch, tries again, fails and finally succeeds on the bolded lines (dealing with a File).

好吧,我想要加粗的代码行实际上只是包裹在**中;猜猜该网站不允许嵌套标签吗?不管怎样,你们都得到了意义.

Okay, the code lines I wanted in emphasize with bold are actually just wrapped in **; guess the site doesn't permit nested tags? Anyway, y'all get the meaning.

阅读起来有点混乱,但这就是为什么即使您收到看上去很讨厌(且完全具有误导性)的异常,它仍然起作用的原因.

It's all a bit of a mess to read, but that's why it works even though you get that nasty-looking (and wholly misleading) exception.

这篇关于log4j2.xml RollingFile配置的正确格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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