Proper format for log4j2.xml RollingFile configuration

一笑奈何 提交于 2020-01-25 03:07:26

问题


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

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

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.

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?


回答1:


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

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.

EDIT:

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.

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);**

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);**

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.




回答2:


Thanks Jon, i was searching all over.. this helped!

This is on Intellij 13, Tomcat 7.0.56

-Dlog4j.configurationFile=file://C:\Surendra\workspace\cmdb\resources\elasticityLogging.xml




回答3:


The problem is not the contents of your log4j2.xml file.

The problem is that log4j2 cannot locate your log4j2.xml config file. If you look carefully at the error, the URL that is reported as invalid is C:/glassfish4/glassfish/domains/domain1/config/log4j2.xml: the config file.

I'm not sure why this is. Are you specifying the location of the config file via the system property -Dlog4j.configurationFile=path/to/log4j2.xml?

Still, if the application and logging works then perhaps there is no problem. Strange though. You can get more details about the log4j configuration by configuring <Configuration status="trace"> at the top of your log4j2.xml file. This will print log4j initialization details to the console.



来源:https://stackoverflow.com/questions/23394041/proper-format-for-log4j2-xml-rollingfile-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!