开发者

referring to log4j configuration file from a log4j configuration file on class path

I have a common jar that multiple web applications are using. All the web applications are using log4j. Each has their own log4j xml configuration file that are basically the same. I would like to put a common log4j configuration in the included jar file and in each individua开发者_如何学Gol web project, in that log4j configuration, I would like to be able to simply refer to the one in the jar file.

I'm not seeing anything in the documentation that explicitly says this is possible. I'm wondering if I might just remove the configuration file from the web projects and stick it in the common jar if it would get loaded automatically since it is on the class path?

In the end, I would like the ability to adjust logging on a particular application for debugging and troubleshooting without modifying the common configuration in the jar file.


I was on a project where I needed to manage dozens of web applications. Each of the apps were logging slightly differently and it was a pretty big pain to manage. I used a strategy similar to what you are describing to standardize on log4j and it has worked out pretty well.

Basically, I created a single common.jar which contains shared code. This jar contains a log4j.xml that sets log level to INFO and sets the default appender to stdout for common.jar. This log4j config is used as the baseline for all other apps.

For this example, pretend this class is inside common.jar:

public class ThirdPartyLib 
{
    protected static final Logger log = LogManager.getLogger("third-party-lib");
    public void doSomething()
    {
        log.debug("Third Party App is about to Do something!");
        log.info("Third Party App just did something");
    }
}

Now, all other web applications can simply depend on common.jar. For example, pretend this class is inside myapp.war:

public class MyApp
{
    public void CallThirdParty()
    {
        ThirdPartyLib lib = new ThirdPartyLib();
        lib.doSomething();
    }
}

Since log4j.xml is inside common.jar, this code will log something like this. In other words, there's no need to put log4j.xml inside myapp.war:

2011-01-03 15:49:22,451 [main] INFO  third-party-lib - Third Party App just did something

Now, if you want/need to control logging in myapp.war, then simply place a log4j.xml inside myapp.war and override settings from common.jar. For example, you might set the level to DEBUG, and then you'll see:

2011-01-03 16:03:22,928 [main] DEBUG third-party-lib - Third Party App is about to Do something!
2011-01-03 16:03:22,928 [main] INFO  third-party-lib - Third Party App just did something

UPDATE - Is it possible to configure each webapp to log to separate file?

Yes, definitely. For example, you can direct logs to a RollingFileAppender instead of stdout inside log4j.xml for myapp.war. This is handy because then you can have each individual webapp log to it's own separate file.

In addition, I wrote a servlet filter (similar to what gigadot suggested) which configures log4j to check for whether a log4j.xml exists at an external path (outside of each webapp's war). This way, the log4j.xml files are accessible outside the wars and we're able to set log levels without restarting the servlet container (tomcat, in this case). If the external log4j.xml doesn't exist, it defaults to use the log4j.xml on each apps classpath.


If you have multiple jar/war files, each file have its own log4j.xml and you don't want to worry about which log4j.xml get loaded, you can explicitly load the configuration yourself.

For a webapp project, put the following context listener as the first listener in your web.xml This will load log4j from your path of preferrence.

<listener>
    <listener-class>mycompany.server.listener.Log4JInitServletContextListener</listener-class>
</listener>

Implementation of Log4JInitServletContextListener.

Note: if you have multiple log4j.xml in the same path (which is possible for multiple jar files), it will be confusing which one gets loaded. So try to have a unique package name for the configuration resource in your common jar.

As you can see, you can use this method to programatically load log4j.xml depending on condition too.

package mycompany.server.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.xml.DOMConfigurator;

public class Log4JInitServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        org.w3c.dom.Element log4jConfigElement = parseFromInputStream(getClass().getResourceAsStream("/unique/package/name/in/common/jar/log4j.xml"););
        DOMConfigurator.configure(log4jConfigElement);
    }

    // omit the rest and implementation of parseFromInputStream method
}


i am not sure that can help but you can set property file location like this,

java -Dlog4j.configuration=jar:file:/full/path/to/app.jar!/log4j.properties -jar app.jar 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜